简体   繁体   English

C ++如何在应用程序的DLL之间进行通信?

[英]C++ How to communicate between DLLs of an application?

I have a application and two Dlls. 我有一个应用程序和两个Dll。 Both libraries are loaded by the application. 这两个库均由应用程序加载。 I don't have the source of the application, but the source of the libs. 我没有应用程序的源,但是库的源。 I want to instantiate a class in lib A and want to use this instance also in lib B. 我想在库A中实例化一个类,并希望在库B中也使用此实例。

How do I do this? 我该怎么做呢? I'm not sure, but I think that both libs are used in different threads of the application. 我不确定,但是我认为这两个库都在应用程序的不同线程中使用。

I have no idea where I have to search for a solution. 我不知道该在哪里寻找解决方案。

No. Think about DLL as just normal library. 不。可以将DLL视为普通库。 Both can be used in a single thread. 两者都可以在单个线程中使用。
If you want to use a class A in library X, you must pass a pointer/reference to it. 如果要在库X中使用类A,则必须将指针/引用传递给它。 The same applies to library Y. This way both libraries can work with same class/data. 这同样适用于库Y。这样,两个库都可以使用相同的类/数据。

Two dll's loaded into the same process is a fairly simple setup. 将两个dll加载到同一进程中是一个相当简单的设置。 You just need to be careful with module scope, which will be the same as dll scope. 您只需要注意模块范围,该范围与dll范围相同。 eg each dll will have its own set of static instances for any static objects. 例如,对于任何静态对象,每个dll都有自己的一组静态实例。 Next you need to understand how to reference functions/classes across the boundary and what sort of types are safe to use as arguments. 接下来,您需要了解如何跨边界引用函数/类以及可以安全用作参数的类型类型。

Have a look on any documentation for dllexport and dllimport - there are several useful questions on this site if you search with those terms. 查看有关dllexport和dllimport的任何文档-如果使用这些术语进行搜索,则本网站上存在一些有用的问题。

You have to realize that even though your DLLs are used by the host application nothing prevents you (that is your DLLs) from using your DLLs as well. 您必须意识到,即使您的DLL被主机应用程序使用,也无法阻止您(即您的DLL)也使用您的DLL。 So in your DLL A you could load and use your DLL B and call functions and stuff. 因此,您可以在DLL A中加载并使用DLL B并调用函数和内容。 When DLL A is unloaded, free DLL B as well. 卸载DLL A后,也将释放DLLB。 DLLs are reference counted, so your DLL A will have a reference of 1 (the host application) and your DLL B 2 (the host application and DLL A). DLL是按引用计数的,因此您的DLL A的引用为1(主机应用程序),而DLL B的引用为2(主机应用程序和DLL A)。 You will not have two instances of DLL B loaded in the same process. 您不会在同一进程中加载​​两个DLL B实例。

Named pipes might be the solution to your problems. 命名管道可以解决您的问题。

If you're targetting windows, you can check this reference 如果您要定位窗口,则可以查看参考

[EDIT] Didnt see you wanted to work on the same instance. [EDIT]没有看到您要在同一实例上工作。 In that case you need shared memory spaces, however, you really have to know what you're doing as it's quite dangerous :) A better solution could be to apply OO Networking principles to your libs and communicate with, say CORBA, using interprocess middleware or the 127.0.0.1 loopback interface (firewalls will need to allow this). 在那种情况下,您需要共享的内存空间,但是,您真的必须知道您在做什么,因为这样做很危险:)更好的解决方案是将OO Networking原理应用于您的库,并使用进程间中间件与CORBA进行通信或127.0.0.1环回接口(防火墙需要允许这样做)。

Seems the simple solution would be to include in some initialization function of library A (or in DllMain if needed) a simple call to a function in library B passing a pointer to the common object. 似乎简单的解决方案是在库A的某些初始化函数中(或在DllMain中,如果需要的话)包括对库B中的函数的简单调用,并传递指向公共对象的指针。 The only caveat is that you must ensure the object is deleted by the same DLL that newed it to avoid problems with the heap manager. 唯一的警告是,必须确保使用与该对象相同的DLL删除该对象,以避免堆管理器出现问题。

If these DLL's are in fact used in different threads you may have to protect access to said data structure using some kind of mutex. 如果这些DLL实际上是在不同的线程中使用的,则可能必须使用某种互斥锁来保护对所述数据结构的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM