C++ Why loading Windows DLL at runtime?

Jut the “tool”. Not C++.

Update 2019 December

Version 2.0.0.

No dependancy on the logger.  See the test.cpp on how to install your own logger.

Please see the readme in the Github project.

Update 2018 December

In case you need it, here is one very relevant use case, for proving the dynamic dll loading usability. Windows dll’s versions.

If you need GetGeoInfoEx (for example) you are faced with a very tricky issue, impossible to be solved just by using WIN32  compile-time macros. The issue is, the existence of this function is not tied to the particular version of Windows, but to a  particular release of Windows 10. Namely, Redstone 3.  GetGeoInfoEx exist in kernel32.dll but only if run-time is Windows 10 Redstone 3 or better. Macros are for compile-time, and this is a run-time issue.

To explain the complexity better, you will be able to compile and link your code that calls this function. But that will fail at run-time. Because the system is not checking before run-time if a particular function exists in a particular dll.

Using WIn32 versionhelpers.h, in this case, will not help. You need a more fine-grained check than that. Yes, you can use WIN32 API to develop a more fine-grained runtime check function, but you might soon realize it is far from straightforward.

I might suggest, to be in control of this situation you can use my dll dynamic loader-user as described below. Here is the snippet showing you how:

First, declare the function pointer required

Now one function call, to get to the pointer to the instance of this function inside  kernel32.dll

Required function pointer type, the dll name, function name and that is it. And here is the secret sauce:

If function is not in the dll, returned value will be nullptr.

Next we have the fp which we use as any other function. With this crucial run time check included.

If your exe is running on W7 or something older or W10 bellow REDSTONE 3 the fp will be null, and you will code easily around that. To replace that if(fp)  line with something else, an amount of WIN32 sorcery is required, to find out do we use the right Windows and the right build of it.

Without this mechanism, this code will be much more involved and complex. As mentioned below the github project is here.

The Original Article

And more importantly, why would anybody do it?

This article contains a Windows programming code and a C++ code of moderate to high complexity.

Why loading dynamically a dll and calling a function from it?

Among other things, because you do not need a header file. You do not need to find it, use it and potentially “fight” with issues some other completely unrelated stuff in the same header, might do to your project.

That is a simple use case. And here are more reasons you will want DLL dynamic loading.

You have written some snazzy C++ and you are very proud of it. Being C++ it might be some deep system “mumbo-jumbo” that you have been amazed about. And every good deep system “mumbo-jumbo”  does not require Graphical User Interface (aka GUI). After all, you are a system programmer and your code runs on servers! But it does require some way of signaling to its human administrator’s servants, and you have decided you want it to release sounds! Like squeaking when not well, that sort of a thing.

OK. Of to yet another masterful dollop of (server-side) C++. It is kernel32.dll and yes, there is a WIN32 function called Beep() in there.  Nothing easier than using it. Bam! Done!

This is where I stop you, my dear C++ sorcerer.

The efficiency of your application, is directly proportional to its memory usage

Think. Your C++ code produces a very small and fast and perhaps useful and invisible Windows program. The last thing you want to do it is to start including DLL’s in its build. Why?

Including DLL’s in a build means your program will need to make sure (at startup time) each of these DLL’s is found and then loaded into memory. And each of them, like kernel32.dll, will do the same for itself with a myriad of other DLL’s it depends on. And so on with those other dll’s. This is called “DLL Dependency Tree”

All of a sudden the file size and the memory footprint of your little, beep emanating C++ wonder, has grown. A lot. And you wanted “just” to call the Beep(), and very rarely at that.

This is why a concept of dynamically loading (Windows) dll’s has been invented. This is the sequence of activities it is made of:

  1. Load a dll
  2. Ask it (nicely) for a function you need
  3. Call a function
  4. Unload the library. (This is a crucial step)

By now I am sure, you might have lost all the patience and are about to leave, and copy-paste next C++ code from somewhere.  This is why I have made this little DBJDBJ tool, to do the dynamic loading of Windows dll’s.

The usage

Basically, there is one function that does it all. For it to be used you need three things

  1. the dll name
  2. the function name
  3. the function signature
 

dll will be properly unloaded when an instance of dll_load goes out of scope. For the fancy test unit please look into test.cpp.

To start the dll dynamic loading, one needs to know where is it, and give the full path to the dll location.

For understanding the “finding rules” please read the article here. Note: we are using LoadLibrary() and not the LoadLibraryEx(). Why? Primarily because we want to enforce the usage policy titled: “Keep it simple”.

We obviously could provide more functionality and make everything more complex. I suggest that a small percentage of readers needing the extended functionality, take this code and extend it.

The full working code is on the Github here. I am sure you know what is testing code and what is a library code in there. Hint: The lib is all in one header. It is made with 2017 Community Edition of Visual Studio. It uses standard C++17.

Any questions, or bugs, fire away and ask. Please use the issues tab, on this GitHub repo.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.