type
TChassisType = (DESKTOP, LAPTOP, UNKNOWN);
const
// the values are declared in Microsoft Platform SDK.
// See description of class Win32_SystemEnclosure
CHASSIS_TYPE_NOTEBOOK = 10;
// this function detects if the destination host is a laptop computer or not.
// It may be used for local or remote host. When used for local host,
// all the parameters must be empty. For remote computer, parameters must be:
// TargetHost: name or IP address of the host, like '10.8.36.54'
// UserName: name of the user, may include domain, like 'MOON\Administrator';
// Password: the user's password.
function DetectLaptop(TargetHost, UserName, Password: string): TChassisType;
var
vConnection: TWmiConnection;
vQuery: TWmiQuery;
vResult: OleVariant;
vLow, vHigh, i: integer;
begin
Result := UNKNOWN;
// setup: create and link the components together
vConnection := TWmiConnection.Create(nil);
vQuery := TWmiQuery.Create(nil);
vQuery.Connection := vConnection;
vQuery.WQL.Text := 'SELECT * FROM Win32_SystemEnclosure';
try
// if remote host specified, setup the credentials
if (TargetHost <> '') then
begin
vConnection.MachineName := TargetHost;
vConnection.Credentials.UserName := UserName;
vConnection.Credentials.Password := Password;
end;
// try to connect. It may fail if the destination computer does not have
// WMI (like windows 95, 98, NT without WMI core installed), or
// if the provided credentials are not valid
try
vQuery.Active := true;
except
Exit;
end;
// laptop in a docking station will have
// more than one system enclosure. Iterate through all of them
while not vQuery.EOF do
begin
vResult := vQuery.FieldByName('ChassisTypes').AsVariant;
vLow := VarArrayLowBound(vResult, 1);
vHigh := VarArrayHighBound(vResult, 1);
for i := vLow to vHigh do
begin
if vResult[i] = CHASSIS_TYPE_NOTEBOOK then
begin
Result := LAPTOP;
Exit;
end;
end;
vQuery.Next;
end;
Result := DESKTOP;
finally
vQuery.Free;
vConnection.Free;
end;
end;
|