Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29257 Discussions

Code behaviors in a DLL .... getting a better understanding

carlls
Beginner
647 Views

Greetings:

Playing with DLLs and noticing behaviors that make sense but seeking confirmation.

I amplugging into a 3'rd party application called Unigraphics so I have little control over the execution environment.I am assuming that they load and explicitly unload my DLL each time the user chooses to perform my task.


1) "Global" variables defined in the DLL get re-initialized upon each loading of the DLL. So if I want any "static" variable to exist between multiple execution of my dll (with in the same PID)I better write them to an environment variable or file. (my choice is env variable as it is specific to the PID and is cleaned up automatically)

2) Every DLL at load time gets it's own copy of "atexit" routines. ( reference http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/2036168e-0f5e-49a7-92d4-24626c80f148)

So how do I load up the "atexit" routines when my dll is loaded? Is there an "atentry" routine that get called when a a dll is loaded?

3) While global variables get re-initialized, file pointers do not. So let's say the DLL opens a file but does not close it before it is unloaded. Can I assume thefile handles scope is at the process level and not the dll level. So any checks on file closure should be done in the dlls "atexit" routine.

Any comments or pointers to more info would be greatly appreciated.

Regards
Carl

0 Kudos
2 Replies
GVautier
New Contributor III
647 Views
Hello

The DllMain function is designed for simple initialization or termination tasks. See :

http://msdn.microsoft.com/en-us/library/ms682583%28v=VS.85%29.aspx
0 Kudos
carlls
Beginner
647 Views
Finding out a bit more ..... from http://msdn.microsoft.com/en-us/library/d1587c1h(v=VS.80).aspx

  • An application can have multiple instances of itself running in the system simultaneously, whereas a DLL can have only one instance.

  • An application can own things such as a stack, global memory, file handles, and a message queue, but a DLL cannot.

    So a file that is opened in a DLL and not closed by DLL exit is "leaked", and subsequent use of the file will be problematic until the process ends.

  • 0 Kudos
    Reply