c++ - How to load GetMappedFileName correctly based on windows version -


msdn's remarks section, described here, mentions there difference between loading types of following function.

since module portable , loads models dynamically, i'm not allowed / able use pre-processors commands:

#if (psapi_version == 2)             (getprocaddress("kernel32.dll", obfuscate(l"k32getmappedfilenamew"))); #elif (psapi_version == 1)             (getprocaddress("psapi.dll", obfuscate(l"getmappedfilenamew"))); #endif 

in addition -

kernel32.dll on windows 7 , windows server 2008 r2; psapi.dll (if psapi_version=1) on windows 7 , windows server 2008 r2; psapi.dll on windows server 2008, windows vista, windows server 2003, , windows xp

doesn't make clearer of how windows version coordinated psapi version.

the getmappedfilename() documentation says:

starting windows 7 , windows server 2008 r2, psapi.h establishes version numbers psapi functions. the psapi version number affects name used call function , library program must load.

if psapi_version 2 or greater, function defined k32getmappedfilename in psapi.h , exported in kernel32.lib , kernel32.dll. if psapi_version 1, function defined getmappedfilename in psapi.h , exported in psapi.lib , psapi.dll wrapper calls k32getmappedfilename.

programs must run on earlier versions of windows windows 7 , later versions should call function getmappedfilename. ensure correct resolution of symbols, add psapi.lib targetlibs macro , compile program -dpsapi_version=1. use run-time dynamic linking, load psapi.dll.

if static linking not option you, , need dynamically load function @ runtime without using #ifdef statements, check both dlls unconditionally, eg:

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() needed ...  if (hpsapi)     freelibrary(hpsapi); 

or, documentation says - ignore kernel32 altogether , use psapi.dll on windows versions. on windows 7 , later, psapi.getmappedfilenamew() wrapper 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() needed ...  freelibrary(hpsapi); 

Comments

Popular posts from this blog

angularjs - Showing an empty as first option in select tag -

c# - Inconsistent accessibility errors -

security - Java Applet Windows-MY keystore PrivateKey getEncoded is null -