调用c语言做的DLL,好像有点难度

dulei115 2006-04-13 10:12:24
如何调用下面的c做的一个DLL函数
DLL的c语法说明如下:
int Encrypt(IN long EncAlgID,IN const BYTE * PubKeyOrCert,IN DWORD PubKeyOrCertLen,IN const BYTE * PlanText,IN DWORD PlanTextLen,OUT BYTE * CipherText,IN OUT DWORD * CipherTextLen)

我试了几天了,参数类型改过好几次,效果最好的是这样
project 中 uses ShareMem

TEncrypt = function (EncAlgID: Longint; PubKeyOrCert: PChar;
PubKeyOrCertLen: PLongWord; PlanText: PChar; PlanTextLen: PLongWord;
CipherText: PChar; var CipherTextLen: PLongWord): Integer; stdcall;

传参数时PChar的全部用array[0..255] of char

---------------------------------------------------
可是调用时,有时还是会出现读地址错,有时甚至还会将一些根本不相干的变量(例如某些循环变量、整形变量)值给改了,弄的我头都晕了。我有时怀疑是dll的问题,可是这个dll,人家用c调用没有问题。
---------------------------------------------------
有哪位知道为什么?该怎么改?先谢了!!!!!!!!
...全文
353 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
fcy_1012 2006-08-21
  • 打赏
  • 举报
回复
为什么程序结束时,报内存错误,但是有返回值
dulei115 2006-04-14
  • 打赏
  • 举报
回复
弄好了,如下:
原始定义:
int Encrypt(IN long EncAlgID,IN const BYTE * PubKeyOrCert,IN DWORD PubKeyOrCertLen,IN const BYTE * PlanText,IN DWORD PlanTextLen,OUT BYTE * CipherText,IN OUT DWORD * CipherTextLen)
------------------------------------
Delphi定义:
TEncrypt = function (EncAlgID: Longint; PubKeyOrCert: PChar;
PubKeyOrCertLen: LongWord; PlanText: PChar; PlanTextLen: LongWord;
CipherText: PChar; CipherTextLen: PLongWord): Integer; cdecl;
--------------------------------------------
调用:
var
...
vErrorCode: Integer;
vPubKey: array[0..127] of Char;
vPubKeyLen: LongWord;
vPlanText: array[0..255] of Char;
vPlanTextLen: LongWord;
vCipherText: array[0..255] of Char;
vCipherTextLen: LongWord;
...
begin
...
vErrorCode := TEncrypt(pProc)(2, @vPubKey[0], vPubKeyLen, @vPlanText[0], vPlanTextLen, @vCipherText[0], @vCipherTextLen);
...
end;
-----------------------------------------------------
先前我也试过将stdcall换成cdecl,但是传参数的时候直接传的arrayofchar,而不是@arrayofchar[0],结果出错,后来改好了又忘了试试cdecl,唉,怪我整个人都给弄晕了。
------------------------------------
谢谢各位了,等会再结贴
dulei115 2006-04-14
  • 打赏
  • 举报
回复
弄好了,如下:
原始定义:
int Encrypt(IN long EncAlgID,IN const BYTE * PubKeyOrCert,IN DWORD PubKeyOrCertLen,IN const BYTE * PlanText,IN DWORD PlanTextLen,OUT BYTE * CipherText,IN OUT DWORD * CipherTextLen)
------------------------------------
Delphi定义:
TEncrypt = function (EncAlgID: Longint; PubKeyOrCert: PChar;
PubKeyOrCertLen: LongWord; PlanText: PChar; PlanTextLen: LongWord;
CipherText: PChar; CipherTextLen: PLongWord): Integer; cdecl;
--------------------------------------------
调用:
var
...
vErrorCode: Integer;
vPubKey: array[0..127] of Char;
vPubKeyLen: LongWord;
vPlanText: array[0..255] of Char;
vPlanTextLen: LongWord;
vCipherText: array[0..255] of Char;
vCipherTextLen: LongWord;
...
begin
...
vErrorCode := TUTEncrypt(pProc)(2, @vPubKey[0], vPubKeyLen, @vPlanText[0], vPlanTextLen, @vCipherText[0], @vCipherTextLen);
...
end;
-----------------------------------------------------
先前我也试过将stdcall换成cdecl,但是传参数的时候直接传的arrayofchar,而不是@arrayofchar[0],结果出错,后来改好了又忘了试试cdecl,唉,怪我整个人都给弄晕了。
------------------------------------
谢谢各位了,等会再结贴
dulei115 2006-04-14
  • 打赏
  • 举报
回复
弄好了,如下:
原始定义:
int Encrypt(IN long EncAlgID,IN const BYTE * PubKeyOrCert,IN DWORD PubKeyOrCertLen,IN const BYTE * PlanText,IN DWORD PlanTextLen,OUT BYTE * CipherText,IN OUT DWORD * CipherTextLen)
------------------------------------
Delphi定义:
TEncrypt = function (EncAlgID: Longint; PubKeyOrCert: PChar;
PubKeyOrCertLen: LongWord; PlanText: PChar; PlanTextLen: LongWord;
CipherText: PChar; CipherTextLen: PLongWord): Integer; cdecl;
--------------------------------------------
调用:
var
...
vErrorCode: Integer;
vPubKey: array[0..127] of Char;
vPubKeyLen: LongWord;
vPlanText: array[0..255] of Char;
vPlanTextLen: LongWord;
vCipherText: array[0..255] of Char;
vCipherTextLen: LongWord;
...
begin
...
vErrorCode := TUTEncrypt(pProc)(2, @vPubKey[0], vPubKeyLen, @vPlanText[0], vPlanTextLen, @vCipherText[0], @vCipherTextLen);
...
end;
-----------------------------------------------------
先前我也试过将stdcall换成cdecl,但是传参数的时候直接传的arrayofchar,而不是@arrayofchar[0],结果出错,还来改好了又忘了试试cdecl,唉,怪我整个人都给弄晕了。
------------------------------------
谢谢各位了,等会再结贴
aiirii 2006-04-13
  • 打赏
  • 举报
回复
不用 uses ShareMem

PChar 就 pchar, 不用再自己定义了

c 的 long 对应是 integer
有个专门转化 c 到 pascal 的工具 (是C, 不是C++)
dext 2006-04-13
  • 打赏
  • 举报
回复
对,把 stdcall 改成 cdecl 试试
jiju 2006-04-13
  • 打赏
  • 举报
回复
如果你给出的是完整的DLL的c语法说明 那你申明的函数入栈方式就不对 stdcall 改为__cdecl
alpsgem 2006-04-13
  • 打赏
  • 举报
回复
全是红星星,观摩
madyak 2006-04-13
  • 打赏
  • 举报
回复
procedure TForm1.Button1Click(Sender: TObject);
var
a: array[0..255] of Byte;
s:string;
begin
s:='ABCDEFG';
Move(s[1],a[0],Length(s));
Edit1.Text:=PChar(@a[0]);
end;
dulei115 2006-04-13
  • 打赏
  • 举报
回复
那就和我开贴的时候说的一样了,改成那样之后的问题是:
为了方便我把这个函数又封装了一下,结果在调用封装后的函数时,会改变其它不相干的变量的值(一般是整形变量)。好像是内存改动了
madyak 2006-04-13
  • 打赏
  • 举报
回复
如果你觉得不方便,你可以用Pchar代替PByte;
相应的array[0..255] of Byte改为array[0..255] of Char;
你完全可以不考虑类型转换的问题,采用Move来操作内存块.
madyak 2006-04-13
  • 打赏
  • 举报
回复
参数PByte类型,也可以用PChar取代,二者区别不大
array[0..255] of char

AEncAlgID: Longint;
APubKeyOrCert: array[0..255] of Byte;
APubKeyOrCertLen: LongWord;
APlanText: array[0..255] of Byte;
APlanTextLen: LongWord;
ACipherText: array[0..255] of Byte;
ACipherTextLen: LongWord
....


function (AEncAlgID, PByte(@APubKeyOrCert[0]), APubKeyOrCertLen;PByte(@PlanText[0]),APlanTextLen, PByte(@ACipherText[0]), @ACipherTextLen ):
dulei115 2006-04-13
  • 打赏
  • 举报
回复
怎样把一个string类型的数据传给PByte,最后得到的PByte怎么转换为String
madyak 2006-04-13
  • 打赏
  • 举报
回复
参数PByte类型,也可以用PChar取代,二者区别不大
array[0..255] of char

AEncAlgID: Longint;
APubKeyOrCert: array[0..255] of Byte;
APubKeyOrCertLen: LongWord;
APlanText: array[0..255] of Byte;
APlanTextLen: LongWord;
ACipherText: array[0..255] of Byte;
ACipherTextLen: LongWord
....


function (AEncAlgID, PByte(@APubKeyOrCert[0]), APubKeyOrCertLen;PByte(@ACipherText[0]), @ACipherTextLen ):
dulei115 2006-04-13
  • 打赏
  • 举报
回复
to madyak
怎么调用
madyak 2006-04-13
  • 打赏
  • 举报
回复
TEncrypt = function (EncAlgID: Longint; PubKeyOrCert: PByte;
PubKeyOrCertLen: LongWord; PlanText: PByte; PlanTextLen: LongWord;
CipherText: PByte; CipherTextLen: PLongWord): Integer; stdcall;
dulei115 2006-04-13
  • 打赏
  • 举报
回复
如果都用PChar,调用DLL的时候机会报错

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧