我想用Delphi调用VC写的动态链接库,有一句不能成功。不知道怎么传入参数.
VC中的动态链接库出口函数声明如下
__declspec(dllexport) int __stdcall SendHTTPByIE(LPCSTR URL,LPCSTR headers,const BYTE *Post, DWORD postLength);
我用VC调用如下:成功
BYTE PostData[73] = "actionprocess=8&doLogin=true&userinfo.username=admin&userinfo.password=1";
SendHTTPByIE("http://10.10.0.23:9080/CPAPLD/userinfoAction.do?","Content-Type: application/x-www-form-urlencoded", PostData, 73);PostData为数组PostData的首地址。
我自己声明的函数指针为
MyArray =array of byte;//动态数组
P=^MyArray ;//指向动态数组的指针
PMyIE = function(URL, headers: LPCSTR; const Post:P; postLength: DWORD): integer; stdcall;
调用的代码如下
var
tmpStr1, tmpStr2, tmpUrl, tmpPost: string;
haha: PMyIE;
i: integer;
AHandle: THandle;
testArray:myArray;
myP:P;
begin
SetLength(testArray,73) ;
tmpStr1 := 'actionprocess=8&doLogin=true&userinfo.username=admin&userinfo.password=1';
for i := 0 to length(tmpStr1) - 1 do
begin
tmpStr2 := copy(tmpStr1, i + 1, 1);
testArray[i] := Byte(tmpStr2[1]);
end;
myP^:=testArray ;
AHandle := LoadLibrary(pchar(ExtractFilePath(Application.ExeName) + 'PostData.dll'));
if AHandle <= 0 then
Exit;
@haha := GetProcAddress(AHandle, 'SendHTTPByIE');
haha('http://10.10.0.23:9080/CPAPLD/userinfoAction.do?', 'Content-Type: application/x-www-form-urlencoded',myP , 73);
end;
在程序执行到最后一步时出地址错误,在VC中调试传进来的内存地址是进来了,不过数据没有进来,抱歉我对Delphi的指针理解不是很深刻,
VC中的动态链接库出口函数声明如下
__declspec(dllexport) int __stdcall SendHTTPByIE(LPCSTR URL,LPCSTR headers,const BYTE *Post, DWORD postLength);
我用VC调用如下:成功
BYTE PostData[73] = "actionprocess=8&doLogin=true&userinfo.username=admin&userinfo.password=1";
SendHTTPByIE("http://10.10.0.23:9080/CPAPLD/userinfoAction.do?","Content-Type: application/x-www-form-urlencoded", PostData, 73);PostData为数组PostData的首地址。
我自己声明的函数指针为
MyArray =array of byte;//动态数组
P=^MyArray ;//指向动态数组的指针
PMyIE = function(URL, headers: LPCSTR; const Post:P; postLength: DWORD): integer; stdcall;
调用的代码如下
var
tmpStr1, tmpStr2, tmpUrl, tmpPost: string;
haha: PMyIE;
i: integer;
AHandle: THandle;
testArray:myArray;
myP:P;
begin
SetLength(testArray,73) ;
tmpStr1 := 'actionprocess=8&doLogin=true&userinfo.username=admin&userinfo.password=1';
for i := 0 to length(tmpStr1) - 1 do
begin
tmpStr2 := copy(tmpStr1, i + 1, 1);
testArray[i] := Byte(tmpStr2[1]);
end;
myP^:=testArray ;
AHandle := LoadLibrary(pchar(ExtractFilePath(Application.ExeName) + 'PostData.dll'));
if AHandle <= 0 then
Exit;
@haha := GetProcAddress(AHandle, 'SendHTTPByIE');
haha('http://10.10.0.23:9080/CPAPLD/userinfoAction.do?', 'Content-Type: application/x-www-form-urlencoded',myP , 73);
end;
在程序执行到最后一步时出地址错误,在VC中调试传进来的内存地址是进来了,不过数据没有进来,抱歉我对Delphi的指针理解不是很深刻,
myP^:=testArray ;
是在给指针所指向的内容赋值,而不是给指针本身赋值,改为:
myP := ◎testArray ;
就行了
BYTE PostData[73] = "actionprocess=8&doLogin=true&userinfo.username=admin&userinfo.password=1";
如果用PostData就指的是PostData[73]的首地址,即PostData[0]的地址,我也曾经这样写过
myP := ◎testArray[0];也没有成功,快崩溃了。
PMyIE = function(URL, headers: LPCSTR; const Post: Poinger; postLength: DWORD): integer; stdcall;
@haha := GetProcAddress(AHandle, 'SendHTTPByIE');
haha('http://10.10.0.23:9080/CPAPLD/userinfoAction.do?', 'Content-Type: application/x-www-form-urlencoded',@PostData , 73); 编译成功,但是执行时出错.
有请对C++有深刻理解的Delphi程序员进来讲一下
type
MyArray = array of byte;//¶¯Ì¬Êý×é
P = ^MyArray ;//Ö¸Ïò¶¯Ì¬Êý×éµÄÖ¸Õëprocedure TForm1.Button1Click(Sender: TObject);
var
tmpStr: string;
I: Integer;
testArray: myArray;
myP:P;
S: string;
begin
SetLength(testArray,73);
tmpStr := 'actionprocess=8&doLogin=true&userinfo.username=admin&userinfo.password=1';
for i := 0 to length(tmpStr) - 1 do
begin
testArray[i] := Byte(tmpStr[i + 1]);
end;
myP := @testArray ;
//以下测试证明上面这个赋值才是正确的
S := '';
for I := 0 to length(tmpStr) - 1 do
begin
S := S + Char(myP^[I]);
end;
ShowMessage(S);
end;