Delphi中如何使用WMI获取系统信息?(有点难度哦)(10分)

  • 主题发起人 主题发起人 wwshuo
  • 开始时间 开始时间
W

wwshuo

Unregistered / Unconfirmed
GUEST, unregistred user!
我以前用VB做过,代码是这样的:<br>Dim BiosSet As SWbemObjectSet<br>Dim bios As SWbemObject<br>Set BiosSet = GetObject("winmgmts:{impersonationLevel=impersonate}"). _<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InstancesOf("Win32_BIOS")<br>For Each bios In BiosSet<br>.....取信息<br>Next<br>我刚用Delphi不知道怎样用Delphi来写,WbemScripting_TLB我知道怎样引用,关键是把上面的代码翻译成delphi的。谢谢!再谢谢!
 
var<br>&nbsp; BiosSet, bios: TObject;<br>begin<br>&nbsp; BiosSet := CreateOjbect('SWbemObjectSet');<br>&nbsp; bios := CreateObject('SWbemObject');<br>。。。。。。。。。。。
 
是不是需要.net framework?
 
不是简单的实例创建,还有winmgmts:{impersonationLevel=impersonate}").InstancesOf("Win32_BIOS")
 
BiosSet := GetObject("winmgmts:{impersonationLevel=impersonate}"). _<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;InstancesOf("Win32_BIOS");<br>不用SET,DELPHI里的对象变量实际上是一个引用类型,类指针一样的用
 
我找到源代码了,但是看不懂,请高手指教!<br>http://www.planet-source-code.com/vb/scripts/ShowZip.asp?lngWId=7&amp;lngCodeId=628&amp;strZipAccessCode=tp%2FG6282254<br>
 
包里缺控件 不能运行
 
其实不需要任何第三方的东西 ,d5 就 OK。下面是我写的源代码,完整奉献。记得要谢谢我:-)<br>------------------------------------------------------------------------------<br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; WbemScripting_TLB, OleServer, StdCtrls,ActiveX;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; WbemLocator: TSWbemLocator;<br>&nbsp; &nbsp; Memo1: TMemo;<br>&nbsp; &nbsp; GroupBox1: TGroupBox;<br>&nbsp; &nbsp; Button3: TButton;<br>&nbsp; &nbsp; Button4: TButton;<br>&nbsp; &nbsp; Sink: TSWbemSink;<br>&nbsp; &nbsp; GroupBox2: TGroupBox;<br>&nbsp; &nbsp; Button2: TButton;<br>&nbsp; &nbsp; Button6: TButton;<br>&nbsp; &nbsp; Label1: TLabel;<br>&nbsp; &nbsp; GroupBox3: TGroupBox;<br>&nbsp; &nbsp; Button5: TButton;<br>&nbsp; &nbsp; Button7: TButton;<br>&nbsp; &nbsp; Button8: TButton;<br>&nbsp; &nbsp; Button9: TButton;<br>&nbsp; &nbsp; Button10: TButton;<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button3Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button4Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button5Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button2Click(Sender: TObject);<br>&nbsp; &nbsp; procedure SinkObjectReady(Sender: TObject; var objWbemObject,<br>&nbsp; &nbsp; &nbsp; objWbemAsyncContext: OleVariant);<br>&nbsp; &nbsp; procedure Button6Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button7Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button8Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button9Click(Sender: TObject);<br>&nbsp; &nbsp; procedure Button10Click(Sender: TObject);<br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var Srv:ISWBEMServices;<br><br>&nbsp; &nbsp; ObjSet:ISWBEMobjectSet;<br>&nbsp; &nbsp; Obj:ISWBEMobject;<br><br>&nbsp; &nbsp; Prop:ISWbemProperty;<br><br>&nbsp; &nbsp; Enum:IEnumVARIANT;<br>&nbsp; &nbsp; Sql:string;<br>&nbsp; &nbsp; i:integer;<br>&nbsp; &nbsp; c:Cardinal;<br>&nbsp; &nbsp; OV:OleVariant;<br>begin<br>&nbsp; Srv:=WbemLocator.ConnectServer('.','root/default','','','','',0,nil);<br>&nbsp; Sql:='Select * from TestClass';<br>&nbsp; ObjSet:= Srv.ExecQuery(Sql,'WQL',0,nil);<br>&nbsp; Enum:=IEnumVARIANT(ObjSet._NewEnum);<br><br>&nbsp; Enum.Reset;<br>&nbsp; For i:=0 to ObjSet.Count-1 do begin<br>&nbsp; &nbsp; Enum.Next(1,OV,C);<br>&nbsp; &nbsp; Obj:= IUnknown(OV) as ISWBEMObject;<br>&nbsp; &nbsp; Prop:=Obj.Properties_.Item('name',0);<br>&nbsp; &nbsp; Memo1.Lines.add(Prop.Get_Value);<br>&nbsp; &nbsp; Prop:=Obj.Properties_.Item('age',0);<br>&nbsp; &nbsp; Memo1.Lines.add(Prop.Get_Value);<br>&nbsp; &nbsp; Prop:=Obj.Properties_.Item('address',0);<br>&nbsp; &nbsp; Memo1.Lines.add(Prop.Get_Value);<br>&nbsp; &nbsp; Memo1.Lines.add('');<br>&nbsp; end;<br>end;<br><br>procedure TForm1.Button3Click(Sender: TObject);<br>var Srv:ISWBEMServices;<br>&nbsp; &nbsp; ObjSet:ISWBEMobjectSet;<br>&nbsp; &nbsp; Obj:ISWBEMObject;<br>begin<br>&nbsp; Srv:=WbemLocator.ConnectServer('.','root/cimv2','','','','',0,nil);<br>&nbsp; ObjSet:=Srv.InstancesOf('__namespace',0,nil);<br>&nbsp; obj:=objset.Item('__NAMESPACE.Name="ggc2"',0);<br>&nbsp; obj.Delete_(0,nil);<br>end;<br><br>procedure TForm1.Button4Click(Sender: TObject);<br>var Srv:ISWBEMServices;<br>&nbsp; &nbsp; ObjSet:ISWBEMobjectSet;<br>&nbsp; &nbsp; Obj:ISWBEMObject;<br>&nbsp; &nbsp; OV:OleVariant;<br>&nbsp; &nbsp; Prop:ISWBEMProperty;<br>begin<br>&nbsp; Srv:=WbemLocator.ConnectServer('.','root/cimv2','','','','',0,nil);<br>&nbsp; ObjSet:=Srv.InstancesOf('__namespace',0,nil);<br>&nbsp; obj:=objset.Item('__NAMESPACE.Name="ggc1"',0).SpawnInstance_(0);<br><br>&nbsp; OV:='kkee';<br>&nbsp; Prop:=Obj.Properties_.Item('name',0);<br>&nbsp; Prop.Set_Value(OV);<br><br><br>// 或<br>// &nbsp;prop:=obj.Properties_.Add('name',0,false,0);<br>// &nbsp;Prop.Set_Value(OV);<br><br>&nbsp; Obj.Put_(0,Obj.Properties_);<br>end;<br><br>procedure TForm1.Button5Click(Sender: TObject);<br>var Srv:ISWBEMServices;<br>&nbsp; &nbsp; ObjSet:ISWBEMobjectSet;<br>&nbsp; &nbsp; Obj,OBj2:ISWBEMObject;<br>&nbsp; &nbsp; OV:OleVariant;<br>&nbsp; &nbsp; PropSet:ISWBEMPropertySet;<br>&nbsp; &nbsp; Prop:ISWBEMProperty;<br><br>&nbsp; &nbsp; Enum:IEnumVARIANT;<br>&nbsp; &nbsp; i:integer;<br>&nbsp; &nbsp; c:Cardinal;<br>begin<br>&nbsp; Srv:=WbemLocator.ConnectServer('.','root/default','','','','',0,nil);<br>&nbsp; Obj:=Srv.Get('',0,nil);<br><br>&nbsp; propSet:=Obj.Properties_;<br>{ &nbsp;Enum:=IEnumVARIANT(Propset.Get__NewEnum);<br>&nbsp; for i:=0 to Propset.Count-1 do begin<br>&nbsp; &nbsp; Enum.Next(1,OV,c);<br>&nbsp; &nbsp; Prop:= IUnknown(OV) as ISWBEMProperty;<br>&nbsp; &nbsp; Memo1.Lines.Add(prop.name);<br>&nbsp; end;<br>&nbsp; Exit; &nbsp; }<br><br><br>&nbsp; Obj.Path_.Class_:='TestClass';<br><br>&nbsp; Prop:=PropSet.Add('name',WBEMCIMTYPESTRING,False,0);<br>&nbsp; OV:=True;<br>&nbsp; Prop.Qualifiers_.Add('key',OV,True,True,True,0);<br><br>&nbsp; PropSet.Add('age',WBEMCIMTYPESTRING,False,0);<br>&nbsp; PropSet.Add('Address',WBEMCIMTYPESTRING,False,0);<br><br>&nbsp; Obj.Put_(0,PropSet);<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>var Srv:ISWBEMServices;<br>&nbsp; &nbsp; ObjSet:ISWBEMobjectSet;<br>&nbsp; &nbsp; Obj:ISWBEMObject;<br>&nbsp; &nbsp; OV:OleVariant;<br>&nbsp; &nbsp; Prop:ISWBEMProperty;<br>&nbsp; &nbsp; Query:String;<br>begin<br>&nbsp; Srv:=WbemLocator.ConnectServer('.','','','','','',0,nil);<br>&nbsp; Query := 'SELECT * FROM __InstanceCreationEvent WITHIN 1 WHERE TargetInstance ISA ''Win32_Process''';<br>&nbsp; Srv.ExecNotificationQueryAsync(Sink.DefaultInterface,Query,'WQL',0,nil,nil);<br>end;<br><br>procedure TForm1.SinkObjectReady(Sender: TObject; var objWbemObject,<br>&nbsp; objWbemAsyncContext: OleVariant);<br>var O:SWBEMObject;<br>&nbsp; &nbsp; P,Prop:SWBEMProperty;<br>&nbsp; &nbsp; Enum:IEnumVARIANT;<br>&nbsp; &nbsp; OV:OleVariant;<br>&nbsp; &nbsp; PropSet:SWBEMPropertySet;i:integer;c:cardinal;<br>begin<br>&nbsp; O:=IUnknown(objWbemObject) as SWBEMObject;<br><br>&nbsp; P:=O.Properties_.Item('TargetInstance',0);<br>&nbsp; O:=IUnknown(P.Get_Value) as SWBEMObject;<br><br>&nbsp; P:=O.Properties_.Item('Name',0);<br>&nbsp; Memo1.lines.add(P.Get_Value);<br><br>{ &nbsp;PropSet:=O.Properties_;<br>&nbsp; Enum:=IEnumVARIANT(Propset.Get__NewEnum);<br>&nbsp; for i:=0 to Propset.Count-1 do begin<br>&nbsp; &nbsp; Enum.Next(1,OV,c);<br>&nbsp; &nbsp; Prop:= IUnknown(OV) as ISWBEMProperty;<br>&nbsp; &nbsp; Memo1.Lines.Add(prop.name);<br>&nbsp; end;}<br>end;<br><br>procedure TForm1.Button6Click(Sender: TObject);<br>begin<br>&nbsp; Sink.Cancel;<br>end;<br><br>procedure TForm1.Button7Click(Sender: TObject);<br>var Srv:ISWBEMServices;<br>&nbsp; &nbsp; ObjSet:ISWBEMobjectSet;<br>&nbsp; &nbsp; ClassObj,OBj:ISWBEMObject;<br>&nbsp; &nbsp; OV:OleVariant;<br>&nbsp; &nbsp; PropSet:ISWBEMPropertySet;<br>&nbsp; &nbsp; Prop:ISWBEMProperty;<br><br>&nbsp; &nbsp; Enum:IEnumVARIANT;<br>begin<br>&nbsp; Srv:=WbemLocator.ConnectServer('.','root/default','','','','',0,nil);<br><br>&nbsp; ClassObj:=Srv.Get('TestClass',0,nil);<br>&nbsp; Obj:=ClassObj.SpawnInstance_(0);<br><br>&nbsp; propSet:=Obj.Properties_;<br><br>&nbsp; Prop:=Obj.Properties_.Item('name',0);<br>&nbsp; OV:='龚谷初';<br>&nbsp; Prop.Set_Value(OV);<br><br>&nbsp; Prop:=Obj.Properties_.Item('age',0);<br>&nbsp; OV:='30';<br>&nbsp; Prop.Set_Value(OV);<br><br>&nbsp; Prop:=Obj.Properties_.Item('address',0);<br>&nbsp; OV:='常德市青年南路';<br>&nbsp; Prop.Set_Value(OV);<br><br>&nbsp; Obj.Put_(0,PropSet);<br>// another instance<br>&nbsp; Obj:=ClassObj.SpawnInstance_(0);<br><br>&nbsp; propSet:=Obj.Properties_;<br><br>&nbsp; Prop:=Obj.Properties_.Item('name',0);<br>&nbsp; OV:='林新良';<br>&nbsp; Prop.Set_Value(OV);<br><br>&nbsp; Prop:=Obj.Properties_.Item('age',0);<br>&nbsp; OV:='29';<br>&nbsp; Prop.Set_Value(OV);<br><br>&nbsp; Prop:=Obj.Properties_.Item('address',0);<br>&nbsp; OV:='澧县电力局';<br>&nbsp; Prop.Set_Value(OV);<br><br>&nbsp; Obj.Put_(0,PropSet);<br>end;<br><br><br>procedure TForm1.Button8Click(Sender: TObject);<br>var Srv:ISWBEMServices;<br>&nbsp; &nbsp; ObjSet:ISWBEMobjectSet;<br>&nbsp; &nbsp; ClassObj,OBj:ISWBEMObject;<br>&nbsp; &nbsp; OV:OleVariant;<br>&nbsp; &nbsp; PropSet:ISWBEMPropertySet;<br>&nbsp; &nbsp; Prop:ISWBEMProperty;<br><br>&nbsp; &nbsp; Enum:IEnumVARIANT;<br>begin<br>&nbsp; Srv:=WbemLocator.ConnectServer('.','root/default','','','','',0,nil);<br><br>&nbsp; ObjSet:=Srv.InstancesOf('TestClass',0,nil);<br>&nbsp; <br>&nbsp; Obj:=ObjSet.Item('TestClass.name="lxl"',0);<br>&nbsp; Obj.Delete_(0,nil);<br><br>&nbsp; Obj:=ObjSet.Item('TestClass.name="lxggc"',0);<br>&nbsp; Obj.Delete_(0,nil);<br>&nbsp; <br>end;<br><br>procedure TForm1.Button9Click(Sender: TObject);<br>var Srv:ISWBEMServices;<br>&nbsp; &nbsp; ObjSet:ISWBEMobjectSet;<br>&nbsp; &nbsp; Obj:ISWBEMObject;<br>begin<br>&nbsp; Srv:=WbemLocator.ConnectServer('.','root/default','','','','',0,nil);<br>&nbsp; Obj:=Srv.Get('TestClass',0,nil);<br>&nbsp; Obj.Delete_(0,nil);<br>end;<br><br>procedure TForm1.Button10Click(Sender: TObject);<br>begin<br>&nbsp; memo1.lines.clear;<br>end;<br><br>end.
 
接受答案了.
 
后退
顶部