System Delphi

Title: What kind of Windows Vista Edition is running ?
Question: If my Delphi application is running under Windows Vista, how can I retrieve the exact Vista running Edition ?
Answer:
Under Windows Vista (and only Windows Vista, remerber my previous article http://www.delphi3000.com/articles/article_5145.asp), I can call the GetProductInfo Windows Vista API function that inform me what kind of Windows Vista Edition is running.
I've found all needed description on how to call this Windows API function on the MSDN site (http://msdn2.microsoft.com/en-us/library/ms724358.aspx)
In my delphi code, all I need to do is to be sure that my application is running under Vista otherwise, by calling an external function that doesn't exist in KERNEL32.DLL, I could see a crash in my application.
My choice has been to define a new type like
TGetProductInfoAPI = function(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType: PDWORD): BOOL stdcall;
with an equivalent function by declaring
function _GetProductInfoAPI(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType: PDWORD): BOOL; stdcall;
I can declare a global variable of this type and initialize it by this line
var
GetProductInfoAPI: TGetProductInfoAPI = _GetProductInfoAPI;
This initialization is very important !
The _GetProductInfoAPI function has the responsability to call the external Windows Vista API function GetProductInfo only if my delphi code runs under Vista. Otherwise, it returns False.
This function is defined by
function _GetProductInfoAPI(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType: PDWORD): BOOL;
var
pFunction: Pointer;
begin
pFunction := GetProcAddress(GetModuleHandle('KERNEL32.DLL'), 'GetProductInfo');
if Assigned(pFunction) then
begin
@GetProductInfoAPI := pFunction;
Result := GetProductInfoAPI(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion, pdwReturnedProductType);
end
else
Result := False;
end;
The entry function will be :
function GetVistaProductInfoStr(var AVistaEdition: String): Boolean;
By calling it you will retrieve the Vista edition in AVistaEdition variable if it returns True.
The usage is :
...
uses uVistaOS; // the complete code is following
...
procedure TForm1.Button1Click(Sender: TObject);
var
sVistaName: String;
begin
if uVistaOS.GetVistaProductInfoStr(sVistaName) then
Caption := sVistaName
else
Caption := 'No Vista Name';
// At this level, you can call the GetVersionEx function
// to retrieve the right Windows version (XP, Win2000...)
end;
Here is the uVistaOS.pas code. You'll retrieve the IsVista function that defined in a previous article.
===================================================================
unit uVistaOS;
interface
uses Windows;
type
TGetProductInfoAPI = function(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType: PDWORD): BOOL stdcall;
function _GetProductInfoAPI(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType: PDWORD): BOOL; stdcall;
function GetVistaProductInfoStr(var AVistaEdition: String): Boolean;
function IsVista: Boolean;

var
GetProductInfoAPI: TGetProductInfoAPI = _GetProductInfoAPI;
implementation
uses SysUtils, Variants;
const
PRODUCT_BUSINESS = $00000006; {Business Edition}
PRODUCT_BUSINESS_N = $00000010; {Business Edition}
PRODUCT_CLUSTER_SERVER = $00000012; {Cluster Server Edition}
PRODUCT_DATACENTER_SERVER = $00000008; {Server Datacenter Edition (full installation)}
PRODUCT_DATACENTER_SERVER_CORE = $0000000C; {Server Datacenter Edition (core installation)}
PRODUCT_DATACENTER_SERVER_CORE_V = $00000027; {Server Datacenter Edition without Hyper-V (core installation)}
PRODUCT_DATACENTER_SERVER_V = $00000025; {Server Datacenter Edition without Hyper-V (full installation)}
PRODUCT_ENTERPRISE = $00000004; {Enterprise Edition}
PRODUCT_ENTERPRISE_N = $0000001B; {Enterprise Edition}
PRODUCT_ENTERPRISE_SERVER = $0000000A; {Server Enterprise Edition (full installation)}
PRODUCT_ENTERPRISE_SERVER_CORE = $0000000E; {Server Enterprise Edition (core installation)}
PRODUCT_ENTERPRISE_SERVER_V = $00000026; {Server Enterprise Edition without Hyper-V (full installation)}
PRODUCT_ENTERPRISE_SERVER_CORE_V = $00000029; {Server Enterprise Edition without Hyper-V (core installation)}
PRODUCT_ENTERPRISE_SERVER_IA64 = $0000000F; {Server Enterprise Edition for Itanium-based Systems}
PRODUCT_HOME_BASIC = $00000002; {Home Basic Edition}
PRODUCT_HOME_BASIC_N = $00000005; {Home Basic Edition}
PRODUCT_HOME_PREMIUM = $00000003; {Home Premium Edition}
PRODUCT_HOME_PREMIUM_N = $0000001A; {Home Premium Edition}
PRODUCT_HOME_SERVER = $00000013; {Home Server Edition}
PRODUCT_SERVER_FOR_SMALLBUSINESS = $00000018; {Server for Small Business Edition}
PRODUCT_SMALLBUSINESS_SERVER = $00000009; {Small Business Server}
PRODUCT_SMALLBUSINESS_SERVER_PREMIUM = $00000019; {Small Business Server Premium Edition}
PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT = $0000001E; {Windows Essential Business Server Management Server}
PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING = $00000020; {Windows Essential Business Server Messaging Server}
PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY = $0000001F; {Windows Essential Business Server Security Server}
PRODUCT_STANDARD_SERVER = $00000007; {Server Standard Edition (full installation)}
PRODUCT_STANDARD_SERVER_V = $00000024; {Server Standard Edition without Hyper-V (full installation)}
PRODUCT_STANDARD_SERVER_CORE = $0000000D; {Server Standard Edition (core installation)}
PRODUCT_STANDARD_SERVER_CORE_V = $00000028; {Server Standard Edition without Hyper-V (core installation)}
PRODUCT_STARTER = $0000000B; {Starter Edition}
PRODUCT_STORAGE_ENTERPRISE_SERVER = $00000017; {Storage Server Enterprise Edition}
PRODUCT_STORAGE_EXPRESS_SERVER = $00000014; {Storage Server Express Edition}
PRODUCT_STORAGE_STANDARD_SERVER = $00000015; {Storage Server Standard Edition}
PRODUCT_STORAGE_WORKGROUP_SERVER = $00000016; {Storage Server Workgroup Edition}
PRODUCT_UNDEFINED = $00000000; {An unknown product}
PRODUCT_ULTIMATE = $00000001; {Ultimate Edition}
PRODUCT_ULTIMATE_N = $0000001C; {Ultimate Edition}
PRODUCT_WEB_SERVER = $00000011; {Web Server Edition}
PRODUCT_WEB_SERVER_CORE = $0000001D; {Web Server Edition (core installation)}
PRODUCT_UNLICENSED = $ABCDABCD; {Unlicensed product}
function IsVista: Boolean;
var
pFunction: Pointer;
begin
pFunction := GetProcAddress(GetModuleHandle('KERNEL32.DLL'), 'GetProductInfo');
Result := Assigned(pFunction);
end;
function _GetProductInfoAPI(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType: PDWORD): BOOL;
var
pFunction: Pointer;
begin
pFunction := GetProcAddress(GetModuleHandle('KERNEL32.DLL'), 'GetProductInfo');
if Assigned(pFunction) then
begin
@GetProductInfoAPI := pFunction;
Result := GetProductInfoAPI(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion, dwSpMinorVersion, pdwReturnedProductType);
end
else
Result := False;
end;
function GetVistaProductInfoStr(var AVistaEdition: String): Boolean;
const
CVistaEdition = 'Windows Vista (%s)';
var
dwReturnedProductType: DWORD;
begin
Result := GetProductInfoAPI(6, 0, 0, 0, @dwReturnedProductType);
if Result then
begin
case dwReturnedProductType of
PRODUCT_BUSINESS : AVistaEdition := 'Business Edition';
PRODUCT_BUSINESS_N : AVistaEdition := 'Business Edition';
PRODUCT_CLUSTER_SERVER : AVistaEdition := 'Cluster Server Edition';
PRODUCT_DATACENTER_SERVER : AVistaEdition := 'Server Datacenter Edition (full installation)';
PRODUCT_DATACENTER_SERVER_CORE : AVistaEdition := 'Server Datacenter Edition (core installation)';
PRODUCT_DATACENTER_SERVER_CORE_V : AVistaEdition := 'Server Datacenter Edition without Hyper-V (core installation)';
PRODUCT_DATACENTER_SERVER_V : AVistaEdition := 'Server Datacenter Edition without Hyper-V (full installation)';
PRODUCT_ENTERPRISE : AVistaEdition := 'Enterprise Edition';
PRODUCT_ENTERPRISE_N : AVistaEdition := 'Enterprise Edition';
PRODUCT_ENTERPRISE_SERVER : AVistaEdition := 'Server Enterprise Edition (full installation)';
PRODUCT_ENTERPRISE_SERVER_CORE : AVistaEdition := 'Server Enterprise Edition (core installation)';
PRODUCT_ENTERPRISE_SERVER_V : AVistaEdition := 'Server Enterprise Edition without Hyper-V (full installation)';
PRODUCT_ENTERPRISE_SERVER_CORE_V : AVistaEdition := 'Server Enterprise Edition without Hyper-V (core installation)';
PRODUCT_ENTERPRISE_SERVER_IA64 : AVistaEdition := 'Server Enterprise Edition for Itanium-based Systems';
PRODUCT_HOME_BASIC : AVistaEdition := 'Home Basic Edition';
PRODUCT_HOME_BASIC_N : AVistaEdition := 'Home Basic Edition';
PRODUCT_HOME_PREMIUM : AVistaEdition := 'Home Premium Edition';
PRODUCT_HOME_PREMIUM_N : AVistaEdition := 'Home Premium Edition';
PRODUCT_HOME_SERVER : AVistaEdition := 'Home Server Edition';
PRODUCT_SERVER_FOR_SMALLBUSINESS : AVistaEdition := 'Server for Small Business Edition';
PRODUCT_SMALLBUSINESS_SERVER : AVistaEdition := 'Small Business Server';
PRODUCT_SMALLBUSINESS_SERVER_PREMIUM : AVistaEdition := 'Small Business Server Premium Edition';
PRODUCT_MEDIUMBUSINESS_SERVER_MANAGEMENT : AVistaEdition := 'Windows Essential Business Server Management Server';
PRODUCT_MEDIUMBUSINESS_SERVER_MESSAGING : AVistaEdition := 'Windows Essential Business Server Messaging Server';
PRODUCT_MEDIUMBUSINESS_SERVER_SECURITY : AVistaEdition := 'Windows Essential Business Server Security Server';
PRODUCT_STANDARD_SERVER : AVistaEdition := 'Server Standard Edition (full installation)';
PRODUCT_STANDARD_SERVER_V : AVistaEdition := 'Server Standard Edition without Hyper-V (full installation)';
PRODUCT_STANDARD_SERVER_CORE : AVistaEdition := 'Server Standard Edition (core installation)';
PRODUCT_STANDARD_SERVER_CORE_V : AVistaEdition := 'Server Standard Edition without Hyper-V (core installation)';
PRODUCT_STARTER : AVistaEdition := 'Starter Edition';
PRODUCT_STORAGE_ENTERPRISE_SERVER : AVistaEdition := 'Storage Server Enterprise Edition';
PRODUCT_STORAGE_EXPRESS_SERVER : AVistaEdition := 'Storage Server Express Edition';
PRODUCT_STORAGE_STANDARD_SERVER : AVistaEdition := 'Storage Server Standard Edition';
PRODUCT_STORAGE_WORKGROUP_SERVER : AVistaEdition := 'Storage Server Workgroup Edition';
PRODUCT_UNDEFINED : AVistaEdition := 'An unknown product';
PRODUCT_ULTIMATE : AVistaEdition := 'Ultimate Edition';
PRODUCT_ULTIMATE_N : AVistaEdition := 'Ultimate Edition';
PRODUCT_WEB_SERVER : AVistaEdition := 'Web Server Edition';
PRODUCT_WEB_SERVER_CORE : AVistaEdition := 'Web Server Edition (core installation)';
PRODUCT_UNLICENSED : AVistaEdition := 'Unlicensed product';
end;
AVistaEdition := Format(CVistaEdition, [AVistaEdition]);
end;
end;
end.
===================================================================