forked from go-ole/go-ole
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ienumvariant_test.go
101 lines (83 loc) · 2.64 KB
/
ienumvariant_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// +build windows
package ole
import "testing"
func TestIEnumVariant_wmi(t *testing.T) {
var err error
var classID *GUID
IID_ISWbemLocator := &GUID{0x76a6415b, 0xcb41, 0x11d1, [8]byte{0x8b, 0x02, 0x00, 0x60, 0x08, 0x06, 0xd9, 0xb6}}
err = CoInitialize(0)
if err != nil {
t.Errorf("Initialize error: %v", err)
}
defer CoUninitialize()
classID, err = ClassIDFrom("WbemScripting.SWbemLocator")
if err != nil {
t.Errorf("CreateObject WbemScripting.SWbemLocator returned with %v", err)
}
comserver, err := CreateInstance(classID, IID_IUnknown)
if err != nil {
t.Errorf("CreateInstance WbemScripting.SWbemLocator returned with %v", err)
}
if comserver == nil {
t.Error("CreateObject WbemScripting.SWbemLocator not an object")
}
defer comserver.Release()
dispatch, err := comserver.QueryInterface(IID_ISWbemLocator)
if err != nil {
t.Errorf("context.iunknown.QueryInterface returned with %v", err)
}
defer dispatch.Release()
wbemServices, err := dispatch.CallMethod("ConnectServer")
if err != nil {
t.Errorf("ConnectServer failed with %v", err)
}
defer wbemServices.Clear()
objectset, err := wbemServices.ToIDispatch().CallMethod("ExecQuery", "SELECT * FROM WIN32_Process")
if err != nil {
t.Errorf("ExecQuery failed with %v", err)
}
defer objectset.Clear()
enum_property, err := objectset.ToIDispatch().GetProperty("_NewEnum")
if err != nil {
t.Errorf("Get _NewEnum property failed with %v", err)
}
defer enum_property.Clear()
enum, err := enum_property.ToIUnknown().IEnumVARIANT(IID_IEnumVariant)
if err != nil {
t.Errorf("IEnumVARIANT() returned with %v", err)
}
if enum == nil {
t.Error("Enum is nil")
t.FailNow()
}
defer enum.Release()
for tmp, length, err := enum.Next(1); length > 0; tmp, length, err = enum.Next(1) {
if err != nil {
t.Errorf("Next() returned with %v", err)
}
tmp_dispatch := tmp.ToIDispatch()
defer tmp_dispatch.Release()
props, err := tmp_dispatch.GetProperty("Properties_")
if err != nil {
t.Errorf("Get Properties_ property failed with %v", err)
}
defer props.Clear()
props_enum_property, err := props.ToIDispatch().GetProperty("_NewEnum")
if err != nil {
t.Errorf("Get _NewEnum property failed with %v", err)
}
defer props_enum_property.Clear()
props_enum, err := props_enum_property.ToIUnknown().IEnumVARIANT(IID_IEnumVariant)
if err != nil {
t.Errorf("IEnumVARIANT failed with %v", err)
}
defer props_enum.Release()
class_variant, err := tmp_dispatch.GetProperty("Name")
if err != nil {
t.Errorf("Get Name property failed with %v", err)
}
defer class_variant.Clear()
class_name := class_variant.ToString()
t.Logf("Got %v", class_name)
}
}