How to make a dll re-entrant?
-
G'day, Is there an easy trick to make a dll re-entrant, e.g. each time the dll is called, the caller gets its own data area? The functions in the dll are written in plain C, no classes are involved. Regards, Henk
DLLs can be written without any class, only in C. This is done easily with VC++.
-
G'day, Is there an easy trick to make a dll re-entrant, e.g. each time the dll is called, the caller gets its own data area? The functions in the dll are written in plain C, no classes are involved. Regards, Henk
henk21cm wrote:
each time the dll is called, the caller gets its own data area?
I'm not sure what you mean here. Each process that is linked to DLL gets its own instances of all the DLLs data, except that data that's explicitly shared somehow. Except for special shared data, you manage your data in the DLL just like you do in an EXE. How would each "caller gets its own data area" in an EXE? Automatic/stack-based variables, some kind of passed data struct, etc. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
G'day, Is there an easy trick to make a dll re-entrant, e.g. each time the dll is called, the caller gets its own data area? The functions in the dll are written in plain C, no classes are involved. Regards, Henk
-
you can implement "dllmain" function in your dll. BOOL WINAPI DllMain( HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved ); this function is called when a DLL is attached/detached to a process or thread. You may use this to do what you want.
Thanks folks, A proposed solution is:
karle wrote:
BOOL WINAPI DllMain(
This is -apart from the name of the argument hinstDLL- exactly the same as the intiation code in this dll. Maybe some more detailed description of what is happening. The dll is called by a web application written in Delphi. The main task of the dll is to read a file and to store the data within this file in both a global struct and a pointer to double. Delphi can access and change this data by means of functions in this dll. When several users simultaneously access each of them their own files, the data is overwritten by each consecutive 'read' call, so user 1 sees the data of user 2. Since this is unwanted behaviour, the work around was to add three functions: Islocked(); Lock(); and Unlock(); Before trying to read or access data, the Delphi application checks whether IsLocked(); is false and then Locks the dll. After processing your data, the Delphi application Unlocks the dll. Not pretty, however it works. I was wondering whether there is a more elegant solution? Regards, Henk 21 cm: the universal wavelength of neutral hydrogen
-
G'day, Is there an easy trick to make a dll re-entrant, e.g. each time the dll is called, the caller gets its own data area? The functions in the dll are written in plain C, no classes are involved. Regards, Henk
My understanding is that DLLs are reentrant. That's sort of the idea behind a DLL in the first place. The thread that makes a call into the DLL has its own stack, and since data is not shared between thread stacks, each call has its own data area. However, if you have, for example, a memory mapped file, a named pipe, a resource handle to say a GDI brush, then you have a resource that's shared by the OS across threads. This has nothing to do with the DLL, but accessing that resource inside of the DLL can cause threading issues to arise, since the various callers of the DLL run asynchronously. For example, suppose your DLL retrieves a handle to brush or writes, reads, then deletes the contents of a memory mapped file. One thread calls into the DLL and, before it exits, is swapped out by the task manager. That thread's stack is set aside and its place of execution within the DLL is marked, then the new thread begins execution. Say the first thread wrote data to the file, but did not read or delete its data. The new thread comes in and writes its data, but when it reads the data back, it gets its data and the data from the previous thread. Then the second thread deletes its data and the data placed there from the previous thread. When the original thread is swapped back in, there is no data there to read back anymore. A DLL is just a function that is made available to call by any executable (more generally, thread) on the OS. Disclaimer: I'm not a software engineer, but I play one at my office.
Without darkness, there are no dreams. -Karla Kuban