我必须在 Delphi 6 中使用来自外部 C++ DLL 的编码函数。以下是提供的声明:
long <Function Name> (char *Data, long &Apply, char *ReturnVal, long &Size)
Data 是输入值,Apply 是布尔值(默认值:FALSE),ReturnVal 是来自 DLL 的返回值,Size 是 ReturnVal 的长度。
为了在 Delphi 6 中使用它,我编写了以下代码:
implementation
const
EncoderDLL = '<DLL NAME>';
FunctionName = 'FUNCTION NAME';
var
_TEST : function(const Data : PChar; Apply : PInteger;stOutput : Pchar;
iSize : PInteger) : integer; stdcall;
.....
.....
var
stInput,stOutput : string;
iLength,i1,iResult : integer;
hnd : THandle;
begin
iLength := 0;
i1 := 0;
stInput := Trim(edtInput.Text);
hnd := SafeLoadLibrary(EncoderDLL);
if hnd > 0 then
begin
@_TEST := GetProcAddress(hnd,FunctionName);
if @_TEST <> nil then
begin
iResult := _TEST(PChar(stInput),@i1,PChar(StOutput),@iLength); // ERROR
end;
end;
FreeLibrary(hnd);
end;
我在注释为“错误”的行中遇到访问冲突。
如果我将函数声明中的PChar替换为string,则访问冲突不会出现在同一行。它是在释放库时出现的。此外,iLength 参数的值已正确填充。
任何人都可以提供解决此问题的指示。