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

DLL memory initialization/release

tarvestad
Beginner
782 Views
A 40,000 line legacy Fortran application is built as a DLL to be called from Visual Basic.

I suspect a particular runtime error is caused the by code's failure to properly initialize some local variable inside the DLL. The first time the DLL is executed, everything is fine. But if the DLL is hit a second time during one execution of the VB appplication, problems occur.

Is there a VB-callable function to release the Fortran DLL? Any suggestions are greatly appreciated.

I am speculating that such a function would resolve my problem, since the variables would be reinitialized on re-load.

0 Kudos
3 Replies
Steven_L_Intel1
Employee
782 Views
Such a routine exists - the Win32 API routine FreeLibrary. To use it, you need to get the "handle" of the loaded DLL. VB won't give it to you, but you can get it by calling GetModuleHandle with the DLL name. The catch may be that VB will go off the deep end if you try this because it will think that the DLL is already loaded and will get confused the next time you call the DLL. Try it and see....

Steve
0 Kudos
Jugoslav_Dujic
Valued Contributor II
782 Views
...of course, that's a cluge -- it's still better to fix the offending code. Try compiling with "Variables default to automatic" switch -- that will probably cause havoc to code's behaviour since you'll (probably) get garbage values for all non-unitialized... the rest is pure debugging ;-).

Jugoslav
0 Kudos
gfthomas8
Novice
782 Views
Supply a DllMain for your Fortran DLL and see if the fwdReason parameter returns DLL_ATTACH_NOTIFICATION the second time you hit the DLL. My guess I that it doesn't so the DLL isn't being reinitialized from one hit to the next. The original Fortran code was undoubtedly written to run in batch mode and its creators are to be forgiven for not anticipating that one day someone would compile it as a DLL and run serial or threaded instances of it without ever giving it a break. If you load the DLL in VB via ModuleID = LoadLibrary("MyCVF.DLL"), use it, and then unload it via FreeLibrary(ModuleID), and repeat all of this for subsequent hits, your DLL will be reinitialized from hit to hit and DllMain should fire a DLL_ATTACH_NOTIFICATION each time you hit your initialized DLL.
0 Kudos
Reply