如何带参数调用DLL程序?
我在使用带有参数的 Dll 过程发送时遇到问题,我不允许在我的测试项目中向 dll 方法的调用添加参数。
我试图调用这个 dll 方法:
procedure Transfer(sMessage: PChar); stdcall;
begin
MainForm.ShowThis(sMessage);
end;
exports
Transfer;
TestProj 使用这个:
procedure TForm1.Button1Click(Sender: TObject);
var
DLLHandle : THandle;
begin
DLLHandle := LoadLibrary ('C:\Program Files\Borland\Delphi5\Projects\Dll\MyLink.dll');
if DLLHandle >= 32 then
try
@Trans := GetProcAddress (DLLHandle, 'Transfer');
if @Trans <> nil then
Trans //Would like to say something like: Trans('Hello')
else
Showmessage('Could not load method address');
finally
FreeLibrary(DLLHandle);
end
else
Showmessage('Could not load the dll');
end;
如果我使用“Trans('Hello')”,我得到的编译错误是: [错误] Unit1.pas(51):实际参数太多。
我允许在没有参数的情况下运行它,但随后我只在我的显示消息框中收到乱码,然后崩溃,因为我不发送任何消息。
所以问题是我如何将字符串作为参数发送到 dll 中?我做错了什么?
I have problems sending using a Dll procedure with parameters, im not allowed to add parameters to the call of the dll method in my test project.
Im trying to call this dll method:
procedure Transfer(sMessage: PChar); stdcall;
begin
MainForm.ShowThis(sMessage);
end;
exports
Transfer;
TestProj using this:
procedure TForm1.Button1Click(Sender: TObject);
var
DLLHandle : THandle;
begin
DLLHandle := LoadLibrary ('C:\Program Files\Borland\Delphi5\Projects\Dll\MyLink.dll');
if DLLHandle >= 32 then
try
@Trans := GetProcAddress (DLLHandle, 'Transfer');
if @Trans <> nil then
Trans //Would like to say something like: Trans('Hello')
else
Showmessage('Could not load method address');
finally
FreeLibrary(DLLHandle);
end
else
Showmessage('Could not load the dll');
end;
The compile error i get if i use the "Trans('Hello')" is :
[Error] Unit1.pas(51): Too many actual parameters.
Im allowed to run it without the parameters but then i only get jiberish in my showmessage box and a crash after, since i dont send any message.
So the question is how do i get to send a string as a parameter into the dll ? what am i doing wrong ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应在赋值左侧使用指针符号 (@),Trans 变量应如下所示:
You should not use the pointer sign (@) at the left side of the assignment, the Trans variable should look like this: