Skip to content

Commit b3fa3bd

Browse files
committed
Update README.md. Improve docs a bit
1 parent 9392c71 commit b3fa3bd

File tree

6 files changed

+88
-50
lines changed

6 files changed

+88
-50
lines changed

README.md

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ Package wmi provides a WQL interface to Windows WMI.
66

77
Note: It interfaces with WMI on the local machine, therefore it only runs on Windows.
88

9+
## Fork Features
10+
**Fork is fully compatibly with the original repo.** If not - please open an issue.
11+
12+
New features introduced in fork:
13+
- Go 1.11 modules support :)
14+
- Improved decoder:
15+
+ support all basic types: all integer types, `float32`,
16+
`string`, `bool`, `uintptr` and `time.Time`
17+
+ support slices and pointers to all basic types
18+
+ support decoding of structure fields (see [events example](./examples/events/main.go))
19+
+ support structure tags
20+
+ support JSON-like interface for custom decoding
21+
+ suitable for decoding properties of any go-ole IDispatch object
22+
- Ability to perform multiple queries in a single connection
23+
- `SWbemServices.Get` + auto dereference of REF fields
24+
- `SWbemServices.ExecNotificationQuery` support
25+
- More other improvements described in [releases page](https://github.com/bi-zone/wmi/releases)
26+
927
## Example
1028
Print names of the currently running processes
1129
```golang
@@ -18,25 +36,23 @@ import (
1836
"github.com/bi-zone/wmi"
1937
)
2038

21-
// When we use `wmi.CreateQuery` the name of the struct should match querying
22-
// WMI class name.
23-
type Win32_Process struct {
39+
type win32Process struct {
2440
PID uint32 `wmi:"ProcessId"`
2541
Name string
2642
UserField int `wmi:"-"`
2743
}
2844

2945
func main() {
30-
var dst []Win32_Process
46+
var dst []win32Process
3147

32-
q := wmi.CreateQuery(&dst, "")
48+
q := wmi.CreateQueryFrom(&dst, "Win32_Process", "")
3349
fmt.Println(q)
3450

3551
if err := wmi.Query(q, &dst); err != nil {
3652
log.Fatal(err)
3753
}
3854
for _, v := range dst {
39-
fmt.Println(v.PID, v.Name)
55+
fmt.Printf("%6d\t%s\n", v.PID, v.Name)
4056
}
4157
}
4258
```
@@ -57,4 +73,15 @@ BenchmarkQuery_SWbemConnection 5000 30099403 ns/op
5773
You could reproduce the results on your machine running:
5874
```bash
5975
go test -run=NONE -bench=Query -benchtime=120s
60-
```
76+
```
77+
78+
## Versioning
79+
80+
Project uses [semantic versioning](http://semver.org) for version numbers, which
81+
is similar to the version contract of the Go language. Which means that the major
82+
version will always maintain backwards compatibility with minor versions. Minor
83+
versions will only add new additions and changes. Fixes will always be in patch.
84+
85+
This contract should allow you to upgrade to new minor and patch versions without
86+
breakage or modifications to your existing code. Leave a ticket, if there is breakage,
87+
so that it could be fixed.

connection.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ func (s *SWbemServicesConnection) Close() error {
110110
// More info about result unmarshalling is available in `Decoder.Unmarshal` doc.
111111
//
112112
// Query is performed using `SWbemServices.ExecQuery` method.
113+
//
113114
// Ref: https://docs.microsoft.com/en-us/windows/desktop/wmisdk/swbemservices-execquery
114115
func (s *SWbemServicesConnection) Query(query string, dst interface{}) error {
115116
s.Lock()

decoder.go

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,15 @@ var timeType = reflect.TypeOf(time.Time{})
9191
// Unmarshal loads `ole.IDispatch` into a struct pointer.
9292
// N.B. Unmarshal supports only limited subset of structure field
9393
// types:
94-
// - all signed and unsigned integers
95-
// - time.Time
96-
// - string
97-
// - bool
98-
// - float32
99-
// - a pointer to one of types above
100-
// - a slice of one of thus types
101-
// - structure types.
94+
// - all signed and unsigned integers
95+
// - uintptr
96+
// - time.Time
97+
// - string
98+
// - bool
99+
// - float32
100+
// - a pointer to one of types above
101+
// - a slice of one of thus types
102+
// - structure types.
102103
//
103104
// To unmarshal more complex struct consider implementing `wmi.Unmarshaler`.
104105
// For such types Unmarshal just calls `.UnmarshalOLE` on the @src object .
@@ -116,22 +117,22 @@ var timeType = reflect.TypeOf(time.Time{})
116117
//
117118
// Unmarshal allows to specify special COM-object property name or skip a field
118119
// using structure field tags, e.g.
119-
// // Will be filled from property `Frequency_Object`
120-
// FrequencyObject int wmi:"Frequency_Object"`
120+
// // Will be filled from property `Frequency_Object`
121+
// FrequencyObject int wmi:"Frequency_Object"`
121122
//
122-
// // Will be skipped during unmarshalling.
123-
// MyHelperField int wmi:"-"`
123+
// // Will be skipped during unmarshalling.
124+
// MyHelperField int wmi:"-"`
124125
//
125-
// // Will be unmarshalled by CIM reference.
126-
// // See `Dereferencer` for more info.
127-
// Field Type `wmi:"FieldName,ref"
128-
// Field2 Type `wmi:",ref"
126+
// // Will be unmarshalled by CIM reference.
127+
// // See `Dereferencer` for more info.
128+
// Field Type `wmi:"FieldName,ref"
129+
// Field2 Type `wmi:",ref"
129130
//
130131
// Unmarshal prefers tag value over the field name, but ignores any name collisions.
131132
// So for example all the following fields will be resolved to the same value.
132-
// Field int
133-
// Field1 int `wmi:"Field"`
134-
// Field2 int `wmi:"Field"`
133+
// Field int
134+
// Field1 int `wmi:"Field"`
135+
// Field2 int `wmi:"Field"`
135136
func (d Decoder) Unmarshal(src *ole.IDispatch, dst interface{}) (err error) {
136137
defer func() {
137138
// We use lots of reflection, so always be alert!
@@ -254,7 +255,9 @@ var (
254255
// tries to fit it inside a given structure field with some possible
255256
// conversions (e.g. possible integer conversions, string to int parsing
256257
// and others).
257-
// This function handles all oleutil.VARIANT types exclude VT_UNKNOWN and VT_DISPATCH.
258+
//
259+
// This function handles all oleutil.VARIANT types except VT_UNKNOWN and
260+
// VT_DISPATCH.
258261
func unmarshalSimpleValue(dst reflect.Value, value interface{}) error {
259262
switch val := value.(type) {
260263
case int8, int16, int32, int64, int:

doc.go

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,5 @@
22

33
/*
44
Package wmi provides a WQL interface for WMI on Windows.
5-
6-
Example code to print names of running processes:
7-
// When we use `wmi.CreateQuery` the name of the struct should match querying
8-
// WMI class name.
9-
type Win32_Process struct {
10-
PID uint32 `wmi:"ProcessId"`
11-
Name string
12-
UserField int `wmi:"-"`
13-
}
14-
15-
func main() {
16-
var dst []Win32_Process
17-
18-
q := wmi.CreateQuery(&dst, "")
19-
fmt.Println(q)
20-
21-
if err := wmi.Query(q, &dst); err != nil {
22-
log.Fatal(err)
23-
}
24-
for _, v := range dst {
25-
fmt.Println(v.PID, v.Name)
26-
}
27-
}
285
*/
296
package wmi

example_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package wmi_test
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/bi-zone/wmi"
8+
)
9+
10+
// Structure with some fields of Win32_Process
11+
type win32Process struct {
12+
PID uint32 `wmi:"ProcessId"` // Field name differ from Win32_Process
13+
Name string // Same name
14+
UserField int `wmi:"-"` // Shouldn't affect WMI fields
15+
}
16+
17+
func Example_EnumerateRunningProcesses() {
18+
var dst []win32Process
19+
20+
q := wmi.CreateQueryFrom(&dst, "Win32_Process", "")
21+
fmt.Println(q)
22+
23+
if err := wmi.Query(q, &dst); err != nil {
24+
log.Fatal(err)
25+
}
26+
for _, v := range dst {
27+
fmt.Printf("%6d\t%s\n", v.PID, v.Name)
28+
}
29+
}

swbemservices.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ func (s *SWbemServices) Close() error {
9191
//
9292
// By default, the local machine and default namespace are used. These can be
9393
// changed using connectServerArgs. See Ref. for more info.
94+
//
9495
// Ref: https://docs.microsoft.com/en-us/windows/desktop/wmisdk/swbemlocator-connectserver
9596
func (s *SWbemServices) Query(query string, dst interface{}, connectServerArgs ...interface{}) (err error) {
9697
s.Lock()

0 commit comments

Comments
 (0)