Wrapper Classes for Dynamic DLL Loading

I needed to develop a program which requires a DLL that is usually found in the Windows system directory. Sometimes this DLL is not installed at all. To make things worse, the utility that needs the call the DLL is not allowed to leave any footprint when removed, so installing in the system directory or changing the registry is not an option. However, it is required to use the DLL from the system directory whenever it is available.

The installation utility doesn't allow the customization of the installed files based on the OS. In some cases, however, a different implementation is needed for different operating systems. A possible solution is to put different DLLs in different subdirectories, then first try to access the system directory. If the DLL is not there, then go to the subdirectory specific for the operating system.

Additionally, I don't believe people love to write all these LoadLibrary and GetProcAddress statements, even if the delayed DLL loading linker option can help here in the simpler cases.

To handle the requirements, I wrote a set of classes and templates that wrap LoadLibrary and GetProcAddress and allow to specify multiple directories to search for the DLL and the order to look in them. The most interesting part of the solution are the templates, which may look like this:

The constructor initializes the base class with the DLL name and function name from the template instance. The function operator first calls the base class to locate and load dynamically the DLL and put the function address into m_pf, then casts this pointer to the actual type and returns with the return type and value of the function. The class LoadProcWrapperBase handles all path and module manipulations.

The above sample is simplified to illustrate the main idea. If you look at the attached sources, there are two intermediate layers of templates there one that provides the constructor, and another that manages default return values that can be used if the function is not located in the DLL.

Two sets of macros expand that the templates are included for zero to four parameters. One set is used for functions that return values, the other is for functions returning void or when return value is not necessary. I am not sure if casting void return type on functions that return a value is 100% safe, so I would recommend to use the macros with no return value really only for exported functions returning void. The macros must be expanded at global level, because templates can only take constant pointers to objects that have external linkage type.

Downloads

Comments

Short blog post uncovers the unquestionable details on gucci as well as how it can certainly harm your organization.

Posted by emeseesip
on 05/06/2013 03:13pm

Essentially The Most Complete adidas Guide You Ever Seen Or else Your Money Back [url=http://www.guccija.biz/]gucci è²¡å¸[/url] Wow, remarkable product. People must consider nike immediately whilst it is still in stock ! ! [url=http://www.guccija.biz/]ã°ãããã­ã¼ã±ã¼ã¹[/url] nike assists everybody by simply adding quite a few distinctive features and options. This is a unvaluable item for any supporter of nike. [url=http://www.guccija.biz/]ã°ãã ã·ã§ã«ãã¼ããã°[/url] Impartial guide shows you Five fresh, new things on nike that no-one is speaking about. [url=http://www.chanelja.biz/]ã·ã£ãã« è²¡å¸[/url] The main reason why not a soul is speaking about adidas and things one should engage in this afternoon. [url=http://www.chanelja.biz/]ã·ã£ãã« é·è²¡å¸[/url] Recent queries about adidas replied and consequently reasons why you should definitely look at every word on this article. [url=http://www.chanelja.biz/]chanel ããã°[/url] Fundamental principles of the nike that you could make use of starting off today.[url=http://www.nikeja.biz/]ãã¤ã­[/url] Methods to discover every part there is to understand concerning gucci in three basic steps.

Top White Papers and Webcasts

Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT
The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility.
Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …