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

1

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 final 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() );
c++
dll
asked on Stack Overflow Nov 29, 2016 by user118708 • edited Nov 30, 2016 by user118708

1 Answer

2

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.


User contributions licensed under CC BY-SA 3.0