How do you call GetVersionEx (Windows)?

Yao G. nospamyao at gmail.com
Sun Aug 22 13:03:29 PDT 2010


On Sun, 22 Aug 2010 14:36:50 -0500, Andrej Mitrovic  
<andrej.mitrovich at gmail.com> wrote:

> There's an example of a module constructor in TDPL. I don't think it's  
> supossed to compile as it is, but I wanted to try it out anyway.
>
> I need a way to call the windows c function GetVersionEx. A grep through  
> the source files doesn't find it (except in the DMD cpp backend which it  
> internally uses), but there's one GetVersion function in  
> core\sys\windows\windows.d. That seems to work and returns a uint  
> representation, but then I have to do bitmasking to get all the info out  
> of it. Yikes!
>
> The TDPL example does actually use GetVersionEx() and calls it with a  
> "OSVERSIONINFOEX" structure argument, and it's all nicely documented on  
> MSDN. But I've no idea if I can call it from D. Anyone doing Windows  
> programming in D? :)

This works on my XP machine:

---------------------------------------
module example;


import std.stdio;
import core.sys.windows.windows;


extern(Windows)
{
     __gshared:

     BOOL GetVersionExW( LPOSVERSIONINFO lpVersionInfo );

     // Change to GetVersionExA if you want the UTF-8 version
     alias GetVersionExW GetVersionEx;

     // Change to char if you want the UTF-8 version
     alias wchar TCHAR;

     struct OSVERSIONINFOEX
     {
         DWORD dwOSVersionInfoSize;
         DWORD dwMajorVersion;
         DWORD dwMinorVersion;
         DWORD dwBuildNumber;
         DWORD dwPlatformId;
         TCHAR szCSDVersion[128];
         WORD wServicePackMajor;
         WORD wServicePackMinor;
         WORD wSuiteMask;
         BYTE wProductType;
         BYTE wReserved;
     }
     alias OSVERSIONINFOEX* LPOSVERSIONINFO;
}

void main( string[] args )
{
     OSVERSIONINFOEX info;
     info.dwOSVersionInfoSize = OSVERSIONINFOEX.sizeof;

     if( GetVersionEx(&info) != 0 )
     {
         if( info.dwMajorVersion == 5 )
         {
             switch( info.dwMinorVersion )
             {
             case 0:
                 writeln( "Windows 2000" );
                 break;

             case 1:
                 writeln( "Windows XP" );
                 break;

             case 2:
                 writeln( "Windows Server 2003" );
                 break;

             default:
                 writeln( "I dunno lol" );
             }
         }
         else
         {
             writeln( "Windows Vista or 7" );
         }
     }
}
---------------------------------------

I have several modified lib files with more functions defined. The ones  
that come bundled with DMD are severely outdated.

If you have some linking problems, try to use the aliases that I suggested  
on the code comments.


-- 
Yao G.


More information about the Digitalmars-d-learn mailing list