forked from ropnop/go-clr
-
Notifications
You must be signed in to change notification settings - Fork 12
/
appdomain.go
212 lines (196 loc) · 6.54 KB
/
appdomain.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//go:build windows
package clr
import (
"fmt"
"syscall"
"unsafe"
"golang.org/x/sys/windows"
)
// AppDomain is a Windows COM object interface pointer for the .NET AppDomain class.
// The AppDomain object represents an application domain, which is an isolated environment where applications execute.
// This structure only contains a pointer to the AppDomain's virtual function table
// https://docs.microsoft.com/en-us/dotnet/api/system.appdomain?view=netframework-4.8
type AppDomain struct {
vtbl *AppDomainVtbl
}
// AppDomainVtbl is a Virtual Function Table for the AppDomain COM interface
// The Virtual Function Table contains pointers to the COM IUnkown interface
// functions (QueryInterface, AddRef, & Release) as well as the AppDomain object's methods
// https://docs.microsoft.com/en-us/dotnet/api/system.appdomain?view=netframework-4.8
type AppDomainVtbl struct {
QueryInterface uintptr
AddRef uintptr
Release uintptr
GetTypeInfoCount uintptr
GetTypeInfo uintptr
GetIDsOfNames uintptr
Invoke uintptr
get_ToString uintptr
Equals uintptr
GetHashCode uintptr
GetType uintptr
InitializeLifetimeService uintptr
GetLifetimeService uintptr
get_Evidence uintptr
add_DomainUnload uintptr
remove_DomainUnload uintptr
add_AssemblyLoad uintptr
remove_AssemblyLoad uintptr
add_ProcessExit uintptr
remove_ProcessExit uintptr
add_TypeResolve uintptr
remove_TypeResolve uintptr
add_ResourceResolve uintptr
remove_ResourceResolve uintptr
add_AssemblyResolve uintptr
remove_AssemblyResolve uintptr
add_UnhandledException uintptr
remove_UnhandledException uintptr
DefineDynamicAssembly uintptr
DefineDynamicAssembly_2 uintptr
DefineDynamicAssembly_3 uintptr
DefineDynamicAssembly_4 uintptr
DefineDynamicAssembly_5 uintptr
DefineDynamicAssembly_6 uintptr
DefineDynamicAssembly_7 uintptr
DefineDynamicAssembly_8 uintptr
DefineDynamicAssembly_9 uintptr
CreateInstance uintptr
CreateInstanceFrom uintptr
CreateInstance_2 uintptr
CreateInstanceFrom_2 uintptr
CreateInstance_3 uintptr
CreateInstanceFrom_3 uintptr
Load uintptr
Load_2 uintptr
Load_3 uintptr
Load_4 uintptr
Load_5 uintptr
Load_6 uintptr
Load_7 uintptr
ExecuteAssembly uintptr
ExecuteAssembly_2 uintptr
ExecuteAssembly_3 uintptr
get_FriendlyName uintptr
get_BaseDirectory uintptr
get_RelativeSearchPath uintptr
get_ShadowCopyFiles uintptr
GetAssemblies uintptr
AppendPrivatePath uintptr
ClearPrivatePath uintptr
SetShadowCopyPath uintptr
ClearShadowCopyPath uintptr
SetCachePath uintptr
SetData uintptr
GetData uintptr
SetAppDomainPolicy uintptr
SetThreadPrincipal uintptr
SetPrincipalPolicy uintptr
DoCallBack uintptr
get_DynamicDirectory uintptr
}
// GetAppDomain is a wrapper function that returns an appDomain from an existing ICORRuntimeHost object
func GetAppDomain(runtimeHost *ICORRuntimeHost) (appDomain *AppDomain, err error) {
debugPrint("Entering into appdomain.GetAppDomain()...")
iu, err := runtimeHost.GetDefaultDomain()
if err != nil {
return
}
err = iu.QueryInterface(IID_AppDomain, unsafe.Pointer(&appDomain))
return
}
func (obj *AppDomain) QueryInterface(riid *windows.GUID, ppvObject *uintptr) uintptr {
debugPrint("Entering into appdomain.QueryInterface()...")
ret, _, _ := syscall.Syscall(
obj.vtbl.QueryInterface,
3,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(riid)),
uintptr(unsafe.Pointer(ppvObject)))
return ret
}
func (obj *AppDomain) AddRef() uintptr {
ret, _, _ := syscall.Syscall(
obj.vtbl.AddRef,
1,
uintptr(unsafe.Pointer(obj)),
0,
0)
return ret
}
func (obj *AppDomain) Release() uintptr {
ret, _, _ := syscall.Syscall(
obj.vtbl.Release,
1,
uintptr(unsafe.Pointer(obj)),
0,
0)
return ret
}
// GetHashCode serves as the default hash function.
// https://docs.microsoft.com/en-us/dotnet/api/system.object.gethashcode?view=netframework-4.8#System_Object_GetHashCode
func (obj *AppDomain) GetHashCode() (int32, error) {
debugPrint("Entering into appdomain.GetHashCode()...")
ret, _, err := syscall.Syscall(
obj.vtbl.GetHashCode,
2,
uintptr(unsafe.Pointer(obj)),
0,
0,
)
if err != syscall.Errno(0) {
return 0, fmt.Errorf("the appdomain.GetHashCode function returned an error:\r\n%s", err)
}
// Unable to avoid misuse of unsafe.Pointer because the Windows API call returns the safeArray pointer in the "ret" value. This is a go vet false positive
return int32(ret), nil
}
// Load_3 Loads an Assembly into this application domain.
// virtual HRESULT __stdcall Load_3 (
// /*[in]*/ SAFEARRAY * rawAssembly,
// /*[out,retval]*/ struct _Assembly * * pRetVal ) = 0;
// https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.load?view=net-5.0
func (obj *AppDomain) Load_3(rawAssembly *SafeArray) (assembly *Assembly, err error) {
debugPrint("Entering into appdomain.Load_3()...")
hr, _, err := syscall.Syscall(
obj.vtbl.Load_3,
3,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(rawAssembly)),
uintptr(unsafe.Pointer(&assembly)),
)
if err != syscall.Errno(0) {
if err != syscall.Errno(1150) {
return
}
}
if hr != S_OK {
err = fmt.Errorf("the appdomain.Load_3 function returned a non-zero HRESULT: 0x%x", hr)
return
}
err = nil
return
}
// ToString Obtains a string representation that includes the friendly name of the application domain and any context policies.
// https://docs.microsoft.com/en-us/dotnet/api/system.appdomain.tostring?view=net-5.0#System_AppDomain_ToString
func (obj *AppDomain) ToString() (domain string, err error) {
debugPrint("Entering into appdomain.ToString()...")
var pDomain *string
hr, _, err := syscall.Syscall(
obj.vtbl.get_ToString,
2,
uintptr(unsafe.Pointer(obj)),
uintptr(unsafe.Pointer(&pDomain)),
0,
)
if err != syscall.Errno(0) {
err = fmt.Errorf("the AppDomain.ToString method retured an error:\r\n%s", err)
return
}
if hr != S_OK {
err = fmt.Errorf("the AppDomain.ToString method returned a non-zero HRESULT: 0x%x", hr)
return
}
err = nil
domain = ReadUnicodeStr(unsafe.Pointer(pDomain))
return
}