wmi

package module
v0.0.0-...-bfaf589 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2022 License: MIT Imports: 4 Imported by: 0

README

wmi

Go Reference

Package wmi provides an interface to WMI. (Windows Management Instrumentation)

Install

go get -v github.com/moonchant12/wmi

Import

import "github.com/moonchant12/wmi"

Usage

Call method with params
// Create process using WMI
func main() {
	// https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-process
	// https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/create-method-in-class-win32-process
	// https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-processstartup
	wmi.With("Win32_ProcessStartup", func(objStartupConfig *ole.IDispatch) error {
		var pid int32
		objStartupConfig.PutProperty("CreateFlags", windows.CREATE_NEW_CONSOLE)
		objStartupConfig.PutProperty("Title", "nice")
		log.Println(wmi.CallMethod("Win32_Process", "Create", "cmd /C notepad.exe", "C:\\Windows\\System32", objStartupConfig, &pid))
		log.Println(pid)
		return nil
	})
}

Outcome

2022/01/03 00:00:00 0 <nil>
2022/01/03 00:00:00 9996
Query
// Query system processes information using WMI
func main() {
	var res []struct {
		Name        string
		HandleCount uint32
	}
	wmi.Query("SELECT Name, HandleCount FROM Win32_Process", &res)
	log.Println(res)
}

Outcome

2022/01/03 00:00:00 [{System Idle Process 0} {System 3860} {Secure System 0} {Registry 0}
...

Documentation

Rendered for windows/amd64

Overview

Package wmi provides an interface to WMI. (Windows Management Instrumentation)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallMethod

func CallMethod(className, methodName string, params ...interface{}) (int32, error)

CallMethod usage: wmi.CallMethod("Win32_Process", "Create", "cmd /C notepad.exe", "C:\\Windows\\System32", objStartupConfig, &pid)

CallMethod calls a method named methodName on an instance of the class named className, with the given params.

CallMethod is a wrapper around DefaultClient.CallMethod.

func Query

func Query(query string, dst interface{}, connectServerArgs ...interface{}) error

Query usage: wmi.Query("SELECT Name, HandleCount FROM Win32_Process", &res)

var res []struct { Name string HandleCount uint32 }

Query runs the WQL query and appends the values to dst.

dst must have type *[]S or *[]*S, for some struct type S. Fields selected in the query must have the same name in dst. Supported types are all signed and unsigned integers, time.Time, string, bool, or a pointer to one of those. Array types are not supported.

By default, the local machine and default namespace are used. These can be changed using connectServerArgs. See https://docs.microsoft.com/en-us/windows/desktop/WmiSdk/swbemlocator-connectserver for details.

Query is a wrapper around DefaultClient.Query.

func With

func With(className string, do func(class *ole.IDispatch) error) error

With usage: wmi.With("Win32_ProcessStartup", func(objStartupConfig *ole.IDispatch) { ... })

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL