开发者

Managing windows API differences between Windows XP and Vista/Server 2008

I am trying to create a single executable of a simple Win32 application that has to be able to run on both Windows XP and Windows Vista/2008.

Due to some ch开发者_C百科anges in the way Vista works some extra Win32 API calls have to be made to make the program function correctly as it did on XP.

Currently I detect if the application is running on a Windows version newer than XP and call the extra win32 functions as needed. This works fine when running on Vista and Server 2008 but fails when running on Windows XP.

On Windows XP when the program starts I get the error message: The procedure entry point ShutdownBlockReasonCreate could not be located in they dynamic link library USER32.DLL. This happens before any of my code starts executing and none of the code paths when running on XP should call that function.

I would really like to just have one executable that works on both XP and Vista. If possible I dont want to have to have conditional compilation and have two executables.

What is the best way to solve this issue?


You will have to use LoadLibrary() and GetProcAddress() to get the entry point for this function. On XP you'll get a NULL back from GetProcAddress(), good enough to simply skip the call. There's a good example in the SDK docs, the only tricky part is declaring the function pointer:

  typedef BOOL (WINAPI *MYPROC)(HWND, LPCWSTR); 


Since delay load does not work on function level, you'll have to call newer functions by pointer returned from GetProcAddress having Windows version checked.


On Windows XP when the program starts I get the error message: The procedure entry point ShutdownBlockReasonCreate could not be located in they dynamic link library USER32.DLL.

See this comparison table for Xp and Vista Windows APIs. According to the report for user32.dll the ShutdownBlockReasonCreate ( HWND hWnd, LPCWSTR pwszReason ) symbol has been added in Vista (and is absent in XP). I think this table may help you to resolve other portability issues.

Managing windows API differences between Windows XP and Vista/Server 2008


The Win32 SDK has a good example of this in the multimon.h header. Multiple monitor support was added in windows 98/2000 and not supported on 95 or NT 4.

#define COMPILE_MULTIMON_STUBS
#include "multimon.h"

When COMPILE_MULTIMON_STUBS is defined safe passthrough function stubs will be created interogate the OS at the lowest level. This keeps calling code from getting messy with with GetVersionEx calls.


You can also use the GetVersionEx function to determine which version of Windows your program is running on. Then conditionally call functions depending on the OS version. XP has dwMajorVersion = 5 and dwMinorVersion = 1; Vista has dwMajorVersion = 6 and dwMinorVersion = 0.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜