Delphi Windows Management Instrumentation Made Easy In Your Delphi Apps With Ultimate Component Suite

FireWind

Свой
Регистрация
2 Дек 2005
Сообщения
1,957
Реакции
1,199
Credits
4,009
Windows Management Instrumentation Made Easy In Your Delphi Apps With Ultimate Component Suite
February 9, 2021 By Anbarasan

Do you need to access management information in an enterprise environment from your Delphi Application? Don’t know where to start with ? Don’t worry. Для просмотра ссылки Войди или Зарегистрируйся System Information Management Suite’s component helps to accessing systems, applications, networks, devices, and other managed components with less code and we will learn how to use MiTeC_WbemScripting_TLB in this blog post. For more info on Windows Management Instrumentation (WMI) Для просмотра ссылки Войди или Зарегистрируйся

Platforms: Windows.

Installation Steps:

You can easily install this Component Suite from GetIt Package Manager. The steps are as follows.
  1. Navigate In RAD Studio IDE->Tools->GetIt Package Manager->select Components in Categories->Components->Trail -MiTec system Information Component Suite 14.3 and click Install Button.
  2. Read the license and Click Agree All. An Information dialog saying ‘Requires a restart of RAD studio at the end of the process. Do you want to proceed? click yes and continue.
  3. It will download the plugin and installs it. Once installed Click Restart now.
How to run the Demo app:
  • Navigate to the System Information Management Suite trails setup, Demos folder which is installed during Get It installation e.g) C:UsersDocumentsEmbarcaderoStudio21.0CatalogRepositoryMiTeC-14.3DemosDelphi7
  • Open the WMIDemo project in RAD studio 10.4.1 compile and Run the application.
  • This Demo App shows how to use MiTeC_WbemScripting_TLB’s ISWbemServices and list the Computer, Operating system, Processor, Memory, Storage etc.
Components used in MSIC WMIDemo App:
  • 3 TEdit‘s to provide machine name, username, password in a enterprise network which helps to connect and access the management information.
  • TTreeView to show the Computer, Operating system, Processor, Memory, Storage etc. in a tree view.
  • TButton to refresh and close.
Implementation Details:
  • Connect to the Machine from the provided machine, username and password details using WMIConnect. An interface variable of type ISWbemServices’s reference is acquired during this connect operation.
  • A WMICommand call helps to retrieve the system information such as operating system, processor, etc. as shown below.
Код:
procedure TForm1.RefreshData;
var
  i,j,k: Integer;
  r,n,c: TTreeNode;
  wmiServices: ISWbemServices;
  wmi,wmi1,wmi2,wmi3: TInstances;
  s,s1,s2: string;
  v: Int64;
begin
  Screen.Cursor:=crHourglass;
  Tree.Hide;
  Tree.Items.BeginUpdate;
  Update;
 
  try
    Tree.Items.Clear;
    if not WMIConnect(eMachine.Text,eUser.Text,ePwd.Text,Rootnamespace,wmiServices) then
      Exit;
 
    WMICommand(wmiServices,'Win32_ComputerSystem',wmi1);
 
    r:=Tree.Items.AddChild(nil,'Computer');
    n:=Tree.Items.AddChild(r,Format('%s - %s',[GetInstancePropertyValue(wmi1,'Name'),
                                               GetInstancePropertyValue(wmi1,'UserName')]));
 
    WMICommand(wmiServices,'Win32_OperatingSystem',wmi);
 
    r:=Tree.Items.AddChild(nil,'Operating system');
    n:=Tree.Items.AddChild(r,GetInstancePropertyValue(wmi,'Name'));
 
    WMICommand(wmiServices,'Win32_Processor',wmi);
 
    r:=Tree.Items.AddChild(nil,'CPU');
    for i:=0 to High(wmi) do begin
      n:=Tree.Items.AddChild(r,Format('%s - %s MHz',[GetInstancePropertyValue(wmi,'Name'),GetInstancePropertyValue(wmi,'CurrentClockSpeed')]));
      c:=Tree.Items.AddChild(n,GetInstancePropertyValue(wmi,'Description'));
    end;
 
    r:=Tree.Items.AddChild(nil,'Memory');
    try v:=StrToInt64(GetInstancePropertyValue(wmi1,'TotalPhysicalMemory')) except v:=0 end;
    n:=Tree.Items.AddChild(r,Format('%d MB',[v shr 20]));
 
    WMICommand(wmiServices,'Win32_DiskDrive',wmi);
 
    r:=Tree.Items.AddChild(nil,'Storage');
    for i:=0 to High(wmi) do begin
      try v:=StrToInt64(GetInstancePropertyValue(wmi,'Size',i)) except v:=0 end;
      n:=Tree.Items.AddChild(r,Format('%s - %d MB',[GetInstancePropertyValue(wmi,'Model',i),
                                                    v shr 20]));
      WMICommand(wmiServices,Format('select * from Win32_DiskPartition where DiskIndex=%s',[GetInstancePropertyValue(wmi,'Index',i)]),wmi1);
      WMICommand(wmiServices,'Win32_LogicalDiskToPartition',wmi2);
      for j:=0 to High(wmi1) do begin
        for k:=0 to High(wmi2) do begin
          s1:=GetInstancePropertyValue(wmi1,'DeviceID',j);
          s2:=GetInstancePropertyValue(wmi2,'Antecedent',k);
          if Pos(s1,s2)>0 then begin
            s:=GetInstancePropertyValue(wmi2,'Dependent',k);
            s:=Copy(s,Pos(':"',s)-1,2);
            WMICommand(wmiServices,Format('select * from Win32_LogicalDisk where DeviceId=''%s''',[s]),wmi3);
            try v:=StrToInt64(GetInstancePropertyValue(wmi,'Size',i)) except v:=0 end;
            c:=Tree.Items.AddChild(n,Format('%s [%s] - %d MB - %s',[s,GetInstancePropertyValue(wmi3,'VolumeName',0),v shr 20,GetInstancePropertyValue(wmi3,'FileSystem',0)]));
          end;
        end;
      end;
    end;
 
    WMICommand(wmiServices,'Win32_CDROMDrive',wmi);
 
    for i:=0 to High(wmi) do begin
      n:=Tree.Items.AddChild(r,GetInstancePropertyValue(wmi,'Name',i));
      c:=Tree.Items.AddChild(n,GetInstancePropertyValue(wmi,'Id',i));
    end;
 
    WMICommand(wmiServices,'Win32_TapeDrive',wmi);
 
    for i:=0 to High(wmi) do begin
      n:=Tree.Items.AddChild(r,GetInstancePropertyValue(wmi,'Name',i));
      c:=Tree.Items.AddChild(n,GetInstancePropertyValue(wmi,'Id',i));
    end;
 
    WMICommand(wmiServices,'Win32_VideoController',wmi);
 
    r:=Tree.Items.AddChild(nil,'Graphics');
    try v:=StrToInt(GetInstancePropertyValue(wmi,'AdapterRAM')) except v:=0 end;
    n:=Tree.Items.AddChild(r,Format('%s - %d MB',[GetInstancePropertyValue(wmi,'Name'),v shr 20]));
 
    WMICommand(wmiServices,'Win32_DesktopMonitor',wmi);
 
    r:=Tree.Items.AddChild(nil,'Monitor');
    for i:=0 to High(wmi) do begin
      n:=Tree.Items.AddChild(r,Format('%s - (%s x %s) px',[GetInstancePropertyValue(wmi,'Name'),
                                                           GetInstancePropertyValue(wmi,'ScreenWidth'),
                                                           GetInstancePropertyValue(wmi,'ScreenHeight')]));
    end;
 
    WMICommand(wmiServices,'Win32_SoundDevice',wmi);
 
    r:=Tree.Items.AddChild(nil,'Audio');
    for i:=0 to High(wmi) do begin
      n:=Tree.Items.AddChild(r,GetInstancePropertyValue(wmi,'Name',i));
    end;
 
    WMICommand(wmiServices,'Win32_NetworkAdapter',wmi);
 
    r:=Tree.Items.AddChild(nil,'Network');
    for i:=0 to High(wmi) do begin
      WMICommand(wmiServices,Format('select * from Win32_NetworkAdapterConfiguration where Caption=''%s''',[GetInstancePropertyValue(wmi,'Caption',i)]),wmi1);
      s1:=GetInstancePropertyValue(wmi,'MACAddress',i);
      s2:=GetInstancePropertyValue(wmi1,'IPAddress',0);
      if not SameText(s1,'NULL') and not SameText(s2,'NULL') then begin
        n:=Tree.Items.AddChild(r,GetInstancePropertyValue(wmi,'Name',i));
        c:=Tree.Items.AddChild(n,s1);
        c:=Tree.Items.AddChild(n,s2);
      end;
    end;
 
    WMICommand(wmiServices,'Win32_Printer',wmi);
 
    r:=Tree.Items.AddChild(nil,'Printers');
    for i:=0 to High(wmi) do begin
      n:=Tree.Items.AddChild(r,GetInstancePropertyValue(wmi,'Name',i));
    end;
 
    Tree.FullExpand;
 
    Tree.Items.GetFirstNode.MakeVisible;
  finally
    WMIDisconnect(wmiServices);
    Finalize(wmi);
    Finalize(wmi1);
    Finalize(wmi2);
    Tree.Items.EndUpdate;
    Tree.Show;
    Screen.Cursor:=crdefault;
  end;
end;
1612890552475.png
MiTeC WMIDemo

Developers can use this MiTeC Component suite and able to obtain management data to automate administrative tasks on remote computers quickly.
Head over and check out the full MiTec System Information Management Suite for Access System Information for Windows in Delphi Applications