views:

374

answers:

5

Hey,

I have a application and two Dlls. 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.

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.

+1  A: 

No. Think about DLL as just normal library. 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. The same applies to library Y. This way both libraries can work with same class/data.

Andrejs Cainikovs
But how do I pass this pointer?
plucked
As a function argument?
Andrejs Cainikovs
But where? I have only the source of the libraies, not of the app where the initialization is done.
plucked
I'm sorry, but now you are trying to ask a specific question about the library which I had never see and possibly will not ever know about. Check the docs.
Andrejs Cainikovs
Yes, sorry, the libraries are not written yet. But they need to communicate. (o:
plucked
In this case you just need to write both libraries in the way that both will be able to handle the same data structure/class/whatever.
Andrejs Cainikovs
A: 

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. 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).

cwap
The problem is that I have to share a lot of data in a realtime rendering application and don't want to slow down this app with jobs like "pack the data" ... "extract the data" and so on. Shared memory looks good, I have to read about it. (o:
plucked
These all sound like cross process solutions - crossing a dll boundary in process does not require this level of complication.
morechilli
The original questioner said that he didn't have control over the hosting application. In that case, the two DLLs will have to do all the coordinating themselves. Named pipes or shared memory sound like a pretty good ideas, even if they are not typically used for in-process communication.
dangph
+1  A: 

Two dll's loaded into the same process is a fairly simple setup. You just need to be careful with module scope, which will be the same as dll scope. e.g. each dll will have its own set of static instances for any static objects. 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.

morechilli
A: 

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. 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.

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.

Elemental
A: 

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. So in your DLL A you could load and use your DLL B and call functions and stuff. When DLL A is unloaded, free DLL B as well. 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). You will not have two instances of DLL B loaded in the same process.

Fozi
I still have the problem. I set up an example solution with Visual Studio 2008. There is a host application which loads two dlls (plugin and proxy). I want the from the host application instance of proxy in the plugin class without changing code in the host application. vs156212.vserver.de/DLLCommunication.zip
plucked
OIC. You will have to modify the constructor in the proxy class to call a function in the plugin DLL and pass it's this to it. You have to be careful though: 1. You are passing a pointer to an incomplete pass. Don't do anything with it in that function. 2. The class was created and is handled by the host application. This means that you can't delete it (because it's in someone else's heap) and it could be deleted by the host application at any time (change the destructor as well). Make it thread save.
Fozi
Oops: s/pass/class/
Fozi