wmi

package module
v0.0.0-...-76f9fdd Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2018 License: MIT Imports: 2 Imported by: 0

README

wmi

wmi.go implements a wrapper to Windows Management Instrumentation API. Currently it implements the bits to be able to run WMI queries.

The wrapper is not based on IDispatch which makes it somewhat more performant than automation-based access. The wrapper itself is based on the awesome com-and-go package.

Example

This is a barebones application to query Windows services:

package main

import (
	"github.com/tianlin/com-and-go/v2"
	"github.com/a-palchikov/wmi"
	"fmt"
	"log"
	"unsafe"
)

func main() {
	com.CoInitializeEx(nil, 0)
	if err := wmi.CoInitializeSecurity(nil, -1, nil, nil,
		wmi.RPC_C_AUTHN_LEVEL_DEFAULT, wmi.RPC_C_IMP_LEVEL_IMPERSONATE, nil, wmi.EOAC_NONE, nil); err != nil {
		log.Fatalln("CoInitializeSecurity=", err)
	}

	var locator *wmi.IWbemLocator
	err := com.CoCreateInstance(wmi.CLSID_WbemLocator,
		nil, wmi.CLSCTX_INPROC_SERVER, wmi.IID_IWbemLocator, unsafe.Pointer(&locator))
	if err != nil {
		log.Fatalln("CoCreateInstance=", err)
	}
	defer locator.Release()

	svc := locator.ConnectServer("root\\CIMV2")
	defer svc.Release()

	err = wmi.CoSetProxyBlanket(&svc.IUnknown,
		wmi.RPC_C_AUTHN_WINNT, wmi.RPC_C_AUTHZ_NONE,
		com.BStr{},
		wmi.RPC_C_AUTHN_LEVEL_CALL, wmi.RPC_C_IMP_LEVEL_IMPERSONATE,
		nil,
		wmi.EOAC_NONE)
	if err != nil {
		log.Fatalln("CoSetProxyBlanket=", err)
	}

	enum := svc.ExecQuery("WQL", "select * from win32_service",
		wmi.WBEM_FLAG_FORWARD_ONLY|wmi.WBEM_FLAG_RETURN_IMMEDIATELY)
	defer enum.Release()

	for enum.Next(wmi.WBEM_INFINITE, 1) {
		fmt.Println("Service.Path=", enum.Get("PathName").(string))
	}
	if enum.Err() != nil {
		panic(enum.Err())
	}
}

Documentation

Index

Constants

View Source
const (
	RPC_C_AUTHN_LEVEL_DEFAULT = iota
	RPC_C_AUTHN_LEVEL_NONE
	RPC_C_AUTHN_LEVEL_CONNECT
	RPC_C_AUTHN_LEVEL_CALL
	RPC_C_AUTHN_LEVEL_PKT
	RPC_C_AUTHN_LEVEL_PKT_INTEGRITY
	RPC_C_AUTHN_LEVEL_PKT_PRIVACY
)
View Source
const (
	RPC_C_AUTHN_NONE          = 0
	RPC_C_AUTHN_DCE_PRIVATE   = 1
	RPC_C_AUTHN_DCE_PUBLIC    = 2
	RPC_C_AUTHN_DEC_PUBLIC    = 4
	RPC_C_AUTHN_GSS_NEGOTIATE = 9
	RPC_C_AUTHN_WINNT         = 10
	RPC_C_AUTHN_GSS_SCHANNEL  = 14
	RPC_C_AUTHN_GSS_KERBEROS  = 16
	RPC_C_AUTHN_DPA           = 17
	RPC_C_AUTHN_MSN           = 18
	RPC_C_AUTHN_DIGEST        = 21
	RPC_C_AUTHN_KERNEL        = 20
	RPC_C_AUTHN_NEGO_EXTENDER = 30
	RPC_C_AUTHN_PKU2U         = 31
	RPC_C_AUTHN_MQ            = 100
	RPC_C_AUTHN_DEFAULT       = 0xffffffff
)
View Source
const (
	RPC_C_IMP_LEVEL_DEFAULT     = 0
	RPC_C_IMP_LEVEL_ANONYMOUS   = 1
	RPC_C_IMP_LEVEL_IDENTIFY    = 2
	RPC_C_IMP_LEVEL_IMPERSONATE = 3
	RPC_C_IMP_LEVEL_DELEGATE    = 4
)
View Source
const (
	RPC_C_AUTHZ_NONE    = 0
	RPC_C_AUTHZ_NAME    = 1
	RPC_C_AUTHZ_DCE     = 2
	RPC_C_AUTHZ_DEFAULT = 0xffffffff
)
View Source
const (
	EOAC_NONE              = 0
	EOAC_MUTUAL_AUTH       = 0x1
	EOAC_STATIC_CLOAKING   = 0x20
	EOAC_DYNAMIC_CLOAKING  = 0x40
	EOAC_ANY_AUTHORITY     = 0x80
	EOAC_MAKE_FULLSIC      = 0x100
	EOAC_DEFAULT           = 0x800
	EOAC_SECURE_REFS       = 0x2
	EOAC_ACCESS_CONTROL    = 0x4
	EOAC_APPID             = 0x8
	EOAC_DYNAMIC           = 0x10
	EOAC_REQUIRE_FULLSIC   = 0x200
	EOAC_AUTO_IMPERSONATE  = 0x400
	EOAC_NO_CUSTOM_MARSHAL = 0x2000
	EOAC_DISABLE_AAA       = 0x1000
)
View Source
const (
	WBEM_FLAG_RETURN_IMMEDIATELY     = 0x10
	WBEM_FLAG_RETURN_WBEM_COMPLETE   = 0
	WBEM_FLAG_BIDIRECTIONAL          = 0
	WBEM_FLAG_FORWARD_ONLY           = 0x20
	WBEM_FLAG_NO_ERROR_OBJECT        = 0x40
	WBEM_FLAG_RETURN_ERROR_OBJECT    = 0
	WBEM_FLAG_SEND_STATUS            = 0x80
	WBEM_FLAG_DONT_SEND_STATUS       = 0
	WBEM_FLAG_ENSURE_LOCATABLE       = 0x100
	WBEM_FLAG_DIRECT_READ            = 0x200
	WBEM_FLAG_SEND_ONLY_SELECTED     = 0
	WBEM_RETURN_WHEN_COMPLETE        = 0
	WBEM_RETURN_IMMEDIATELY          = 0x10
	WBEM_MASK_RESERVED_FLAGS         = 0x1f000
	WBEM_FLAG_USE_AMENDED_QUALIFIERS = 0x20000
	WBEM_FLAG_STRONG_VALIDATION      = 0x100000
)
View Source
const (
	WBEM_NO_WAIT  = 0
	WBEM_INFINITE = -1
)
View Source
const (
	CLSCTX_INPROC_SERVER = 0x1
)

Variables

View Source
var (
	CLSID_WbemLocator        = com.NewGUID("{4590f811-1d3a-11d0-891f-00aa004b2e24}")
	IID_IWbemLocator         = com.NewGUID("{dc12a687-737f-11cf-884d-00aa004b2e24}")
	IID_IEnumWbemClassObject = com.NewGUID("{027947e1-d731-11ce-a357-000000000001}")
	IID_IWbemClassObject     = com.NewGUID("{dc12a681-737f-11cf-884d-00aa004b2e24}")
	IID_IWbemServices        = com.NewGUID("{9556dc99-828c-11cf-a37e-00aa003240c7}")
)
View Source
var NilStr = com.BStr{}

Functions

func CoInitializeSecurity

func CoInitializeSecurity(secDesc *uint32, numAuthSvc int, authSvc *uint32,
	reserved1 *uint32, authLevel, impLevel uint32,
	authList *uint32, caps uint32, reserved3 *uint32) error

func CoSetProxyBlanket

func CoSetProxyBlanket(proxy *com.IUnknown, authnSvc, authzSvc uint32, serverPrincName com.BStr,
	authnLevel, impLevel uint32, authInfo *uint32, caps uint32) error

Types

type EnumWbemClassObject

type EnumWbemClassObject struct {
	// contains filtered or unexported fields
}

func (*EnumWbemClassObject) Err

func (d *EnumWbemClassObject) Err() error

func (*EnumWbemClassObject) Get

func (d *EnumWbemClassObject) Get(name string) (ret interface{})

func (*EnumWbemClassObject) Next

func (d *EnumWbemClassObject) Next(timeout int, count uint32) (ok bool)

func (*EnumWbemClassObject) Release

func (d *EnumWbemClassObject) Release()

type IEnumWbemClassObject

type IEnumWbemClassObject struct {
	com.IUnknown
}

func (*IEnumWbemClassObject) NextErr

func (d *IEnumWbemClassObject) NextErr(timeout int, count uint32) (objs *IWbemClassObject, n uint32, err error)

type IWbemClassObject

type IWbemClassObject struct {
	com.IUnknown
}

func (*IWbemClassObject) Get

func (d *IWbemClassObject) Get(name string, flags int) (ret interface{})

func (*IWbemClassObject) GetErr

func (d *IWbemClassObject) GetErr(name string, flags int) (ret interface{}, err error)

type IWbemLocator

type IWbemLocator struct {
	com.IUnknown
}

func (*IWbemLocator) ConnectServer

func (d *IWbemLocator) ConnectServer(resource string) (svc *IWbemServices)

func (*IWbemLocator) ConnectServerErr

func (d *IWbemLocator) ConnectServerErr(resource string) (svc *IWbemServices, err error)

type IWbemServices

type IWbemServices struct {
	com.IUnknown
}

func (*IWbemServices) ExecQuery

func (d *IWbemServices) ExecQuery(queryL, query string, flags int) (enum EnumWbemClassObject)

func (*IWbemServices) ExecQueryErr

func (d *IWbemServices) ExecQueryErr(queryL, query string, flags int) (enum EnumWbemClassObject, err error)

Jump to

Keyboard shortcuts

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