C dll由于对未调用的不存在的函数的引用而未加载

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

本文关键字:不存在 函数 引用 加载 调用 dll      更新时间:2023-10-16

在dll i有以下代码,该代码有条件地调用Windows 8函数IsimmermerSiveProcess()。由于此功能不存在,因此DLL不会在Windows 7上加载。因为实际上没有调用此功能,所以我该怎么做才能强制DLL加载到内存?

#include <Windows.h>
#include <VersionHelpers.h>
...
if ( IsWindows8OrGreater() ) 
  IsImmersive = IsImmersiveProcess ( GetCurrentProcess() );

加载此dll时我从c#遇到的错误是:

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

-

-

谢谢大家。我根据答案的最终代码如下:

BOOL (WINAPI *pIsImmersiveProcess) ( HANDLE hProcess );
byte IsImmersive = 0;
pIsImmersiveProcess = (BOOL (WINAPI *) ( HANDLE hProcess ))
  GetProcAddress ( GetModuleHandle ( L"User32.dll" ), "IsImmersiveProcess" );
if ( pIsImmersiveProcess ) 
  IsImmersive = pIsImmersiveProcess ( GetCurrentProcess() );

就编译器和链接器而言,该函数被称为。

有两种避免这种方法:

  • 在编译时间中排除呼叫,以使编译器知道它没有调用。

  • 避免对该功能的编译时间引用并在运行时获得其地址。

对于第一种方法,您可以根据编译时间已知Windows版本进行专门设计模板。

但是,如果您想要所有受支持的Windows版本的单个DLL,则需要使用第二种方法,本质上是使用GetProcAddress