user118708 user118708 - 1 year ago 79
C++ Question

C++ DLL not loading because of a reference to a non-existent function that is not called

In a DLL I have the following code that conditionally calls the Windows 8 function IsImmersiveProcess(). The DLL does not load on Windows 7 since this function is not present. Because this function is not actually called, what can I do to force the DLL to load into memory ?

#include <Windows.h>
#include <VersionHelpers.h>


if ( IsWindows8OrGreater() )
IsImmersive = IsImmersiveProcess ( GetCurrentProcess() );

The error I get from C# when loading this DLL is:

Unable to load DLL 'd.dll': The specified procedure could not be found. (Exception from HRESULT: 0x8007007F)



Thanks all. My finally code based on the answers is as follows:

BOOL (WINAPI *pIsImmersiveProcess) ( HANDLE hProcess );
byte IsImmersive = 0;

pIsImmersiveProcess = (BOOL (WINAPI *) ( HANDLE hProcess ))
GetProcAddress ( GetModuleHandle ( L"User32.dll" ), "IsImmersiveProcess" );

if ( pIsImmersiveProcess )
IsImmersive = pIsImmersiveProcess ( GetCurrentProcess() );

Answer Source

As far as the compiler and linker is concerned the function is called.

There are two ways to avoid that:

  • Exclude the call at compile time so that the compiler knows it isn't called.

  • Avoid a compile time reference to the function and obtain its address at runtime.

For the first approach, you can specialize a template based on the compile time known Windows version.

But if you want a single DLL for all supported Windows versions, then you need to go with the second approach, essentially using GetProcAddress.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download