Einzelnen Beitrag anzeigen

NicoDE
(Gast)

n/a Beiträge
 
#5

Re: auf WMI-Verfügbarkeit prüfen

  Alt 5. Jun 2007, 10:24
Zitat von Mazel:
Ich weis aber nicht ob das die beste Variante ist, ich muss erst noch testen, ob eine Fehlermeldung erzeugt wird, die vermieden werden sollte.
Ich wüsste nicht, warum da eine Meldung kommen sollte...
Delphi-Quellcode:
////////////////////////////////////////////////////////////////////////////////
//
// Excerpt from WbemCli.pas
//

type
  TCimType = LongInt;
const
  CIM_EMPTY = TCimType(0);
  CIM_SINT16 = TCimType(2);
  CIM_SINT32 = TCimType(3);
  CIM_REAL32 = TCimType(4);
  CIM_REAL64 = TCimType(5);
  CIM_STRING = TCimType(8);
  CIM_BOOLEAN = TCimType(11);
  CIM_OBJECT = TCimType(13);
  CIM_SINT8 = TCimType(16);
  CIM_UINT8 = TCimType(17);
  CIM_UINT16 = TCimType(18);
  CIM_UINT32 = TCimType(19);
  CIM_SINT64 = TCimType(20);
  CIM_UINT64 = TCimType(21);
  CIM_DATETIME = TCimType(101);
  CIM_REFERENCE = TCimType(102);
  CIM_CHAR16 = TCimType(103);
  CIM_ILLEGAL = TCimType($0FFF);
  CIM_FLAG_ARRAY = TCimType($2000);

type
  IWbemContext = interface(IUnknown)
    ['{44ACA674-E8FC-11D0-A07C-00C04FB68820}']
    function Clone(out ppNewCopy: IWbemContext): HRESULT; stdcall;
    function GetNames(lFlags: LongInt; out pNames: TSafeArray
      ): HRESULT; stdcall;
    function BeginEnumeration(lFlags: LongInt): HRESULT; stdcall;
    function Next(lFlags: LongInt; out pstrName: WideString;
      out pValue: OleVariant): HRESULT; stdcall;
    function EndEnumeration(): HRESULT; stdcall;
    function SetValue(wszName: LPCWSTR; lFlags: LongInt;
      const pValue: OleVariant): HRESULT; stdcall;
    function GetValue(wszName: LPCWSTR; lFlags: LongInt; out pValue: OleVariant
      ): HRESULT; stdcall;
    function DeleteValue(wszName: LPCWSTR; lFlags: LongInt): HRESULT; stdcall;
    function DeleteAll(): HRESULT; stdcall;
  end;

type
  IWbemQualifierSet = interface(IUnknown)
    ['{DC12A680-737F-11CF-884D-00AA004B2E24}']
    function Get(wszName: LPCWSTR; lFlags: LongInt; out pVal: OleVariant;
      out plFlavor: LongInt): HRESULT; stdcall;
    function Put(wszName: LPCWSTR; const pVal: OleVariant; lFlavor: LongInt
      ): HRESULT; stdcall;
    function Delete(wszName: LPCWSTR): HRESULT; stdcall;
    function GetNames(lFlags: LongInt; out pNames: TSafeArray
      ): HRESULT; stdcall;
    function BeginEnumeration(lFlags: LongInt): HRESULT; stdcall;
    function Next(lFlags: LongInt; out pstrName: WideString;
      out pVal: OleVariant; out plFlavor: LongInt): HRESULT; stdcall;
    function EndEnumeration(): HRESULT; stdcall;
  end;

type
  IWbemClassObject = interface(IUnknown)
    ['{DC12A681-737F-11CF-884D-00AA004B2E24}']
    function GetQualifierSet(out ppQualSet: IWbemQualifierSet
      ): HRESULT; stdcall;
    function Get(wszName: LPCWSTR; lFlags: LongInt; out pVal: OleVariant;
      out pType: TCimType; out plFlavor: LongInt): HRESULT; stdcall;
    function Put(wszName: LPCWSTR; lFlags: LongInt; const pVal: OleVariant;
      Type_: TCimType): HRESULT; stdcall;
    function Delete(wszName: LPCWSTR): HRESULT; stdcall;
    function GetNames(wszQualifierName: LPCWSTR; lFlags: LongInt;
      const pQualifierVal: OleVariant; out pNames: TSafeArray
      ): HRESULT; stdcall;
    function BeginEnumeration(lEnumFlags: LongInt): HRESULT; stdcall;
    function Next(lFlags: LongInt; out strName: WideString;
      out pVal: OleVariant; out pType: TCimType; out plFlavor: LongInt
      ): HRESULT; stdcall;
    function EndEnumeration(): HRESULT; stdcall;
    function GetPropertyQualifierSet(wszProperty: LPCWSTR;
      out ppQualSet: IWbemQualifierSet): HRESULT; stdcall;
    function Clone(out ppCopy: IWbemClassObject): HRESULT; stdcall;
    function GetObjectText(lFlags: LongInt; out pstrObjectText: WideString
      ): HRESULT; stdcall;
    function SpawnDerivedClass(lFlags: LongInt; out ppNewClass: IWbemClassObject
      ): HRESULT; stdcall;
    function SpawnInstance(lFlags: LongInt; out ppNewInstance: IWbemClassObject
      ): HRESULT; stdcall;
    function CompareTo(lFlags: LongInt; pCompareTo: IWbemClassObject
      ): HRESULT; stdcall;
    function GetPropertyOrigin(wszName: LPCWSTR; out pstrClassName: WideString
      ): HRESULT; stdcall;
    function InheritsFrom(strAncestor: LPCWSTR): HRESULT; stdcall;
    function GetMethod(wszName: LPCWSTR; lFlags: LongInt;
      out ppInSignature: IWbemClassObject; out ppOutSignature: IWbemClassObject
      ): HRESULT; stdcall;
    function PutMethod(wszName: LPCWSTR; lFlags: LongInt;
      pInSignature: IWbemClassObject; pOutSignature: IWbemClassObject
      ): HRESULT; stdcall;
    function DeleteMethod(wszName: LPCWSTR): HRESULT; stdcall;
    function BeginMethodEnumeration(lEnumFlags: LongInt): HRESULT; stdcall;
    function NextMethod(lFlags: LongInt; out pstrName: WideString;
      out ppInSignature, ppOutSignature: IWbemClassObject): HRESULT; stdcall;
    function EndMethodEnumeration(): HRESULT; stdcall;
    function GetMethodQualifierSet(wszMethod: LPCWSTR;
      out ppQualSet: IWbemQualifierSet): HRESULT; stdcall;
    function GetMethodOrigin(wszMethodName: LPCWSTR;
      out pstrClassName: WideString): HRESULT; stdcall;
  end;

type
  IWbemObjectSink = interface(IUnknown)
    ['{7C857801-7381-11CF-884D-00AA004B2E24}']
    function Indicate(lObjectCount: LongInt; const apObjArray: IWbemClassObject
      ): HRESULT; stdcall;
    function SetStatus(lFlags: LongInt; hResult: HRESULT; strParam: WideString;
      pObjParam: IWbemClassObject): HRESULT; stdcall;
  end;

type
  IEnumWbemClassObject = interface(IUnknown)
    ['{027947E1-D731-11CE-A357-000000000001}']
    function Reset(): HRESULT; stdcall;
    function Next(lTimeout: LongInt; uCount: LongWord;
      out apObjects: IWbemClassObject; out puReturned: LongWord
      ): HRESULT; stdcall;
    function NextAsync(uCount: LongWord; pSink: IWbemObjectSink
      ): HRESULT; stdcall;
    function Clone(out ppEnum: IEnumWbemClassObject): HRESULT; stdcall;
    function Skip(lTimeout: LongInt; nCount: LongWord): HRESULT; stdcall;
  end;

type
  IWbemServices = interface; // forward

  IWbemCallResult = interface(IUnknown)
    ['{44ACA675-E8FC-11D0-A07C-00C04FB68820}']
    function GetResultObject(lTimeout: LongInt;
      out ppResultObject: IWbemClassObject): HRESULT; stdcall;
    function GetResultString(lTimeout: LongInt;
      out pstrResultString: WideString): HRESULT; stdcall;
    function GetResultServices(lTimeout: LongInt; out ppServices: IWbemServices
      ): HRESULT; stdcall;
    function GetCallStatus(lTimeout: LongInt; out plStatus: LongInt
      ): HRESULT; stdcall;
  end;

  IWbemServices = interface(IUnknown)
    ['{9556DC99-828C-11CF-A37E-00AA003240C7}']
    function OpenNamespace(const strNamespace: WideString; lFlags: LongInt;
      pCtx: IWbemContext; out ppWorkingNamespace: IWbemServices;
      out ppResult: IWbemCallResult): HRESULT; stdcall;
    function CancelAsyncCall(pSink: IWbemObjectSink): HRESULT; stdcall;
    function QueryObjectSink(lFlags: LongInt;
      out ppResponseHandler: IWbemObjectSink): HRESULT; stdcall;
    function GetObject(const strObjectPath: WideString; lFlags: LongInt;
      pCtx: IWbemContext; out ppObject: IWbemClassObject;
      out ppCallResult: IWbemCallResult): HRESULT; stdcall;
    function GetObjectAsync(const strObjectPath: WideString; lFlags: LongInt;
      pCtx: IWbemContext; pResponseHandler: IWbemObjectSink): HRESULT; stdcall;
    function PutClass(pObject: IWbemClassObject; lFlags: LongInt;
      pCtx: IWbemContext; out ppCallResult: IWbemCallResult): HRESULT; stdcall;
    function PutClassAsync(pObject: IWbemClassObject; lFlags: LongInt;
      pCtx: IWbemContext; pResponseHandler: IWbemObjectSink): HRESULT; stdcall;
    function DeleteClass(const strClass: WideString; lFlags: LongInt;
      pCtx: IWbemContext; out ppCallResult: IWbemCallResult): HRESULT; stdcall;
    function DeleteClassAsync(const strClass: WideString; lFlags: LongInt;
      pCtx: IWbemContext; pResponseHandler: IWbemObjectSink): HRESULT; stdcall;
    function CreateClassEnum(const strSuperclass: WideString; lFlags: LongInt;
      pCtx: IWbemContext; out ppEnum: IEnumWbemClassObject): HRESULT; stdcall;
    function CreateClassEnumAsync(const strSuperclass: WideString;
      lFlags: LongInt; pCtx: IWbemContext; pResponseHandler: IWbemObjectSink
      ): HRESULT; stdcall;
    function PutInstance(pInst: IWbemClassObject; lFlags: LongInt;
      pCtx: IWbemContext; out ppCallResult: IWbemCallResult): HRESULT; stdcall;
    function PutInstanceAsync(pInst: IWbemClassObject; lFlags: LongInt;
      pCtx: IWbemContext; pResponseHandler: IWbemObjectSink): HRESULT; stdcall;
    function DeleteInstance(const strObjectPath: WideString; lFlags: LongInt;
      pCtx: IWbemContext; out ppCallResult: IWbemCallResult): HRESULT; stdcall;
    function DeleteInstanceAsync(const strObjectPath: WideString;
      lFlags: LongInt; pCtx: IWbemContext; pResponseHandler: IWbemObjectSink
      ): HRESULT; stdcall;
    function CreateInstanceEnum(const strFilter: WideString; lFlags: LongInt;
      pCtx: IWbemContext; out ppEnum: IEnumWbemClassObject): HRESULT; stdcall;
    function CreateInstanceEnumAsync(const strFilter: WideString;
      lFlags: LongInt; pCtx: IWbemContext; pResponseHandler: IWbemObjectSink
      ): HRESULT; stdcall;
    function ExecQuery(const strQueryLanguage: WideString;
      const strQuery: WideString; lFlags: LongInt; pCtx: IWbemContext;
      out ppEnum: IEnumWbemClassObject): HRESULT; stdcall;
    function ExecQueryAsync(const strQueryLanguage, strQuery: WideString;
      lFlags: LongInt; pCtx: IWbemContext; pResponseHandler: IWbemObjectSink
      ): HRESULT; stdcall;
    function ExecNotificationQuery(const strQueryLanguage, strQuery: WideString;
      lFlags: LongInt; pCtx: IWbemContext; out ppEnum: IEnumWbemClassObject
      ): HRESULT; stdcall;
    function ExecNotificationQueryAsync(const strQueryLanguage,
      strQuery: WideString; lFlags: LongInt; pCtx: IWbemContext;
      pResponseHandler: IWbemObjectSink): HRESULT; stdcall;
    function ExecMethod(const strObjectPath, strMethodName: WideString;
      lFlags: LongInt; pCtx: IWbemContext; pInParams: IWbemClassObject;
      out ppOutParams: IWbemClassObject; out ppCallResult: IWbemCallResult
      ): HRESULT; stdcall;
    function ExecMethodAsync(const strObjectPath, strMethodName: WideString;
      lFlags: LongInt; pCtx: IWbemContext; pInParams: IWbemClassObject;
      pResponseHandler: IWbemObjectSink): HRESULT; stdcall;
  end;

type
  IWbemLocator = interface(IUnknown)
    ['{DC12A687-737F-11CF-884D-00AA004B2E24}']
    function ConnectServer(const strNetworkResource, strUser, strPassword,
      strLocale: WideString; lSecurityFlags: LongInt;
      const strAuthority: WideString; pCtx: IWbemContext;
      out ppNamespace: IWbemServices): HRESULT; stdcall;
  end;

const
  IID_IWbemLocator = IWbemLocator;
  CLSID_IWbemLocator: TCLSID = '{4590F811-1D3A-11D0-891F-00AA004B2E24}';

//
////////////////////////////////////////////////////////////////////////////////

function IsWmiAvailable(const AResource: WideString = 'root\default'): Boolean;
var
  Locator: IWbemLocator;
  Resource: WideString;
  Services: IWbemServices;
begin
  Result := False;
  CoInitialize(nil);
  try
    if Succeeded(CoCreateInstance(CLSID_IWbemLocator, nil, CLSCTX_INPROC_SERVER,
      IID_IWbemLocator, Locator)) and Assigned(Locator) then
    try
      //NOTE: [NicoDE] Don't pass a WideString constant directly here.
      // Even if "strNetworkResource" is defined as "const BSTR", the
      // WBEM implementation might raise an exception if you pass the
      // default parameter directly to the ConnectServer() function.
      // Happend on Windows Vista x64 (RTM) with '\\.\root\default'.
      Resource := AResource;
      UniqueString(Resource);
      if Succeeded(Locator.ConnectServer(Resource, '', '', '', 0, '', nil,
        Services)) and Assigned(Services) then
      try
        Result := True;
      finally
        Services := nil;
      end;
    finally
      Locator := nil;
    end;
  finally
    CoUninitialize();
  end;
end;
ps: Deklarationen aus WbemCli.pas reinkopiert.
  Mit Zitat antworten Zitat