views:

737

answers:

3

On my Windows 7 system, the GetVersionEx Windows API function returns "6.0", indicating Windows Vista, when it should return "6.1".

If it matters, I used the following Delphi code:

function winver: string;
var
  ver: TOSVersionInfo;
begin
  ver.dwOSVersionInfoSize := SizeOf(ver);
  if GetVersionEx(ver) then
    with ver do
      result := IntToStr(dwMajorVersion) + '.' + IntToStr(dwMinorVersion) + '.' + IntToStr(dwBuildNumber) + ' (' + szCSDVersion + ')';
end;

and the string "6.0.6002 (Service Pack 2)" was returned.

Isn't this highly odd?

+5  A: 

Is your executable running with any compatibility settings defined (I assume this might be the case for legacy Delphi applications)? The documentation of GetVersionEx states:

If compatibility mode is in effect, the GetVersionEx function reports the operating system as it identifies itself, which may not be the operating system that is installed. For example, if compatibility mode is in effect, GetVersionEx reports the operating system that is selected for application compatibility.

Maybe GetProductInfo can do what you want?

0xA3
Delphi 2009 is not really old...
Andreas Rejbrand
Also you need to remember that if you are creating child process that have set comatiblity with for example Vista SP2, then that child process will inherit that behaviour too. Example: you are using TotalCommander with compatibility VistaSP2, then each started process by TC will also use VistaSP2 compatibility.
kibab
I now found that GetVersionEx returns Vista when my application runs through the Delphi 2009 debugger, but Windows 7 when the application is executed alone.
Andreas Rejbrand
@Andreas, IMO this belongs as an answer and not a comment. i.e. you found the true answer, so you should post it as such. 0xA3 had a good answer, but I think yours is THE answer.
Chris Thornton
@Chris Thornton: You are probably right, and I'll do so (but I cannot accept my own answer sooner than in two days).
Andreas Rejbrand
+6  A: 

I now found that GetVersionEx returns Vista when my application runs through the Delphi 2009 debugger, but Windows 7 when the application is executed alone. I also found that RAD Studio (the Delphi IDE) actually runs in compatibility mode for Windows Vista SP2. Hence everything makes sense, for, as pointed out by kibab, a child process will "inherit" the compatibility settings of its parent process.

Andreas Rejbrand
+1  A: 

I think it may just be you. i.e. your D2009 may have been marked by windows, as needing to run in compatibility mode. I made a test app with your function, and compiled and ran both with D2009 and D2010, inside the debugger and externally (click the exe in windows explorer), and for all 4 cases, it came back with: 6.1.7600 ()

Running on Windows7, 32-bit.

Chris Thornton