彼得。在使用WMI連接到遠程計算機之前,必須啓用對遠程計算機中指定用戶的DCOM訪問。
閱讀這些文章,瞭解並解決使用WMI連接到遠程計算機的問題。
另外這裏我留下一個更清晰的代碼來連接到WMI在遠程計算機上。檢查EOleException
異常處理的部分以獲取錯誤代碼並找出問題的原因。
program WMIRemote;
{$APPTYPE CONSOLE}
uses
SysUtils,
ActiveX,
ComObj,
Variants;
procedure GetWMIOSInfo(const RemoteMachine,User,Password : string);
var
FSWbemLocator : OLEVariant;
FWMIService : OLEVariant;
FWbemObjectSet: OLEVariant;
FWbemObject : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
begin;
FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
FWMIService := FSWbemLocator.ConnectServer(RemoteMachine, 'root\CIMV2', User, Password);
FWbemObjectSet:= FWMIService.ExecQuery('SELECT * FROM Win32_OperatingSystem','WQL',0);
oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
while oEnum.Next(1, FWbemObject, iValue) = 0 do
begin
Writeln(FWbemObject.Name);
//code
FWbemObject:=Unassigned;
end;
FWbemObjectSet:=Unassigned;
end;
begin
try
CoInitialize(nil);
try
//GetWMIOSInfo('localhost','','');
GetWMIOSInfo('192.168.52.2','Administrator','password');
finally
CoUninitialize;
end;
except
on E:EOleException do
Writeln(Format('Error Code %d ($%x) Msg : %s',[E.ErrorCode,E.ErrorCode, E.Message]));
on E:Exception do
Writeln(E.Classname, ':', E.Message);
end;
Readln;
end.
謝謝你的回覆+代碼。它像一個魅力。問候,彼得。 – 2011-02-15 12:12:17