tags:

views:

132

answers:

3

i have an object A which should be notified (A::Notify() method) when some thread starts or dies.
Lets say this thread dynamically loads some DLL file of mine (i can write it).
I believe i should write the dllMain function of this DLL, however i'm not sure how to get a reference to the A object from this function so i can run it's Notify() method.
any ideas?

A: 

Is it okay to make A::Notify() as static method? Otherwise, Singleton method might serve the purpose.

aJ
A: 

So if I understand you write, in your main program you have an instance of class A. When your main program loads certain dlls you want it to call A::Notify for that instance?

As far as I'm aware there is no way to pass an additional argument to LoadLibrary.

If A::Notify can be either static, or A is a singleton, export a "NotifyA" method from the exe, then have the dll call LoadLibrary("yourexe") and you GetProcAddress to get the address of NotifyA which you can then call. (Yes exe files can export methods like dlls!)

A second option is to write your own LoadLibrary, that call a second method after dll main, eg

HMODULE MyLoadLibrary(string dll, A *a)
{
    HMODULE module = LoadLibrary(dll.c_str())
    void (call*)(A*) = void (*)(A*)GetProcAddress(module, "Init");
    call(a);
    return module;
}

The dlls Init method can then store the A instance for later.

Fire Lancer
I think that the A object is not in the DLL, no ?
neuro
+1  A: 

A DLL is loaded once in every process. Once loaded, its DllMain is automatically called whenever a thread is created in the process. Assuming A is a global variable, you can do the following:

  1. After you first load the DLL, call an exported function that will set a global pointer to A in the DLL
  2. Whenever DllMain is called with the reason being thread attached, call A via the pointer you have in the DLL.

Another option would be to start a message loop in your exe, and pass it's thread ID to the DLL. Then, whenever a thread attaches to the DLL send the message loop a message with the details of the created thread. This is a slightly more complicated solution, but it will save you the need of making the DLL familiar with the A class.

eran