相同的DLL数据可以被2个不同的进程共享吗?

发布于 2024-09-04 07:11:11 字数 188 浏览 6 评论 0原文

我有两个同时运行的不同 C# 应用程序。

我希望它们都能够访问 DLL 的相同“实例”(也在 C# 中)。

该 DLL 保存了一些数据,我希望将这些数据返回给两个应用程序中任何一个需要它的应用程序。

我的 DLL 是线程安全的,所以我希望这是可能的,但我不确定如何实现。

任何帮助或建议将不胜感激。

I have two different C# applications that are running at the same time.

I would like both of them to be able to access the same "instance" of a DLL (also in C#).

The DLL holds some data that I'd like to return to whichever of the two applications is asking for it.

My DLL is thread-safe so I was hoping this would be possible but I'm not sure how.

Any help or advice would be much appreciated.

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

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

发布评论

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

评论(5

挽你眉间 2024-09-11 07:11:11

进程空间会有所不同,因此,DLL 中的全局变量将特定于每个单独的进程。内存中的代码可能会被共享(Windows 通常使用引用计数来提高该部分的效率)。

如果您想在两个进程之间共享 DLL 中访问的信息,那么似乎有必要使用某种 IPC(进程间通信)机制,例如套接字、共享内存、管道等。

The process space will be different so, for example, global variables in the DLL will be specific to each separate process. It is possible that the code in memory will be shared (Windows typically uses reference counting to make that part more efficient).

If you are wanting to share information that is accessed in the DLL between the two processes, then it seems likely that it will be necessary to use some kind of IPC (interprocess communication) mechanism such as sockets, shared memory, pipes, etc.

紫罗兰の梦幻 2024-09-11 07:11:11

DLL 没有实例,它被加载到主机进程中。在两个应用程序中引用程序集并使用其类/方法。

如果您想避免为两个应用程序部署相同的程序集,您可以 将其放入 GAC

A DLL has no instance, it is loaded in a host process. Reference the assembly in both applications and use its classes/methods.

If you want to avoid deploying the same assembly for both applications you could put it in the GAC.

梦里人 2024-09-11 07:11:11

这是有可能的。您可以将 DLL 安装在 GAC 中(需要强命名程序集),以便两个应用程序都可以轻松访问它。

或者将其粘贴到一个文件夹中,然后让两个应用程序在该文件夹中搜索 dll。

MSDN 支持文章

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="MyAssembly2"  culture="neutral" publicKeyToken="307041694a995978"/>
            <codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>

It's possible. You could install the DLL in the GAC (requires strong named assemblies) in order for both applications to have easy access to it.

Or stick it in a folder and have both apps search that folder for the dll.

MSDN support article

<configuration>
   <runtime>
      <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
         <dependentAssembly>
            <assemblyIdentity name="MyAssembly2"  culture="neutral" publicKeyToken="307041694a995978"/>
            <codeBase version="1.0.1524.23149" href="FILE://C:/Myassemblies/MyAssembly2.dll"/>
         </dependentAssembly>
      </assemblyBinding>
   </runtime>
</configuration>
陌路黄昏 2024-09-11 07:11:11

我不知道这是否可以在 C# 中完成,但在 C++ 中,如果要共享的信息不太复杂,您也可以使用共享内存部分。您只需要使用例如互斥锁来同步对此资源的访问

关于该主题的一篇好文章: http://www.codeproject.com/KB/threads/SharedMemory_IPC_Threads.aspx

玩得开心

I don't know if this can be done in C# but in C++ you can also use Shared Memory sections if the info to share is not too complicated. You would simply need to synchronize access to this ressource using for example a mutex

A good article on the subject : http://www.codeproject.com/KB/threads/SharedMemory_IPC_Threads.aspx

Have fun

天涯沦落人 2024-09-11 07:11:11

如果您的 DLL 创建一个名为 MemoryMappedFile(在内存中或磁盘上),则两个应用程序可以共享 DLL 创建的内存。每个应用程序都有一个指向共享内存的不同指针,但内存实际上是共享的。
您必须对共享内存使用相同的名称,并且就进程之间的线程安全而言,您只能靠自己。 (命名信号量或互斥体可以工作,而 CriticalSection 则不行。)

If your DLL creates a named MemoryMappedFile (in memory or on disk) then the two applications can share the memory created by the DLL. Each application will have a different pointer to the shared memory but the memory will actually be shared.
You have to use the same name for the shared memory and you're on your own as far being thread safe between processes. (Named semaphores or mutexes will work, CriticalSection will not.)

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