Windows版本使用GetProductInfo

时间:2013-03-26 18:33:52

标签: delphi

我正在尝试使用Delphi XE2中的GetProductInfo功能显示Vista版本的Windows版本和产品详细信息,如Senoir会员所建议的那样。我已经定义了以下代码,但我无法编译它。我的代码如下:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
TGetProductInfoAPI = function(dwOSMajorVersion, dwOSMinorVersion,
dwSpMajorVersion, dwSpMinorVersion: DWORD; pdwReturnedProductType:
PDWORD): BOOL stdcall;
begin
  Edit1.Text:=IntToStr(dwOSMajorVersion);
  Edit2.Text:=IntToStr(dwOSMinorVersion);
  Edit3.Text:=IntToStr(dwSpMajorVersion);
  Edit4.Text:=IntToStr(dwSpMinorVersion);
  Edit5.Text:=TGetProductInfoAPI.pdwReturnedProductType;
end;

end.

我认为我犯了一些错误,但我无法找到它。请告诉我我在哪里弄错了。

1 个答案:

答案 0 :(得分:2)

kernel32.dll中使用this function的正确方法是编写

function GetProductInfo(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion,
  dwSpMinorVersion: DWORD; out pdwReturnedProductType: DWORD): BOOL stdcall;
  external kernel32 delayed;

在某个单位(任何程序之外,类型声明等)

例如,

function GetProductInfo(dwOSMajorVersion, dwOSMinorVersion, dwSpMajorVersion,
  dwSpMinorVersion: DWORD; out pdwReturnedProductType: DWORD): BOOL stdcall;
  external kernel32 delayed;

const
  PRODUCT_HOME_PREMIUM = $00000003;

procedure TForm1.Button1Click(Sender: TObject);
var
  rpt: cardinal;
begin
  if GetProductInfo(6, 1, 0, 0, rpt) then
    if rpt and PRODUCT_HOME_PREMIUM <> 0 then
      ShowMessage('Yeah, you''re running Home Premium.');
end;

如果您只想查看返回的值,那么只需显示它!

procedure TForm1.Button2Click(Sender: TObject);
var
  rpt: cardinal;
begin
  if GetProductInfo(6, 1, 0, 0, rpt) then
    ShowMessage(IntToStr(rpt));
end;

另外,只是为了说清楚:这个函数的四个第一个参数(显然)是输入,而不是输出。如果您想获得操作系统的版本,可以使用GetVersionEx或只是查看Win32MajorVersion中的变量SysUtils等。