Results 1 to 15 of 15

Thread: [RESOLVED] When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?

  1. #1

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    Resolved [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.

  2. #2
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,671

    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.

  3. #3
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,042

    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.

  4. #4

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    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.

  5. #5
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,598

    Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?

    Quote Originally Posted by dz32 View Post
    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,092

    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 )

  7. #7

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    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.

  8. #8

  9. #9

  10. #10
    Frenzied Member VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    1,294

    Post 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.

  11. #11
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,042

    Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?

    Quote Originally Posted by The trick View Post
    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.

  12. #12
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,092

    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.

  13. #13

    Thread Starter
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,817

    Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?

    Quote Originally Posted by The trick View Post
    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.

  14. #14
    PowerPoster
    Join Date
    Feb 2015
    Posts
    2,671

    Re: When do standard DLLs (non-ActiveX, StdCall DLLs) get loaded/unloaded in VB6?

    Quote Originally Posted by dz32 View Post
    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.

  15. #15
    Frenzied Member
    Join Date
    Jun 2015
    Posts
    1,042

    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
  •  



Click Here to Expand Forum to Full Width