如何使用从dll运行的应用程序? [英] How to use an running application from a dll?

查看:248
本文介绍了如何使用从dll运行的应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用主机应用程序中的数据时遇到问题。

我想通过dll将主机发送一个字符串,然后将其与主机应用程序的某些数据相结合。

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.

只要在使用子句中包含表单,我可以使用方法将数据发送到主机中,当数据被收回时,我尝试添加一个lokal变量,这是当我获得访问冲突:

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:

主持人:

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;

我不认为dll正在使用正在运行的应用程序表单,这是导致访问冲突的原因(也许是im错误?)
如何使DLL知道正在运行的应用程序(即其主机应用程序),并使用该实例来获取或操纵数据本身?

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 ?

我使用Delphi 5。

Im using Delphi 5.

推荐答案

建议不要在应用程序和DLL边界之间传递本地Delphi对象。

如果你想这样做,你应该使用Packages而不是DLL。

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.

全局变量是应用程序和Dll之间不共享。

Global variables are not shared between application and Dll.

在您的情况下,您引用DLL中的全局 mainform 变量,如果调试该代码,您会发现您的主机应用程序中 mainform = nil或与 mainform 不同的另一个地址。

n your case, your referencing the global mainform variable in the DLL, if you debug that code you will find that mainform = nil or another address that is not the same as the mainform in your host application.

这篇关于如何使用从dll运行的应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆