我试图将这段代码转换为delphi 7的代码
LIBEXPORT int __stdcall MY_GetDLLInfo(const char **pp_version, const char **pp_release_type, const char **pp_build_date, const char **pp_load_path);
// C++ Call Example
const char *p_version = NULL;
const char *p_release_type = NULL;
const char *p_build_date = NULL;
MY_GetDLLInfo(&p_version,&p_release_type,&p_build_date,NULL);
Delphi代码
MY_GetDLLInfo: function (const pp_version:PPAnsiChar;const pp_release_type:PPAnsiChar;const pp_build_date:PPAnsiChar;const pp_load_path:PPAnsiChar): Integer; stdcall;
// Delphi Call Example
var
hHandle:THandle;
p_version,p_release_type,p_build_date,p_load_path:PPAnsiChar;
begin
hHandle := LoadLibrary(Dl_path);
@MY_GetDLLInfo:=GetProcAddress(hHandle, PChar('MY_GetDLLInfo'));
if Assigned(MY_GetDLLInfo) then begin
MY_GetDLLInfo(p_version,p_release_type,p_build_date,@null);
ShowMessage(StrPas(@p_version)); // <- I Get Strange Output
ShowMessage(StrPas(@p_release_type)); // <- I Get Strange Output
ShowMessage(StrPas(@p_build_date)); // <- I Get Strange Output
end;
end;
我的转换后的代码得到奇怪的输出,我想我在使用strpas时做错了?
char*
在CC++中是 PAnsiChar
在德尔菲,和 char**
是 PPAnsiChar
.
你的本地字符串变量需要被声明为 PAnsiChar
不像 PPAnsiChar
,然后你需要使用 @
地址运算符(Delphi相当于CC++的 &
地址运算符)来将变量传递到函数中。
此外,还可以: null
是一个 Variant
在Delphi中。 要将一个空指针赋值给一个指针,需要使用 nil
而不是。
试试这个
// Delphi Call Example
var
hHandle: THandle;
MY_GetDLLInfo: function(const pp_version, pp_release_type, pp_build_date, pp_load_path: PPAnsiChar): Integer; stdcall;
p_version, p_release_type, p_build_date: PAnsiChar;
begin
hHandle := LoadLibrary(Dl_path);
if hHandle <> 0 then
begin
@MY_GetDLLInfo := GetProcAddress(hHandle, 'MY_GetDLLInfo');
if Assigned(MY_GetDLLInfo) then
begin
p_version := nil;
p_release_type := nil;
p_build_date := nil;
MY_GetDLLInfo(@p_version, @p_release_type, @p_build_date, nil);
ShowMessage(p_version);
ShowMessage(p_release_type);
ShowMessage(p_build_date);
end;
FreeLibrary(hHandle);
end;
end;