Quick.WMI.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.WMI
  4. Description : Common functions
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 04/04/2019
  8. Modified : 08/04/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.WMI;
  22. {$i QuickLib.inc}
  23. {$TYPEDADDRESS OFF}
  24. {$WARN SYMBOL_PLATFORM OFF}
  25. {$WRITEABLECONST ON}
  26. {$VARPROPSETTER ON}
  27. interface
  28. uses
  29. SysUtils,
  30. WbemScripting_TLB,
  31. ActiveX,
  32. ComObj,
  33. Quick.Arrays,
  34. Quick.Value,
  35. Variants;
  36. type
  37. TFlexArray = TArray<TFlexValue>;
  38. TWMIObject = record
  39. private
  40. fInstance : string;
  41. fProperty : string;
  42. end;
  43. TWMICollector = class
  44. private
  45. class function GetObject(const aObjectName: string) : IDispatch;
  46. public
  47. class function GetProperty(const aWMIHost, aRoot, aWMIClass, aWMIProperty : string) : TFlexValue;
  48. class function GetPropertyInstances(const aWMIHost, aRoot, aWMIClass,aWMIProperty : string; const aInstances : string = '*') : TFlexPairArray;
  49. end;
  50. TWMIInstance = class
  51. private
  52. fInstance : string;
  53. public
  54. property Instance :
  55. end;
  56. TWMIObject2 = class
  57. public
  58. function FromInstance(const aInstanceName : string) : TWMIInstance;
  59. end;
  60. EWMICollector = class(Exception);
  61. implementation
  62. class function TWMICollector.GetObject(const aObjectName: string) : IDispatch;
  63. var
  64. chEaten : Integer;
  65. bindCtx : IBindCtx;
  66. moniker : IMoniker;
  67. begin
  68. OleCheck(CreateBindCtx(0, bindCtx));
  69. OleCheck(MkParseDisplayName(bindCtx, PWideChar(aObjectName), chEaten, moniker));
  70. OleCheck(Moniker.BindToObject(bindCtx, nil, IDispatch, Result));
  71. end;
  72. class function TWMICollector.GetProperty(const aWMIHost, aRoot, aWMIClass, aWMIProperty: string) : TFlexValue;
  73. var
  74. objWMIService : OLEVariant;
  75. colItems : OLEVariant;
  76. colItem : OLEVariant;
  77. oEnum : IEnumvariant;
  78. iValue : LongWord;
  79. begin
  80. CoInitialize(nil);
  81. try
  82. try
  83. objWMIService := GetObject(Format('winmgmts:\\%s\%s',[aWMIHost,aRoot]));
  84. colItems := objWMIService.ExecQuery(Format('SELECT * FROM %s',[aWMIClass]),'WQL',0);
  85. oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
  86. while oEnum.Next(1, colItem, iValue) = 0 do
  87. begin
  88. Result := colItem.Properties_.Item(aWMIProperty, 0);
  89. Break;
  90. end;
  91. except
  92. on E : Exception do raise EWMICollector.CreateFmt('Error getting WMI property: %s',[e.Message]);
  93. end;
  94. finally
  95. CoUninitialize;
  96. end;
  97. end;
  98. class function TWMICollector.GetPropertyInstances(const aWMIHost, aRoot, aWMIClass,aWMIProperty : string; const aInstances : string = '*') : TFlexPairArray;
  99. var
  100. objWMIService : OLEVariant;
  101. colItems : OLEVariant;
  102. colItem : OLEVariant;
  103. oEnum : IEnumvariant;
  104. iValue : LongWord;
  105. begin
  106. CoInitialize(nil);
  107. try
  108. try
  109. objWMIService := GetObject(Format('winmgmts:\\%s\%s',[aWMIHost,aRoot]));
  110. colItems := objWMIService.ExecQuery(Format('SELECT * FROM %s',[aWMIClass]),'WQL',0);
  111. oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
  112. while oEnum.Next(1, colItem, iValue) = 0 do
  113. begin
  114. if (aInstances = '*') or (CompareText(aInstances,colItem.Name) = 0) then
  115. begin
  116. Result.Add(colItem.Name,colItem.Properties_.Item(aWMIProperty, 0));
  117. end;
  118. end;
  119. except
  120. on E : Exception do raise EWMICollector.CreateFmt('Error getting WMI property: %s',[e.Message]);
  121. end;
  122. finally
  123. CoUninitialize;
  124. end;
  125. end;
  126. end.