如何解决Delphi 中 DLL 的结果,其中参数为 PAnsiChar
我有一个小问题。 在我的 DLL 中,我有:
uses
ShareMem,SysUtils,Classes,Dialogs;
function My_func (Param1,Param2,Param3: PAnsiChar) : Integer;
var
s1,s2,s3 : string;
begin
s1 := string(Param1);
s2 := string(Param2);
s3 := string(Param3);
ShowMessage(s1);
ShowMessage(s2);
ShowMessage(s3);
My_func := 0;
end;
来自我的 TestUnit.pas 的调用
unit Utestunit;
interface
uses
ShareMem,Windows,Messages,Variants,Graphics,Controls,Forms,Dialogs;
procedure Button1Click(Sender: TObject);
type
TMy_func = function (Param1,Param3: PAnsiChar) : Integer; StdCall;
var
my_func : TMyfunc;
error_code:integer;
My_library : THandle;
the_end : Boolean;
path_library,path_library_full : string;
test1,test2,test3 : array [0..255] of AnsiChar;
begin
// open the DLL
path_library := ExtractFilePath(Application.ExeName)+'DLL\RS_DLL.dll';
path_library_full := ExtractFilePath(Application.ExeName)+'DLL\';
SetCurrentDir(path_library_full);
try
the_end := False;
My_library := SafeLoadLibrary(PChar(path_library));
if My_library > 32 then
begin
@My_func := GetProcAddress(My_library,PChar('My_func'));
if @My_func = nil then
begin
ShowMessage('There is no library in '+path_library);
the_end := True;
end;
end
else
begin
error_code := GetLastError;
ShowMessage('Błąd biblioteki '+ sciezka_biblioteki+' nr:'+IntToStr(error_code));
the_end := True;
end;
if not the_end then
begin
// the calling
test1 := 'My string nr 1';
test2 := 'My string nr 2';
test3 := 'My string nr 3';
kod_bledu := My_func(
test1,test3
);
end;
finally
FreeLibrary(My_library);
SetCurrentDir(ExtractFilePath(Application.ExeName))
end;
end;
结果是:
My string nr 1
trash
trash
作为来自我的 DLL 的消息。
为什么只有第一个结果是好的,而其余的都是垃圾? 这只是我的应用程序中的测试。
解决方法
这段代码中有很多非常不好的习惯,我不想进入,因为它会花费很多精力。
但是,该行为的原因是您没有将 DLL 中的函数声明为 stdcall
。解决这个问题,文本将被正确传递。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。