views:

189

answers:

4

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 changes 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?

+3  A: 

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.

GSerg
+5  A: 

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); 
Hans Passant
A: 

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.

Vanessa MacDougal
A: 

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.

dkackman