如何从 .NET Dll 或 Exe 访问 .NET 独立可执行文件的运行实例?

发布于 2024-07-22 05:14:21 字数 1490 浏览 3 评论 0原文

我有一个由 Windows 窗体项目和类库项目组成的应用程序。 当可执行文件启动时,它会在 dll 中设置一个静态值。

using MyClassLibrary;
namespace MyExeApplication
{
   public partial class MainForm : Form
   {
        Hashtable ht = null;
        void Form_Load(...)
        {
          ht = new Hashtable();
          ht.add("1", "Open");
          ht.add("2", "Close");
          Calculate.BasicValues = ht;
        }
        public Hashtable GetBasicValues()
        {
          return ht;
        }
   }

}     
namespace MyClassLibrary
{
   public class Calculate()
   {
       public static Hashtable BasicValues {get; set;}
   }
}

现在假设应用程序正在内存中运行(可执行文件)。 我的目的是创建另一个独立的应用程序,并在类库中的函数Calculate 中使用BasicValues 值。

using MyClassLibrary;
namespace TestApplication
{
  public partial class MainForm : Form
  {
     private void TestValueFromDll()
     {
          System.Windows.Forms.MessageBox.Show("Values of Hashtable");
          Hashtable ht = Calculate.BasicValues;
          //The hashtable is null and values are not there
          //The above will not work. Could I say something like
          //Get the running instance of MyExeApplication 
          //and invoke GetBasicValues() ?                
     }
  }
}

我猜它不起作用,因为我的 TestApplication 已将 MyClassLibrary.dll 复制到可执行 TestApplication.exe 所在的 bin 文件夹中。 因此使用了不同的 dll(不是第一个应用程序 MyExeApplication 使用的 dll)。

我的问题是如何解决这个问题? 是否可以使用反射并获取 MyExeApplication 的实例并从那里读取值? 还有其他办法吗?

谢谢

I have have an application which consists of an windows form project and a class library project. When it starts the executable sets a static value in the dll.

using MyClassLibrary;
namespace MyExeApplication
{
   public partial class MainForm : Form
   {
        Hashtable ht = null;
        void Form_Load(...)
        {
          ht = new Hashtable();
          ht.add("1", "Open");
          ht.add("2", "Close");
          Calculate.BasicValues = ht;
        }
        public Hashtable GetBasicValues()
        {
          return ht;
        }
   }

}     
namespace MyClassLibrary
{
   public class Calculate()
   {
       public static Hashtable BasicValues {get; set;}
   }
}

Now suppose the application is running in memory (the executable).
My purpose is to create another standalone application and use the value BasicValues in the function Calculate in the class library.

using MyClassLibrary;
namespace TestApplication
{
  public partial class MainForm : Form
  {
     private void TestValueFromDll()
     {
          System.Windows.Forms.MessageBox.Show("Values of Hashtable");
          Hashtable ht = Calculate.BasicValues;
          //The hashtable is null and values are not there
          //The above will not work. Could I say something like
          //Get the running instance of MyExeApplication 
          //and invoke GetBasicValues() ?                
     }
  }
}

I guess it does not work because my TestApplication has copied the MyClassLibrary.dll to the bin folder where the executable TestApplication.exe is located. Thus a different dll was used (not the dll that was used by the first application MyExeApplication).

And my question is how can I solve this problem?
Is it possible to use reflection and Get the instance of MyExeApplication and read the values from there? Is there any other way?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷梦 2024-07-29 05:14:21

这与 DLL 位于何处无关。 听起来真正的问题是 DLL 被加载到“MyExeApplication”应用程序域中,并且您需要从“TestApplication”应用程序域获取访问权限。 事实证明,.NET Remoting 旨在用于跨应用程序域的通信。 MSDN。

It is not a matter of where the DLL is located. It sounds like the real problem is that the DLL is loaded into the "MyExeApplication" appdomain and you need to get access from the "TestApplication" appdomain. And as it turns out, .NET Remoting was intended for communication across appdomains. There is a bunch of info about it over at MSDN.

摇划花蜜的午后 2024-07-29 05:14:21

如果您将 TestApplication、MyExeApplication 和 MyClassLibrary 构建到同一文件夹(在项目的“属性”菜单中设置输出文件夹),我想您可以避免使用反射。 另外,我很好奇为什么你需要做这样的事情? BasicValues 是否可以被 SQL 表甚至注册表设置替换?

祝你好运!

If you build TestApplication, MyExeApplication, and MyClassLibrary to the same folder (setting the output folder in the projects' Properties menu), I'd imagine you can avoid having to use reflection. Also, I'm curious as to why you'd need to do something like this? Is BasicValues something that could be replaced by a SQL Table or even registry settings?

Good luck!

薆情海 2024-07-29 05:14:21

据我所知,除了作为调试器附加到其他进程之外,没有办法做到这一点。

最好的选择是让第一个应用程序将哈希表存储在某个共享空间中。 作为磁盘上、数据库中的文件,具有某种形式的共享内存,或者通过使用远程处理来传递它。

您想要使用哪个选项实际上取决于每个应用程序将如何使用数据。

There's no way to do this, that I am aware of, short of attaching to the other process as a debugger.

Your best option is to have the first application store the Hashtable in some shared space. Either as a file on disk, in a database, with some form of shared memory, or pass it around by using remoting.

Which option you want to use will really depend on how the data is going to be used by each application.

瑾兮 2024-07-29 05:14:21

谢谢大家的回复。
我目前使用注册表作为临时存储来存储值。
我将阅读有关远程处理和应用程序域的内容,并尝试这样做。

我想要它的原因是我有一个 exe 应用程序在后台运行(最小化到 Windows 托盘)。 此 exe 应用程序充当中央 API,并生成一些 Web 登录信息,这些信息必须可从任何使用 MyClassLibrary.dll 的 TestApplication 进行访问。

如果我将 dll 类中的所有代码都放入 exe 中,然后只有一个可执行文件包含所有内容,这样会好吗?

然后在 MyTestApplication 中我可以添加对 exe 的引用。 但我仍然会遇到同样的问题。 因为如果我想创建另一个与 exe API 集成的应用程序,例如 MyTestApplication2 并添加对 exe 的引用,我还会遇到同样的问题吗?

我想从反思来看这是不可能的。
然而,在 VB6 中,您可以说 object o = GetObject(,"MyExeApplication.Class") 之类的内容,然后我可以说 o.GetBasicValues())。

Thank you all for the responses.
I am currently using the registry as temporary storage to store values.
I will read about remoting and application domains and will try to do that way.

The reason I want it, is that I have an exe application running in the background (minimized to windows tray). This exe app acts as an central API and generates some web login information which must be available to be accessed from any TestApplication that uses MyClassLibrary.dll.

Would it be good if I put all the code from my dll class to exe and then have only one executable which has everything?

Then in MyTestApplication I could add a reference to exe. But I would still have the same problem. Because if I want to create another application that integration with exe API, e.g. MyTestApplication2 and add a reference to exe would I still have the same problem?

I guess this is not possible from reflection.
However in VB6 you could say something like object o = GetObject(,"MyExeApplication.Class") and then I could say o.GetBasicValues()).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文