如何使用 dll 中正在运行的应用程序?
我在使用主机应用程序中的数据时遇到问题。
我想通过 dll 向主机发送一个字符串,然后将其与主机应用程序中的一些数据结合起来。
通过仅在使用子句中包含表单,我可以使用方法将数据发送到主机,当接收数据时,我尝试添加一个 lokal 变量,这是我遇到访问冲突的时候:
Host:
procedure TMainForm.DllLink(sMessage: String);
begin
try
//This is ok:
Showmessage(sMessage);
//This is causes Access error:
Showmessage(sMessage +sPid);
except
Showmessage('Access violation');
end;
end;
Dll:
procedure Transfer(sMessage: PChar); stdcall;
var
sMyPid : String;
begin
try
//Get error if i try to use this method to get sPid which is a string:
sMyPid := MainForm.GetPid;
//Or this:
MainForm.NextsysDllLink(sMessage);
except
showmessage('Error');
end;
end;
I don't think the dll 正在使用正在运行的应用程序表单,这就是导致访问冲突的原因(也许我错了?) 如何使 dll 知道正在运行的应用程序(即其主机应用程序)并使用该实例从其自身获取或操作数据?
我使用的是 Delphi 5。
Im having problems using data from the host application.
I want to send a string through the dll into the host, then combine it with some data from the host app.
By just including the form in the uses clauses i can use methods to send data into the host, When the data is recived i try to add a lokal variable, this is when i get a access violation:
Host:
procedure TMainForm.DllLink(sMessage: String);
begin
try
//This is ok:
Showmessage(sMessage);
//This is causes Access error:
Showmessage(sMessage +sPid);
except
Showmessage('Access violation');
end;
end;
Dll:
procedure Transfer(sMessage: PChar); stdcall;
var
sMyPid : String;
begin
try
//Get error if i try to use this method to get sPid which is a string:
sMyPid := MainForm.GetPid;
//Or this:
MainForm.NextsysDllLink(sMessage);
except
showmessage('Error');
end;
end;
I dont think the dll is using the running applications forms that is what's causing the access violations (maybe im wrong ?)
How do I make the dll aware of a running application(that is its host app.) and use that instance to ether get or manipulate data from itself ?
Im using Delphi 5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
建议不要在应用程序和 DLL 边界之间传递本机 Delphi 对象。
如果你想这样做,你应该使用包而不是 DLL。
全局变量不在应用程序和 Dll 之间共享。
在您的情况下,您引用 DLL 中的全局
mainform
变量,如果您调试该代码,您会发现mainform
= nil 或另一个与主机应用程序中的mainform
。It is recommend to not pass native Delphi Objects between Application and DLL boundaries.
If you want to do that you should be using Packages instead of DLLs.
Global variables are not shared between application and Dll.
n your case, your referencing the global
mainform
variable in the DLL, if you debug that code you will find thatmainform
= nil or another address that is not the same as themainform
in your host application.ShareMem 应该可以解决问题。
ShareMem should do the trick.