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.
function VerifyVersionInfo(var lpVersionInformation: TOSVersionInfo; dwTypeMask: DWORD; dwlConditionMask: DWORDLONG): BOOL; stdcall;
And in this case we need pass a OSVERSIONINFOEX Structure, so we must re-declare this function in this way.
{$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}
Now to work with the VerifyVersionInfo function we need establish the requirements to check .
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
then we need set the flags to interrogate the VerifyVersionInfo function using the VerSetConditionMask function
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 );
and finally call the function in this way
VerifyVersionInfo(osvi,VER_MAJORVERSION OR VER_MINORVERSION OR VER_SERVICEPACKMAJOR OR VER_SERVICEPACKMINOR OR VER_PRODUCT_TYPE, dwlConditionMask);
This is the source code of the demo console application.
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.
Pingback: Determine when the Windows is a Desktop or Server edition using Delphi
April 14, 2014 at 5:41 pm
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);
}