-
Shared Data in a DLL
I am working on a dll at the mo which needs to be adapted from the original 16 bit version. Now from what i have read i understand that LibMain would only have been called once, where as dllmain is called for each new process, which gets its own memory address space! So to get round this i added a variable that would toggle once the dll had been entered the first time, and therefore avoid being repeated every new process:
#pragma data_seg("SHARDATA")
static HWND hwndMain = NULL;
static int nLineHeight = 0;
int calledonce = 0;
#pragma data_seg()
calledonce would then be used as follows:
case DLL_PROCESS_ATTACH:
// Initialize once for each new process.
// Return FALSE to fail DLL load.
if (calledonce == 0)
{
hInstance = hModule;
calledonce = 1;
MessageBox(NULL, "incremented", " DLL Main", MB_OK);
}
HOw ever this dosent work....am i setting the shared data segment incorrectly or have o got the wrong end of the stick with regard to the shared data! The libmain version only had hInstance = hModule; in it and this is all i wish to achieve with the new version.
Can anybody tell me why my code won't work?! and wether it will solve the random painting problem with my app!?
the code is here if your interested or need to look at it to solve the prob or shed any light on it.
http://www.uea.ac.uk/~u9945296/Downloads/key32.zip
Cheers in advance
Andy http://www.uea.ac.uk/~u9945296/Downloads/key32.zip
-
I think if you make a global variable in a DLL it's global across all the users of that DLL...not sure though. Check out the documentation for Thread Local Storage and it should come across to something like this :)
-
why do you use a shared data segment?
-
I wonder...
You could make a chat program... that was made up of a client (exe) and the server (dll)... The DLL could be placed on a network share... somewhere...
the Server could use a shared data segment to hold whatever...
-
What do you think about that idea?
-
I'm just trying to mimic the original libmain! in the dllmain, as i only want the vraible hInstance = hModule set once.
-
Amac - it's shared in memory, it doesn't change the original file, so that won't work :)
Andy - I have a nasty feeling that this might not work. I think that DLLs can be mapped anywhere in their calling process's address space, which will be different. When DllMain is called, hDLLInst (or whatever you called it ;)) could be different. It might not be, but it's all dependent on what order the linker gave for the OS to resolve the names in the DLL.