-
Notifications
You must be signed in to change notification settings - Fork 63
/
disp.h
400 lines (348 loc) · 14.9 KB
/
disp.h
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//-------------------------------------------------------------------------------------------------------
// Project: NodeActiveX
// Author: Yuri Dursin
// Description: DispObject class declarations. This class incapsulates COM IDispatch interface to Node JS Object
//-------------------------------------------------------------------------------------------------------
#pragma once
#include "utils.h"
enum options_t {
option_none = 0,
option_async = 0x0001,
option_type = 0x0002,
option_activate = 0x0004,
option_prepared = 0x0100,
option_owned = 0x0200,
option_property = 0x0400,
option_function_simple = 0x0800,
option_mask = 0x00FF,
option_auto = (option_async | option_type)
};
inline bool TypeInfoGetName(ITypeInfo *info, DISPID dispid, BSTR *name) {
HRESULT hrcode = info->GetDocumentation(dispid, name, NULL, NULL, NULL);
if SUCCEEDED(hrcode) return true;
UINT cnt_ret;
return info->GetNames(dispid, name, 1, &cnt_ret) == S_OK && cnt_ret > 0;
}
template<typename T>
bool TypeInfoPrepareFunc(ITypeInfo *info, UINT n, T process) {
FUNCDESC *desc;
if (info->GetFuncDesc(n, &desc) != S_OK) return false;
process(info, desc, nullptr);
info->ReleaseFuncDesc(desc);
return true;
}
template<typename T>
bool TypeInfoPrepareVar(ITypeInfo *info, UINT n, T process) {
VARDESC *desc;
if (info->GetVarDesc(n, &desc) != S_OK) return false;
process(info, nullptr, desc);
info->ReleaseVarDesc(desc);
return true;
}
template<typename T>
void TypeInfoPrepare(ITypeInfo *info, int mode, T process) {
UINT cFuncs = 0, cVars = 0;
TYPEATTR *pattr = NULL;
if (info->GetTypeAttr(&pattr) == S_OK) {
cFuncs = pattr->cFuncs;
cVars = pattr->cVars;
info->ReleaseTypeAttr(pattr);
}
if ((mode & 1) != 0) {
for (UINT n = 0; n < cFuncs; n++) {
TypeInfoPrepareFunc<T>(info, n, process);
}
}
if ((mode & 2) != 0) {
for (UINT n = 0; n < cVars; n++) {
TypeInfoPrepareVar<T>(info, n, process);
}
}
}
template<typename T>
bool TypeLibEnumerate(ITypeLib *typelib, int mode, T process) {
UINT i, cnt = typelib ? typelib->GetTypeInfoCount() : 0;
for (i = 0; i < cnt; i++) {
CComPtr<ITypeInfo> info;
if (typelib->GetTypeInfo(i, &info) != S_OK) continue;
TypeInfoPrepare<T>(info, mode, process);
}
return cnt > 0;
}
class DispInfo {
public:
std::weak_ptr<DispInfo> parent;
CComPtr<IDispatch> ptr;
std::wstring name;
int options;
bool bManaged;
struct type_t {
DISPID dispid;
int kind;
int argcnt_get;
inline type_t(DISPID dispid_, int kind_) : dispid(dispid_), kind(kind_), argcnt_get(0) {}
inline bool is_property() const { return ((kind & INVOKE_FUNC) == 0); }
inline bool is_property_simple() const { return (((kind & (INVOKE_PROPERTYGET | INVOKE_FUNC))) == INVOKE_PROPERTYGET) && (argcnt_get == 0); }
inline bool is_function_simple() const { return (((kind & (INVOKE_PROPERTYGET | INVOKE_FUNC))) == INVOKE_FUNC) && (argcnt_get == 0); }
inline bool is_property_advanced() const { return kind == (INVOKE_PROPERTYGET | INVOKE_PROPERTYPUT) && (argcnt_get == 1); }
};
typedef std::shared_ptr<type_t> type_ptr;
typedef std::map<DISPID, type_ptr> types_by_dispid_t;
types_by_dispid_t types_by_dispid;
inline DispInfo(IDispatch *disp, const std::wstring &nm, int opt, std::shared_ptr<DispInfo> *parnt = nullptr)
: ptr(disp), options(opt), name(nm), bManaged(false)
{
if (parnt) parent = *parnt;
if ((options & option_type) != 0)
Prepare(disp);
}
void Prepare(IDispatch *disp) {
Enumerate(1/*functions only*/, &bManaged, [this](ITypeInfo *info, FUNCDESC *func, VARDESC *var) {
type_ptr &ptr = this->types_by_dispid[func->memid];
if (!ptr) ptr.reset(new type_t(func->memid, func->invkind));
else ptr->kind |= func->invkind;
if ((func->invkind & INVOKE_PROPERTYGET) != 0) {
if (func->cParams > ptr->argcnt_get)
ptr->argcnt_get = func->cParams;
}
});
bool prepared = types_by_dispid.size() > 3; // QueryInterface, AddRef, Release
if (prepared) options |= option_prepared;
}
inline bool GetTypeInfo(const DISPID dispid, type_ptr &info) {
if ((options & option_prepared) == 0) return false;
types_by_dispid_t::const_iterator it = types_by_dispid.find(dispid);
if (it == types_by_dispid.end()) return false;
info = it->second;
return true;
}
HRESULT FindProperty(LPOLESTR name, DISPID *dispid) {
return DispFind(ptr, name, dispid);
}
HRESULT GetProperty(DISPID dispid, LONG argcnt, VARIANT *args, VARIANT *value, EXCEPINFO *except = 0) {
HRESULT hrcode = DispInvoke(ptr, dispid, argcnt, args, value, DISPATCH_PROPERTYGET, except);
return hrcode;
}
HRESULT GetProperty(DISPID dispid, LONG index, VARIANT *value, EXCEPINFO *except = 0) {
CComVariant arg(index);
LONG argcnt = (index >= 0) ? 1 : 0;
return DispInvoke(ptr, dispid, argcnt, &arg, value, DISPATCH_PROPERTYGET, except);
}
HRESULT SetProperty(DISPID dispid, LONG argcnt, VARIANT *args, VARIANT *value, EXCEPINFO *except = 0) {
HRESULT hrcode = DispInvoke(ptr, dispid, argcnt, args, value, DISPATCH_PROPERTYPUT, except);
if FAILED(hrcode) value->vt = VT_EMPTY;
return hrcode;
}
HRESULT ExecuteMethod(DISPID dispid, LONG argcnt, VARIANT *args, VARIANT *value, EXCEPINFO *except = 0) {
HRESULT hrcode = DispInvoke(ptr, dispid, argcnt, args, value, DISPATCH_METHOD, except);
return hrcode;
}
template<typename T>
bool Enumerate(int mode, bool* checkManaged, T process = nullptr) {
UINT i, cnt;
CComPtr<ITypeLib> prevtypelib;
if (!ptr || FAILED(ptr->GetTypeInfoCount(&cnt))) cnt = 0;
else for (i = 0; i < cnt; i++) {
CComPtr<ITypeInfo> info;
if (ptr->GetTypeInfo(i, 0, &info) != S_OK) continue;
// Query type library
UINT typeindex;
CComPtr<ITypeLib> typelib;
if (info->GetContainingTypeLib(&typelib, &typeindex) == S_OK) {
// Check if typelib is managed
if (checkManaged != nullptr && !*checkManaged) {
CComPtr<ITypeLib2> typeLib2;
if (SUCCEEDED(typelib->QueryInterface(IID_ITypeLib2, (void**)&typeLib2))) {
// {90883F05-3D28-11D2-8F17-00A0C9A6186D}
static const GUID GUID_ExportedFromComPlus = { 0x90883F05, 0x3D28, 0x11D2, { 0x8F, 0x17, 0x00, 0xA0, 0xC9, 0xA6, 0x18, 0x6D } };
CComVariant cv;
if (SUCCEEDED(typeLib2->GetCustData(GUID_ExportedFromComPlus, &cv))) {
*checkManaged = true;
}
}
}
// Enumerate all types in library types
// May be very slow! need a special method
// if (typelib != prevtypelib) {
// TypeLibEnumerate<T>(typelib, mode, process);
// prevtypelib.Attach(typelib.Detach());
// }
CComPtr<ITypeInfo> tinfo;
if (typelib->GetTypeInfo(typeindex, &tinfo) == S_OK) {
TypeInfoPrepare<T>(tinfo, mode, process);
}
}
// Process types
else {
TypeInfoPrepare<T>(info, mode, process);
}
}
return cnt > 0;
}
};
typedef std::shared_ptr<DispInfo> DispInfoPtr;
class DispObject: public NodeObject
{
public:
DispObject(const DispInfoPtr &ptr, const std::wstring &name, DISPID id = DISPID_UNKNOWN, LONG indx = -1, int opt = 0);
~DispObject();
static Persistent<ObjectTemplate> inst_template;
static Persistent<FunctionTemplate> clazz_template;
static NodeMethods clazz_methods;
static void NodeInit(const Local<Object> &target, Isolate* isolate, Local<Context> &ctx);
static bool HasInstance(Isolate *isolate, const Local<Value> &obj) {
Local<FunctionTemplate> clazz = clazz_template.Get(isolate);
return !clazz.IsEmpty() && clazz->HasInstance(obj);
}
static DispObject *GetPtr(Isolate *isolate, const Local<Object> &obj) {
Local<FunctionTemplate> clazz = clazz_template.Get(isolate);
if (clazz.IsEmpty() || !clazz->HasInstance(obj)) return nullptr;
return Unwrap<DispObject>(obj);
}
static IDispatch *GetDispPtr(Isolate *isolate, const Local<Object> &obj) {
DispObject *self = GetPtr(isolate, obj);
return (self && self->disp) ? self->disp->ptr : nullptr;
}
static bool GetValueOf(Isolate *isolate, const Local<Object> &obj, VARIANT &value) {
DispObject *self = GetPtr(isolate, obj);
return self && SUCCEEDED(self->valueOf(isolate, value, false));
}
static Local<Object> NodeCreate(Isolate *isolate, IDispatch *disp, const std::wstring &name, int opt) {
Local<Object> parent;
DispInfoPtr ptr(new DispInfo(disp, name, opt));
return DispObject::NodeCreate(isolate, parent, ptr, name);
}
private:
static Local<Object> NodeCreate(Isolate *isolate, const Local<Object> &parent, const DispInfoPtr &ptr, const std::wstring &name, DISPID id = DISPID_UNKNOWN, LONG indx = -1, int opt = 0);
static void NodeCreate(const FunctionCallbackInfo<Value> &args);
static void NodeValueOf(const FunctionCallbackInfo<Value> &args);
static void NodeToString(const FunctionCallbackInfo<Value> &args);
static void NodeRelease(const FunctionCallbackInfo<Value> &args);
static void NodeCast(const FunctionCallbackInfo<Value> &args);
static void NodeGet(Local<Name> name, const PropertyCallbackInfoGetter &args);
static void NodeSet(Local<Name> name, Local<Value> value, const PropertyCallbackInfoSetter &args);
static void NodeGetByIndex(uint32_t index, const PropertyCallbackInfoGetter&args);
static void NodeSetByIndex(uint32_t index, Local<Value> value, const PropertyCallbackInfoSetter &args);
static void NodeCall(const FunctionCallbackInfo<Value> &args);
#ifdef NODE_INTERCEPTED
static inline Intercepted InterceptedNodeGet(Local<Name> name, const PropertyCallbackInfoGetter& args) {
NodeGet(name, args);
return Intercepted::kYes;
}
static inline Intercepted InterceptedNodeSet(Local<Name> name, Local<Value> value, const PropertyCallbackInfoSetter& args) {
NodeSet(name, value, args);
return Intercepted::kYes;
}
static inline Intercepted InterceptedNodeGetByIndex(uint32_t index, const PropertyCallbackInfoGetter& args) {
NodeGetByIndex(index, args);
return Intercepted::kYes;
}
static inline Intercepted InterceptedNodeSetByIndex(uint32_t index, Local<Value> value, const PropertyCallbackInfoSetter& args) {
NodeSetByIndex(index, value, args);
return Intercepted::kYes;
}
#endif
static void NodeConnectionPoints(const FunctionCallbackInfo<Value> &args);
static void PeakAndDispatchMessages(const FunctionCallbackInfo<Value> &args);
protected:
bool release();
bool get(LPOLESTR tag, LONG index, const PropertyCallbackInfoGetter &args);
bool set(LPOLESTR tag, LONG index, const Local<Value> &value, const PropertyCallbackInfoSetter &args);
void call(Isolate *isolate, const FunctionCallbackInfo<Value> &args);
HRESULT valueOf(Isolate *isolate, VARIANT &value, bool simple);
HRESULT valueOf(Isolate *isolate, const Local<Object> &self, Local<Value> &value);
void toString(const FunctionCallbackInfo<Value> &args);
Local<Value> getIdentity(Isolate *isolate);
private:
int options;
inline bool is_null() { return !disp; }
inline bool is_prepared() { return (options & option_prepared) != 0; }
inline bool is_object() { return dispid == DISPID_VALUE /*&& index < 0*/; }
inline bool is_owned() { return (options & option_owned) != 0; }
Persistent<Value> items, methods, vars;
void initTypeInfo(Isolate *isolate);
DispInfoPtr disp;
std::wstring name;
DISPID dispid;
LONG index;
HRESULT prepare();
};
class VariantObject : public NodeObject
{
public:
VariantObject() {};
VariantObject(const VARIANT &v) : value(v) {};
VariantObject(const FunctionCallbackInfo<Value> &args);
static Persistent<ObjectTemplate> inst_template;
static Persistent<FunctionTemplate> clazz_template;
static NodeMethods clazz_methods;
static void NodeInit(const Local<Object> &target, Isolate* isolate, Local<Context> &ctx);
static bool HasInstance(Isolate *isolate, const Local<Value> &obj) {
Local<FunctionTemplate> clazz = clazz_template.Get(isolate);
return !clazz.IsEmpty() && clazz->HasInstance(obj);
}
static VariantObject *GetInstanceOf(Isolate *isolate, const Local<Object> &obj) {
Local<FunctionTemplate> clazz = clazz_template.Get(isolate);
if (clazz.IsEmpty() || !clazz->HasInstance(obj)) return nullptr;
return Unwrap<VariantObject>(obj);
}
static bool GetValueOf(Isolate *isolate, const Local<Object> &obj, VARIANT &value) {
Local<FunctionTemplate> clazz = clazz_template.Get(isolate);
if (clazz.IsEmpty() || !clazz->HasInstance(obj)) return false;
VariantObject *self = Unwrap<VariantObject>(obj);
return self && SUCCEEDED(self->value.CopyTo(&value));
}
static Local<Object> NodeCreateInstance(const FunctionCallbackInfo<Value> &args);
static void NodeCreate(const FunctionCallbackInfo<Value> &args);
static Local<Object> NodeCreate(Isolate* isolate, const VARIANT& var);
static void NodeClear(const FunctionCallbackInfo<Value> &args);
static void NodeAssign(const FunctionCallbackInfo<Value> &args);
static void NodeCast(const FunctionCallbackInfo<Value> &args);
static void NodeValueOf(const FunctionCallbackInfo<Value> &args);
static void NodeToString(const FunctionCallbackInfo<Value> &args);
static void NodeGet(Local<Name> name, const PropertyCallbackInfoGetter &args);
static void NodeSet(Local<Name> name, Local<Value> value, const PropertyCallbackInfoSetter&args);
static void NodeGetByIndex(uint32_t index, const PropertyCallbackInfoGetter&args);
static void NodeSetByIndex(uint32_t index, Local<Value> value, const PropertyCallbackInfoSetter&args);
#ifdef NODE_INTERCEPTED
static inline Intercepted InterceptedNodeGet(Local<Name> name, const PropertyCallbackInfoGetter& args) {
NodeGet(name, args);
return Intercepted::kYes;
}
static inline Intercepted InterceptedNodeSet(Local<Name> name, Local<Value> value, const PropertyCallbackInfoSetter& args) {
NodeSet(name, value, args);
return Intercepted::kYes;
}
static inline Intercepted InterceptedNodeGetByIndex(uint32_t index, const PropertyCallbackInfoGetter& args) {
NodeGetByIndex(index, args);
return Intercepted::kYes;
}
static inline Intercepted InterceptedNodeSetByIndex(uint32_t index, Local<Value> value, const PropertyCallbackInfoSetter& args) {
NodeSetByIndex(index, value, args);
return Intercepted::kYes;
}
#endif
private:
CComVariant value, pvalue;
bool assign(Isolate *isolate, Local<Value> &val, Local<Value> &type);
};
class ConnectionPointObject : public NodeObject
{
public:
ConnectionPointObject(IConnectionPoint *p, IDispatch* d);
ConnectionPointObject(const FunctionCallbackInfo<Value> &args) {}
static Persistent<ObjectTemplate> inst_template;
static Persistent<FunctionTemplate> clazz_template;
static Local<Object> NodeCreateInstance(Isolate *isolate, IConnectionPoint *p, IDispatch* d);
static void NodeInit(const Local<Object> &target, Isolate* isolate, Local<Context> &ctx);
static void NodeCreate(const FunctionCallbackInfo<Value> &args);
static void NodeAdvise(const FunctionCallbackInfo<Value> &args);
static void NodeUnadvise(const FunctionCallbackInfo<Value> &args);
static void NodeConnectionPointMethods(const FunctionCallbackInfo<Value> &args);
private:
bool InitIndex();
CComPtr<IConnectionPoint> ptr;
CComPtr<IDispatch> disp;
DispObjectImpl::index_t index;
std::unordered_set<DWORD> cookies;
};