-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAXCtrl.c
257 lines (223 loc) · 5.22 KB
/
AXCtrl.c
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Created by patryk on 23.06.16.
*/
#include "AXCtrl.h"
HRESULT AX_init(void) {
HRESULT res;
res = OleInitialize(NULL);
return res <= S_FALSE ? S_OK : res;
}
void AX_deinit(void) {
OleUninitialize();
}
HRESULT AX_getControl(AX_object *obj, LPCOLESTR name) {
HRESULT res;
CLSID id;
/*TODO: error specific to init step*/
if((res = CLSIDFromProgID(name, &id)) ||
(res = CoCreateInstance(&id, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER, &IID_IDispatch, (void **) &obj->dispatch)) ||
(res = obj->dispatch->lpVtbl->QueryInterface(obj->dispatch, &IID_IPersistStreamInit, (void **) &obj->stream)) ||
(res = obj->stream->lpVtbl->InitNew(obj->stream))) {
return res;
}
return S_OK;
}
HRESULT AX_destroyControl(AX_object *obj) {
HRESULT res;
if((res = obj->stream->lpVtbl->Release(obj->stream)) ||
(res = obj->dispatch->lpVtbl->Release(obj->dispatch))) {
return res;
}
return S_OK;
}
/*TODO: enum with return codes*/
HRESULT AX_callMethod(AX_object *obj, OLECHAR *name, enum VARENUM ret_type, void *ret_val, ...) {
va_list args;
enum VARENUM type;
VARIANT *var, tmp, ret;
DISPPARAMS params;
size_t allocated, length, i;
HRESULT res;
DISPID id;
res = obj->dispatch->lpVtbl->GetIDsOfNames(obj->dispatch, &IID_NULL, &name, 1, LOCALE_SYSTEM_DEFAULT, &id);
if(res) {
return res;
}
length = 0;
allocated = 5;
var = calloc(allocated, sizeof *var);
va_start(args, ret_val);
while((type = va_arg(args, enum VARENUM))) {
var[length].vt = type;
switch(type) {
case VT_I1:
var[length++].cVal = (CHAR)va_arg(args, int);
break;
case VT_I2:
var[length++].iVal = (SHORT)va_arg(args, int);
break;
case VT_I4:
var[length++].lVal = va_arg(args, LONG);
break;
case VT_I8:
var[length++].llVal = va_arg(args, LONGLONG);
break;
case VT_UI1:
var[length++].bVal = (BYTE)va_arg(args, int);
break;
case VT_UI2:
var[length++].uiVal = (USHORT)va_arg(args, int);
break;
case VT_UI4:
var[length++].ulVal = va_arg(args, ULONG);
break;
case VT_UI8:
var[length++].ullVal = va_arg(args, ULONGLONG);
break;
case VT_R4:
var[length++].fltVal = (float)va_arg(args, double);
break;
case VT_R8:
var[length++].dblVal = va_arg(args, DOUBLE);
break;
case VT_BSTR:
var[length++].bstrVal = SysAllocString(va_arg(args, LPWSTR));
break;
case VT_BOOL:
var[length++].boolVal = va_arg(args, BOOL) ? TRUE : FALSE;
break;
default:
while(length--) {
VariantClear(var + length * sizeof *var);
}
free(var);
return -1;
}
if(length == allocated) {
allocated += 5;
var = realloc(var, allocated);
}
}
va_end(args);
for(i = 0; i < length / 2; i++) {
tmp = var[i];
var[i] = var[length - 1 - i];
var[length - 1 - i] = tmp;
}
params.cArgs = length;
params.cNamedArgs = 0;
params.rgvarg = var;
res = obj->dispatch->lpVtbl->Invoke(obj->dispatch, id, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, ¶ms,
&ret, NULL, NULL);
while(length--) {
VariantClear(var + length * sizeof *var);
}
free(var);
if(res) {
return res;
}
switch(ret_type) {
case VT_NULL:
case VT_EMPTY:
VariantClear(&ret);
break;
case VT_I1:
*(CHAR *)ret_val = ret.cVal;
break;
case VT_I2:
*(SHORT *)ret_val = ret.iVal;
break;
case VT_I4:
*(LONG *)ret_val = ret.lVal;
break;
case VT_I8:
*(LONGLONG *)ret_val = ret.llVal;
break;
case VT_UI1:
*(BYTE *)ret_val = ret.bVal;
break;
case VT_UI2:
*(USHORT *)ret_val = ret.uiVal;
break;
case VT_UI4:
*(ULONG *)ret_val = ret.ulVal;
break;
case VT_UI8:
*(ULONGLONG *)ret_val = ret.ullVal;
break;
case VT_R4:
*(FLOAT *)ret_val = ret.fltVal;
break;
case VT_R8:
*(DOUBLE *)ret_val = ret.dblVal;
break;
case VT_BSTR:
*(BSTR *)ret_val = ret.bstrVal;
break;
case VT_BOOL:
*(BOOL *)ret_val = ret.boolVal;
break;
default:
return -3;
}
return S_OK;
}
HRESULT AX_setValue(AX_object *obj, OLECHAR *name, enum VARENUM type, void *data) {
VARIANT var;
DISPPARAMS params;
DISPID id, disp;
HRESULT res;
res = obj->dispatch->lpVtbl->GetIDsOfNames(obj->dispatch, &IID_NULL, &name, 1, LOCALE_SYSTEM_DEFAULT, &id);
if(res) {
return res;
}
var.vt = type;
switch(type) {
case VT_I1:
var.cVal = *(CHAR *)data;
break;
case VT_I2:
var.iVal = *(SHORT *)data;
break;
case VT_I4:
var.lVal = *(LONG *)data;
break;
case VT_I8:
var.llVal = *(LONGLONG *)data;
break;
case VT_UI1:
var.bVal = *(BYTE *)data;
break;
case VT_UI2:
var.uiVal = *(USHORT *)data;
break;
case VT_UI4:
var.ulVal = *(ULONG *)data;
break;
case VT_UI8:
var.ullVal = *(ULONGLONG *)data;
break;
case VT_R4:
var.fltVal = *(FLOAT *)data;
break;
case VT_R8:
var.dblVal = *(DOUBLE *)data;
break;
case VT_BSTR:
var.bstrVal = SysAllocString((LPWSTR)data);
break;
case VT_BOOL:
var.boolVal = *(BOOL *)data ? TRUE : FALSE;
break;
default:
return -1;
}
disp = DISPID_PROPERTYPUT;
params.cArgs = 1;
params.rgvarg = &var;
params.cNamedArgs = 1;
params.rgdispidNamedArgs = &disp;
res = obj->dispatch->lpVtbl->Invoke(obj->dispatch, id, &IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYPUT, ¶ms, NULL, NULL, NULL);
VariantClear(&var);
return res;
}