-
Feb 2nd, 2023, 10:46 AM
#1
[RESOLVED] When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
The subject says it all, but I'll repeat it:
When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
I've never really researched this in detail. I've sort of always assumed that the entire DLL library gets loaded when any of its API entry points are called, and that it never gets unloaded until the project is terminated. But I'm not positive about that.
I also suspect that the StdCall doesn't make any difference, and that the same logic holds for non-AX libraries with CDECL calls.
I guess I should also say that we're not explicitly loading or unloading these libraries. We're just declaring API calls, and then using those when we need.
Last edited by Elroy; Feb 2nd, 2023 at 02:05 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 2nd, 2023, 12:01 PM
#2
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
If you use the late-bound call (using the Declare statement) the dll is loaded when the first call occurs. Those Dlls are unloaded when all the project contexts are destroyed. For example if you have an AxDll with object instances in 3 STA the dll will be unloaded when all 3 instances will be destroyed and project will be ready to unloading.
If you use the early-bound call (using a typelib) the dll is loaded when executable is initialized. Those Dlls are unloaded when the executable is unloaded.
-
Feb 2nd, 2023, 12:05 PM
#3
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Declares are loaded on demand first time one of thier api are called the module handle is then cached. They do not get unloaded unless you explicitly call freelibrary
When working in the ide, debug sessions run within the ide process itself so standard dlls will get loaded and then be stuck in memory until ide is closed. When developing stddlls that need recompiled over and over I manually call load library in form load and freelibrary in unload to help avoid having to close the ide to recompile the dll again.
-
Feb 2nd, 2023, 12:15 PM
#4
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Ahh, I didn't think about TypeLib declarations. It's good to know that they're just loaded when the EXE is loaded, and stay loaded.
It sounds like my assumption was correct about non-TypeLib, non-AX DLLs (those using Declare ... in our code):
...the entire DLL library gets loaded when any of its API entry points are called, and that it never gets unloaded until the project is terminated.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 2nd, 2023, 01:16 PM
#5
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Originally Posted by dz32
When working in the ide, debug sessions run within the ide process itself so standard dlls will get loaded and then be stuck in memory until ide is closed.
Well that explains it. A lifetime ago when I was first learning how to write standard DLLs in C++ for use in VB6, I was unable to re-build the DLL in C++ after testing it in VB6 because the DLL was always locked and could not be overridden by the C++ compiler. I had to close the VB6 IDE completely then re-compile the C++ DLL. I used to wonder why that was.
-
Feb 2nd, 2023, 01:18 PM
#6
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
The previous thread was about Ax DLLs being unloaded by COM. Standard DLLs get unloaded on matching each LoadLibrary by FreeLibrary. Who calls these is a different story - it might be VB runtime (declares) or user code calling these manually.
Imported DLLs are practically never unloaded i.e. when the executable is unloaded by the OS it must long ago been terminated so you don’t get a chance of noticing that any of the DLLs is missing )
-
Feb 2nd, 2023, 02:08 PM
#7
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Ok, this all begs another question (or a couple).
I've never made it a habit to use LoadLibrary & FreeLibrary, and have only used them on rare occasions. If we're really concerned about memory, I suppose it would be a good habit to use those, maybe just load all our DLLs when we start execution, and free them all when we're getting ready to terminate the project's execution.
I thought about just doing the FreeLibrary, but then we wouldn't have hLibModule to pass to FreeLibrary. So, a second question ... is there a way to get the hLibModule handles for DLLs that are just automatically loaded?
And, I guess we can't explicitly free the Kernel32.dll library, because that's where the FreeLibrary call is. I doubt this is like a COM object that can free itself because it's got an internal reference that's not freed until all code execution in that COM is completed.
Last edited by Elroy; Feb 2nd, 2023 at 02:11 PM.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 2nd, 2023, 04:07 PM
#8
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Originally Posted by dz32
When working in the ide, debug sessions run within the ide process itself so standard dlls will get loaded and then be stuck in memory until ide is closed. .
No. When IDE resets the project (stop button or end execution) it unloads dlls (called with Declare statement) as well as in an executable. DLLs declared in TLB are unloaded when you close the project.
-
Feb 2nd, 2023, 04:10 PM
#9
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Originally Posted by Elroy
I thought about just doing the FreeLibrary, but then we wouldn't have hLibModule to pass to FreeLibrary. So, a second question ... is there a way to get the hLibModule handles for DLLs that are just automatically loaded?
You could use GetModuleHandle to get the handle but you shouldn't.
-
Feb 2nd, 2023, 04:35 PM
#10
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
I've been using "LoadLibrary" to load the "riched20.dll" for my RichEdit Unicode Textbox project. I am loading the DLL in the "Class_Initialize" event but then I thought it doesn't make sense to load it every time a new class is created and sure enough that is indeed the case. So, just as "The trick" mentioned, "GetModuleHandle" does the job perfectly:
Code:
If GetModuleHandle(RICHEDIT_DLL) = 0 Then LoadLibrary RICHEDIT_DLL ' Load the library only once
It never crossed my mind to call "FreeLibrary" at any point because that is done automatically when the program ends.
-
Feb 2nd, 2023, 04:42 PM
#11
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Originally Posted by The trick
No. When IDE resets the project (stop button or end execution) it unloads dlls (called with Declare statement) as well as in an executable. DLLs declared in TLB are unloaded when you close the project.
I had constant problems when recompiling c dlls with the ide hanging onto them when used only through declare. This was resolved almost completely if I did my own load library before and freelibrary on close.
The manual load library was also convient if the dll lived in a different path for dev vrs distribution ensuring it was always found. Also it lets me test early if the dll will load properly on system such as if secondary dependency missing.
I never specifically watched ide generated freelibrary calls but it was not releasing them properly in ide anyway ¯\_(ツ)_/¯
The only time the technique fails me now is if I hit end and the my freelibrary does not execute
Last edited by dz32; Feb 2nd, 2023 at 05:07 PM.
-
Feb 2nd, 2023, 04:55 PM
#12
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Yes, “pre-loading” API declared dlls is very neat esp. with custom path dlls are distributed under.
-
Feb 2nd, 2023, 05:27 PM
#13
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Originally Posted by The trick
When IDE resets the project (stop button or end execution) it unloads dlls (called with Declare statement) as well as in an executable.
Well, I'm glad to have how it works nailed down, but The Trick's insight really obviates any need to worry about it. The last time I actually compiled a non-AX DLL, it was a piece of Fortran code I needed, and I didn't have any problems with that. And I've long since rewritten that Fortran code into VB6. The only non-AX DLLs I use are standard DLLs distributed by Microsoft with Windows.
Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.
-
Feb 3rd, 2023, 05:07 AM
#14
Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?
Originally Posted by dz32
I had constant problems when recompiling c dlls with the ide hanging onto them when used only through declare. This was resolved almost completely if I did my own load library before and freelibrary on close.
So if you load a library before and unload on close - it doesn't affect to the locking status when you have a declared (with Declare statement) function. When you manually call LoadLibrary it loads the library. The code then calls a declared function from the dll. The runtime calls DllFunctionCall function which calls LoadLibrary (the library counter now equals 2 (in our case)). Then you call FreeLibrary which decrement the library counter but doesn't unload lib because counter greater than 0. When you end debugging in the active project the runtime releases all the Declared libraries by calling FreeLibrary which also decrements the counter and library unloaded now. So your LoadLibrary/FreeLibrary pair isn't affect to locking dll behavior. Moreover if you stop your code before you call FreeLibrary explicitly you get the locking library.
Note im telling about behavior when runtime resets the project. For example if you have an opened form with an active usercontrol the project isn't reset.
-
Feb 3rd, 2023, 07:06 AM
#15
Re: [RESOLVED] When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded
¯\_(ツ)_/¯ All I know is that I suffered with it and that fixed it. I have definitely seen it misbehave. Next time I run into it I will do a deeper analysis and post back.
Last edited by dz32; Feb 3rd, 2023 at 07:29 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|