PB.Ex WMI (Windows)

Applications, Games, Tools, User libs and useful stuff coded in PureBasic
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

PB.Ex WMI (Windows)

Post by RSBasic »

Hello,

WMI (Windows Management Instrumentation) is an interface to retrieve information (e. g. battery status, PC information, hard disk information, network information, programs,...), to execute methods (e. g. domain registration, fan control, activate/deactivate network connections, printer management,...) and to query events (e. g. whether the process has been started or stopped,...).

Functions:
  • ExecuteWMIQuery()
    • Syntax:

      Code: Select all

      Result = ExecuteWMIQuery(Namespace$, Query$, @Output$)
    • Description: Executes a query.
    • Parameter:
      1. Namespace$: Name of the namespace.
      2. Query$: Query of the class.
      3. @Output$: The result of the query is stored in XML format in the string variable.
    • Return value:
      • 0: The process was successful.
    • Example:

      Code: Select all

      EnableExplicit
      
      Global PBEx_WMI
      
      CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
        PBEx_WMI = OpenLibrary(#PB_Any, "PB.Ex_WMI_x86.dll")
      CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
        PBEx_WMI = OpenLibrary(#PB_Any, "PB.Ex_WMI_x64.dll")
      CompilerEndIf
      
      If PBEx_WMI
        Prototype ExecuteWMIQuery(Namespace.p-Unicode, Query.p-Unicode, Output)
        Global ExecuteWMIQuery.ExecuteWMIQuery = GetFunction(PBEx_WMI, "ExecuteWMIQuery")
        Prototype ExecuteWMIMethod(Namespace.p-Unicode, Query.p-Unicode, Method.p-Unicode, ParameterArray, ParameterArraySize)
        Global ExecuteWMIMethod.ExecuteWMIMethod = GetFunction(PBEx_WMI, "ExecuteWMIMethod")
        Prototype WaitWMIEvent(Query.p-Unicode, Timeout = 0)
        Global WaitWMIEvent.WaitWMIEvent = GetFunction(PBEx_WMI, "WaitWMIEvent")
        
        Define Output$ = Space(1000000)
        ExecuteWMIQuery("root\CIMV2", "SELECT * FROM Win32_LogicalDisk", @Output$)
        ParseXML(1, Output$)
        FormatXML(1, #PB_XML_WindowsNewline | #PB_XML_ReFormat | #PB_XML_ReIndent)
        Debug ComposeXML(1)
        
        CloseLibrary(PBEx_WMI)
      EndIf
  • ExecuteWMIMethod()
    • Syntax:

      Code: Select all

      Result = ExecuteWMIMethod(Namespace$, Query$, Method$, ParameterArray, ParameterArraySize)
    • Description: Executes a method.
    • Parameter:
      1. Namespace$: Name of the namespace.
      2. Query$: Query of the class and optional with conditions.
      3. Method$: Name of the method to be executed.
      4. ParameterArray: A string array with parameters to pass. Parameter name and value are separated with an equal sign without spaces.
      5. ParameterArraySize: Number of elements.
    • Return value:
      • 0: The process was successful.
    • Example:

      Code: Select all

      EnableExplicit
      
      Global PBEx_WMI
      
      CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
        PBEx_WMI = OpenLibrary(#PB_Any, "PB.Ex_WMI_x86.dll")
      CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
        PBEx_WMI = OpenLibrary(#PB_Any, "PB.Ex_WMI_x64.dll")
      CompilerEndIf
      
      If PBEx_WMI
        Prototype ExecuteWMIQuery(Namespace.p-Unicode, Query.p-Unicode, Output)
        Global ExecuteWMIQuery.ExecuteWMIQuery = GetFunction(PBEx_WMI, "ExecuteWMIQuery")
        Prototype ExecuteWMIMethod(Namespace.p-Unicode, Query.p-Unicode, Method.p-Unicode, ParameterArray, ParameterArraySize)
        Global ExecuteWMIMethod.ExecuteWMIMethod = GetFunction(PBEx_WMI, "ExecuteWMIMethod")
        Prototype WaitWMIEvent(Query.p-Unicode, Timeout = 0)
        Global WaitWMIEvent.WaitWMIEvent = GetFunction(PBEx_WMI, "WaitWMIEvent")
        
        ;Administrator rights required
        Dim ParameterArray.s(0)
        ParameterArray(0) = "Name=YourNewUsername"
        Debug ExecuteWMIMethod("root\CIMV2", "Win32_UserAccount.Domain='YourWorkgroup',Name='YourUsername'", "Rename", ParameterArray(), ArraySize(ParameterArray())+1)
        
        CloseLibrary(PBEx_WMI)
      EndIf
  • WaitWMIEvent()
    • Syntax:

      Code: Select all

      Result = WaitWMIEvent(Class$, Query$, Timeout = 0)
    • Description: Waiting for an event.
    • Parameter:
      1. Query$: Query of the class and optional with conditions.
      2. Timeout: Time in milliseconds, how long the maximum waiting time for an event should be. If nothing or 0 is specified, the system waits until the event has been triggered.
    • Return value:
      • 0: The process was successful.
    • Example:

      Code: Select all

      EnableExplicit
      
      Global PBEx_WMI
      
      CompilerIf #PB_Compiler_Processor = #PB_Processor_x86
        PBEx_WMI = OpenLibrary(#PB_Any, "PB.Ex_WMI_x86.dll")
      CompilerElseIf #PB_Compiler_Processor = #PB_Processor_x64
        PBEx_WMI = OpenLibrary(#PB_Any, "PB.Ex_WMI_x64.dll")
      CompilerEndIf
      
      If PBEx_WMI
        Prototype ExecuteWMIQuery(Namespace.p-Unicode, Query.p-Unicode, Output)
        Global ExecuteWMIQuery.ExecuteWMIQuery = GetFunction(PBEx_WMI, "ExecuteWMIQuery")
        Prototype ExecuteWMIMethod(Namespace.p-Unicode, Query.p-Unicode, Method.p-Unicode, ParameterArray, ParameterArraySize)
        Global ExecuteWMIMethod.ExecuteWMIMethod = GetFunction(PBEx_WMI, "ExecuteWMIMethod")
        Prototype WaitWMIEvent(Query.p-Unicode, Timeout = 0)
        Global WaitWMIEvent.WaitWMIEvent = GetFunction(PBEx_WMI, "WaitWMIEvent")
        
        ;Administrator rights required
        Debug "Wait until the calculator is started...."
        If WaitWMIEvent("SELECT * FROM Win32_ProcessStartTrace WHERE ProcessName = 'calc.exe'") = 0
          Debug "Calculator was started."
        Else
          Debug "Error"
        EndIf
        
        CloseLibrary(PBEx_WMI)
      EndIf
System requirements:
  • Windows XP or higher
  • .NET Framework 3.5 or higher
  • Unicode activation (standard from PB 5.50)
Since the required. NET Framework is preinstalled by default from Windows 7 and can be installed on XP and Vista, this shouldn't be a problem to use this version.
No assembly registration with regasm.exe with administrator rights is necessary as with COMatePLUS.

Licence: This DLL file is free of charge and may be used both privately and commercially.
The following copyright texts must be provided:
Copyright © 2019 RSBasic.de
Download: https://www.rsbasic.de/downloads/downlo ... Ex_WMI.zip
Image

I would be happy to receive feedback, suggestions for improvement, bug reports or requests. If you want to support me, you can also donate me a little something. Thank you :)
Image
Image
User avatar
RSBasic
Moderator
Moderator
Posts: 1218
Joined: Thu Dec 31, 2009 11:05 pm
Location: Gernsbach (Germany)
Contact:

Re: PB.Ex WMI (Windows)

Post by RSBasic »

PB.Ex WMI 1.0.1.0 has been released.

Changelog:
  • Updated: .NET Framework 3.5 > .NET Framework 4.7.2
Image
Image
Post Reply