如何基于windows版本

问题描述:

MSDN的备注部分正确加载GetMappedFileName,描述here,特别提到有负载类型的以下功能之间的差异。如何基于windows版本

由于我模块的便携式和负载模型动态,我不能/能够使用任何预处理器指令:

#if (PSAPI_VERSION == 2) 
      (GetProcAddress("kernel32.dll", OBFUSCATE(L"K32GetMappedFileNameW"))); 
#elif (PSAPI_VERSION == 1) 
      (GetProcAddress("psapi.dll", OBFUSCATE(L"GetMappedFileNameW"))); 
#endif 

另外 -

的Kernel32.dll在Windows 7和Windows Server 2008 R2; Windows 7和Windows Server 2008 R2上的Psapi.dll(如果 PSAPI_VERSION = 1); 的Windows Server 2008,Windows Vista中,在Windows Server 2003和Windows XP

上的Psapi.dll不让它变得如何windows版本正好与PSAPI版本协调清晰。

+1

下一页:“必须在较早版本的Windows以及Windows 7和更高版本上运行的程序应始终将此函数作为GetMappedFileName调用。” – 2015-03-31 13:22:01

GetMappedFileName() documentation特别说:

与Windows 7的启动和Windows Server 2008 R2,Psapi.h建立版本号为PSAPI功能。 的PSAPI版本号会影响用于调用功能和程序必须加载库的名称。

如果PSAPI_VERSION为2或更大,则此函数在Psapi.h中定义为K32GetMappedFileName,并在Kernel32.lib和Kernel32.dll中导出。如果PSAPI_VERSION是1,则该函数被定义为在Psapi.h GetMappedFileName和在Psapi.lib和的Psapi.dll导出为调用K32GetMappedFileName的包装。

必须在早期版本的Windows以及Windows 7和更高版本上运行的程序应始终将此函数作为GetMappedFileName调用。为确保符号正确解析,请将Psapi.lib添加到TARGETLIBS宏,并使用-DPSAPI_VERSION = 1编译程序。要使用运行时动态链接,请加载Psapi.dll。

如果静态链接是不适合你的选择,你需要动态地加载在运行时的功能,而无需使用#ifdef语句,然后简单地检查这两个DLL文件无条件,如:

typedef DWORD WINAPI (*LPFN_GetMappedFileNameW)(HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename, DWORD nSize); 

HINSTANCE hPsapi = NULL; 
LPFN_GetMappedFileNameW lpGetMappedFileNameW = NULL; 

... 

lpGetMappedFileNameW = (LPFN_GetMappedFileNameW) GetProcAddress(GetModuleHandle("kernel32.dll"), L"K32GetMappedFileNameW")); 
if (lpGetMappedFileNameW == NULL) 
{ 
    hPsapi = LoadLibraryW(L"psapi.dll"); 
    lpGetMappedFileNameW = (LPFN_GetMappedFileNameW) GetProcAddress(hPsapi, L"GetMappedFileNameW"); 
} 

// use lpGetMappedFileNameW() as needed ... 

if (hPsapi) 
    FreeLibrary(hPsapi); 

或者,只要按照文档所述 - 完全忽略kernel32,并在所有Windows版本上单独使用psapi.dll。在Windows 7和更高版本中,psapi.GetMappedFileNameW()kernel32.K32GetMappedFileNameW()的包装。

typedef DWORD WINAPI (*LPFN_GetMappedFileNameW)(HANDLE hProcess, LPVOID lpv, LPWSTR lpFilename, DWORD nSize); 

HINSTANCE hPsapi = NULL; 
LPFN_GetMappedFileNameW lpGetMappedFileNameW = NULL; 

... 

hPsapi = LoadLibraryW(L"psapi.dll"); 
lpGetMappedFileNameW = (LPFN_GetMappedFileNameW) GetProcAddress(hPsapi, L"GetMappedFileNameW"); 

// use lpGetMappedFileNameW() as needed ... 

FreeLibrary(hPsapi);