Skip to content
The Road to Delphi — Delphi, VCL, VCL Styles, UxTheme and WinAPI

Determine when Windows is a Desktop or Server edition using Delphi

,

Sometimes we need determine when a windows version is a server edition (2000, 2003 or 2008) or a Desktop edition. to do this we can use the VerifyVersionInfo function. this function compares a set of operating system version requirements to the corresponding values for the currently running version of the system.

but exist a problem, the version of this function declared in the Windows unit uses a OSVERSIONINFO parameter.

[sourcecode language=”delphi” wraplines=”false”]
function VerifyVersionInfo(var lpVersionInformation: TOSVersionInfo;
dwTypeMask: DWORD; dwlConditionMask: DWORDLONG): BOOL; stdcall;
[/sourcecode]

And in this case we need pass a OSVERSIONINFOEX Structure, so we must re-declare this function in this way.

[sourcecode language=”delphi” wraplines=”false”]
{$IFDEF UNICODE}
function VerifyVersionInfo(var LPOSVERSIONINFOEX : OSVERSIONINFOEX;dwTypeMask: DWORD;dwlConditionMask: int64): BOOL; stdcall; external kernel32 name ‘VerifyVersionInfoW’;
{$ELSE}
function VerifyVersionInfo(var LPOSVERSIONINFOEX : OSVERSIONINFOEX;dwTypeMask: DWORD;dwlConditionMask: int64): BOOL; stdcall; external kernel32 name ‘VerifyVersionInfoA’;
{$ENDIF}
[/sourcecode]

Now to work with the VerifyVersionInfo function we need establish the requirements to check .

[sourcecode language=”delphi” wraplines=”false”]

ZeroMemory(@osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize := sizeof(OSVERSIONINFOEX);
osvi.dwMajorVersion := 5; //at least windows 2000
osvi.dwMinorVersion := 0;
osvi.wServicePackMajor := 0;
osvi.wServicePackMinor := 0;
osvi.wProductType := VER_NT_SERVER; // Check windows server edition
[/sourcecode]

then we need set the flags to interrogate the VerifyVersionInfo function using the VerSetConditionMask function

[sourcecode language=”delphi” wraplines=”false”]
op:=VER_GREATER_EQUAL;
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_MAJORVERSION, op );
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_MINORVERSION, op );
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_SERVICEPACKMINOR, op );
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_PRODUCT_TYPE, VER_EQUAL );
[/sourcecode]

and finally call the function in this way

[sourcecode language=”delphi” wraplines=”false”]
VerifyVersionInfo(osvi,VER_MAJORVERSION OR VER_MINORVERSION OR
VER_SERVICEPACKMAJOR OR VER_SERVICEPACKMINOR OR VER_PRODUCT_TYPE, dwlConditionMask);
[/sourcecode]

This is the source code of the demo console application.

[sourcecode language=”delphi” wraplines=”false”]
program ISWindowsServer;

{$APPTYPE CONSOLE}

uses
Windows,
SysUtils;

{$IFDEF UNICODE}
function VerifyVersionInfo(var LPOSVERSIONINFOEX : OSVERSIONINFOEX;dwTypeMask: DWORD;dwlConditionMask: int64): BOOL; stdcall; external kernel32 name ‘VerifyVersionInfoW’;
{$ELSE}
function VerifyVersionInfo(var LPOSVERSIONINFOEX : OSVERSIONINFOEX;dwTypeMask: DWORD;dwlConditionMask: int64): BOOL; stdcall; external kernel32 name ‘VerifyVersionInfoA’;
{$ENDIF}
function VerSetConditionMask(dwlConditionMask: int64;dwTypeBitMask: DWORD; dwConditionMask: Byte): int64; stdcall; external kernel32;

function IsWinServer : Boolean;
const
VER_NT_SERVER = $0000003;
VER_EQUAL = 1;
VER_GREATER_EQUAL = 3;
var
osvi : OSVERSIONINFOEX;
dwlConditionMask : DWORDLONG;
op : Integer;
begin
dwlConditionMask := 0;
op:=VER_GREATER_EQUAL;

ZeroMemory(@osvi, sizeof(OSVERSIONINFOEX));
osvi.dwOSVersionInfoSize := sizeof(OSVERSIONINFOEX);
osvi.dwMajorVersion := 5;
osvi.dwMinorVersion := 0;
osvi.wServicePackMajor := 0;
osvi.wServicePackMinor := 0;
osvi.wProductType := VER_NT_SERVER;

dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_MAJORVERSION, op );
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_MINORVERSION, op );
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_SERVICEPACKMINOR, op );
dwlConditionMask:=VerSetConditionMask( dwlConditionMask, VER_PRODUCT_TYPE, VER_EQUAL );

Result:=VerifyVersionInfo(osvi,VER_MAJORVERSION OR VER_MINORVERSION OR
VER_SERVICEPACKMAJOR OR VER_SERVICEPACKMINOR OR VER_PRODUCT_TYPE, dwlConditionMask);
end;

const
WindowsEditionStr : array [boolean] of string = (‘Desktop’,’Server’);

begin
try
Writeln( Format(‘Running in Windows %s edition’,[WindowsEditionStr[IsWinServer]]));
except
on E: Exception do
Writeln(E.ClassName, ‘: ‘, E.Message);
end;
readln;
end.
[/sourcecode]

2 responses

  1. […] function. this function compares a set of operating system version requirements to the… [full post] Rodrigo The Road to Delphi – a Blog About Delphi Programming (mostly) delphiwinapi […]

  2. FWIW, VersionHelpers.h does it like this:

    VERSIONHELPERAPI
    IsWindowsServer()
    {
    OSVERSIONINFOEXW osvi = { sizeof(osvi), 0, 0, 0, 0, {0}, 0, 0, 0, VER_NT_WORKSTATION };
    DWORDLONG const dwlConditionMask = VerSetConditionMask( 0, VER_PRODUCT_TYPE, VER_EQUAL );

    return !VerifyVersionInfoW(&osvi, VER_PRODUCT_TYPE, dwlConditionMask);
    }

Leave a Reply