Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your code is not working because you are passing a double-quote in the WMI class name.</p> <p>change this code</p> <pre><code>GetWMIstring('','Win32_DiskDrive"','SerialNumber'); </code></pre> <p>To this</p> <pre><code>GetWMIstring('','Win32_DiskDrive','SerialNumber'); </code></pre> <p>Btw, you can improve a lot your WMI function (GetWMIstring) if you follow the recommendations of the answer to this question <a href="https://stackoverflow.com/questions/10199531/how-can-i-improve-the-wmi-performance-using-delphi/10199841#10199841"><code>How can I improve the WMI performance using delphi?</code></a>.</p> <p>Try this sample (this code use late binding and don't need the <code>WbemScripting_TLB</code> unit)</p> <pre><code> uses ActiveX, ComObj; var FSWbemLocator : OLEVariant; FWMIService : OLEVariant; function GetWMIstring(const WMIClass, WMIProperty:string): string; const wbemFlagForwardOnly = $00000020; var FWbemObjectSet: OLEVariant; FWbemObject : OLEVariant; oEnum : IEnumvariant; iValue : LongWord; begin; Result:=''; FWbemObjectSet:= FWMIService.ExecQuery(Format('Select %s from %s',[WMIProperty, WMIClass]),'WQL',wbemFlagForwardOnly); oEnum := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant; if oEnum.Next(1, FWbemObject, iValue) = 0 then if not VarIsNull(FWbemObject.Properties_.Item(WMIProperty).Value) then Result:=FWbemObject.Properties_.Item(WMIProperty).Value; FWbemObject:=Unassigned; end; procedure TForm4.Button1Click(Sender: TObject); var x:string; Y:string; begin FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator'); FWMIService := FSWbemLocator.ConnectServer('localhost', 'root\CIMV2', '', ''); X:=GetWMIstring('Win32_BIOS','SerialNumber'); Y:=GetWMIstring('Win32_PhysicalMedia','SerialNumber'); ShowMessage(x+';'+y); end; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload