问题描述:
如何调用在delphi中调用vc写的dll?该dll不是自己的,只有如下描述信息
void CALLBACK test(LPSTR outStr ,LPCSTR inStr);我的做法:
在console程序中采用外部调用,代码如下
type
Ttest= procedure(outStr, inStr: LPSTR); stdcall;var
strOut, strIn: LPSTR;
DLLHandle: THandle;
test: Ttest;
begin
DLLHandle := LoadLibrary('test.dll');
if DLLHandle <> 0 then
begin
try
@test:= GetProcAddress(DLLHandle, 'test');
if @test<> nil then
begin
//调用
test(strOut, pAnsichar(/*some string*/);
//这时strOut应该是我要得到的东西了
use strOut.......
end;
end;
finally
FreeLibrary(DLLHandle);
end;end;但是代码在执行到test(strOut, pAnsichar(/*some string*/);时直接到finally模块了,说明函数调用失败了。可是我找不到问题。希望大家帮忙解决!谢谢!
如何调用在delphi中调用vc写的dll?该dll不是自己的,只有如下描述信息
void CALLBACK test(LPSTR outStr ,LPCSTR inStr);我的做法:
在console程序中采用外部调用,代码如下
type
Ttest= procedure(outStr, inStr: LPSTR); stdcall;var
strOut, strIn: LPSTR;
DLLHandle: THandle;
test: Ttest;
begin
DLLHandle := LoadLibrary('test.dll');
if DLLHandle <> 0 then
begin
try
@test:= GetProcAddress(DLLHandle, 'test');
if @test<> nil then
begin
//调用
test(strOut, pAnsichar(/*some string*/);
//这时strOut应该是我要得到的东西了
use strOut.......
end;
end;
finally
FreeLibrary(DLLHandle);
end;end;但是代码在执行到test(strOut, pAnsichar(/*some string*/);时直接到finally模块了,说明函数调用失败了。可是我找不到问题。希望大家帮忙解决!谢谢!
Exception EAccessViolation in module a.exe at 10001925.
Access violation at address 10002925. Write of address 00000000.
AV错误一般都是这些错误
if @test<> nil then
begin
//调用
//预先给strOut分配好足够的空间
strOut:=AllocMem(5000);
test(strOut, pAnsichar(/*some string*/);
//这时strOut应该是我要得到的东西了
use strOut.......
end;
应该预先分配空间
t1: string[255];
strOut := @t1;