-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCOMBase.h
284 lines (229 loc) · 11.6 KB
/
COMBase.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
#ifndef COMBASE_INCLUDED
#define COMBASE_INCLUDED
/*
Code required to manage COM class/object creation from C code.
-- Wim Decelle
*/
#include "Features.h"
#include "LinkedList.h"
#include "COMInits.h"
#include "BinaryTree.h"
#define _WIN32_DCOM
#include <objbase.h>
#include <OCIdl.h>
#include <Oleauto.h>
#include <olectl.h>
typedef struct COMObjectStruct COMObject;
typedef void (*Structor)(COMObject * object);
typedef struct COMClassStruct
{
char * name;
GUID * clsid;
unsigned int default_iid;
unsigned int iids_count;
GUID ** iids;
GUID * events_iid;//0 if no events
unsigned int size;
void * lpVtbl;
Structor constructor;
Structor destructor;
ITypeInfo * typeinfo;
int register_as_local_server;
COMObject * factory;
IUnknown * activeobject;
BinaryTree * instances;
} COMClass;
typedef struct COMObjectStruct
{
void * lpVtbl;
unsigned int refcount;
struct COMObjectStruct * parent;//if parent, then addref/release is done by the parent
COMClass * _class;
struct COMObjectStruct * _CPC;
BinaryTree * weakreferences;
} COMObject;
typedef struct WeakReferenceStruct
{
COMObject * to;
unsigned int isValid;
} WeakReference;
WeakReference * CreateWeakReference(COMObject * to);
void DeleteWeakReference(WeakReference * wr);
void InternalAddRef();
void InternalRelease();
typedef struct COMConfigStruct
{
char ServerName[256];
char TypeLibFile[256];
GUID * AppId;
//int isLocalServer;
} COMConfig;
COMConfig * GetCOMBaseConfigurations();
//construction/destruction
void DestructObject(COMObject * todestruct);
COMObject * ConstructObject(COMClass * fromclass);
//default IUnknown implementation
HRESULT STDMETHODCALLTYPE DefaultQueryInterface(COMObject * pThis,REFIID riid, COMObject ** ppv);
ULONG STDMETHODCALLTYPE DefaultAddRef(COMObject * pThis);
ULONG STDMETHODCALLTYPE DefaultRelease(COMObject * pThis);
typedef HRESULT (STDMETHODCALLTYPE * pQueryInterface)(IUnknown *, REFIID riid, void **);
typedef HRESULT (STDMETHODCALLTYPE * pAddRef)(IUnknown *);
typedef HRESULT (STDMETHODCALLTYPE * pRelease)(IUnknown *);
#define DEFAULT_UNKNOWN\
pQueryInterface QueryInterface;\
pAddRef AddRef\
pRelease Release
#define DEFAULT_UNKNOWN_VTBL (pQueryInterface)DefaultQueryInterface, (pAddRef)DefaultAddRef, (pRelease)DefaultRelease
//default class factory implementation
HRESULT STDMETHODCALLTYPE DefaultClassCreateInstance(COMObject * pThis,IUnknown * punkOuter, REFIID vTableGuid, COMObject ** ppv);
HRESULT STDMETHODCALLTYPE DefaultClassLockServer(COMObject * pThis,BOOL flock);
typedef HRESULT (STDMETHODCALLTYPE * pClassQueryInterface)(IClassFactory *, REFIID riid, void **);
typedef HRESULT (STDMETHODCALLTYPE * pClassAddRef)(IClassFactory *);
typedef HRESULT (STDMETHODCALLTYPE * pClassRelease)(IClassFactory *);
typedef HRESULT (STDMETHODCALLTYPE * pClassCreateInstance)(IClassFactory *,IUnknown *, REFIID, void ** ppv);
typedef HRESULT (STDMETHODCALLTYPE * pClassLockServer)(IClassFactory * pThis,BOOL flock);
//default dispatch implementation
HRESULT STDMETHODCALLTYPE DefaultGetTypeInfoCount(COMObject * pThis, UINT * pCount);
HRESULT STDMETHODCALLTYPE DefaultGetTypeInfo(COMObject * pThis, UINT itinfo, LCID lcid, ITypeInfo ** pTypeInfo);
HRESULT STDMETHODCALLTYPE DefaultGetIDsOfNames(COMObject * pThis, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgdispid);
HRESULT STDMETHODCALLTYPE DefaultInvoke(COMObject * pThis, DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *params, VARIANT *result, EXCEPINFO *pexcepinfo, UINT *puArgErr);
typedef HRESULT (STDMETHODCALLTYPE * pDispatchQueryInterface)(IDispatch *, REFIID riid, void **);
typedef HRESULT (STDMETHODCALLTYPE * pDispatchAddRef)(IDispatch *);
typedef HRESULT (STDMETHODCALLTYPE * pDispatchRelease)(IDispatch *);
typedef HRESULT (STDMETHODCALLTYPE * pDispatchGetTypeInfoCount)(IDispatch * pThis, UINT * pCount);
typedef HRESULT (STDMETHODCALLTYPE * pDispatchGetTypeInfo)(IDispatch * pThis, UINT itinfo, LCID lcid, ITypeInfo ** pTypeInfo);
typedef HRESULT (STDMETHODCALLTYPE * pDispatchGetIDsOfNames)(IDispatch * pThis, REFIID riid, LPOLESTR * rgszNames, UINT cNames, LCID lcid, DISPID * rgdispid);
typedef HRESULT (STDMETHODCALLTYPE * pDispatchInvoke)(IDispatch * pThis, DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *params, VARIANT *result, EXCEPINFO *pexcepinfo, UINT *puArgErr);
#define DEFAULT_DISPATCH\
pDispatchQueryInterface QueryInterface;\
pDispatchAddRef AddRef;\
pDispatchRelease Release;\
pDispatchGetTypeInfoCount GetTypeInfoCount;\
pDispatchGetTypeInfo GetTypeInfo;\
pDispatchGetIDsOfNames GetIDsOfNames;\
pDispatchInvoke Invoke
#define DEFAULT_DISPATCH_VTBL (pDispatchQueryInterface)DefaultQueryInterface, (pDispatchAddRef)DefaultAddRef, (pDispatchRelease)DefaultRelease, (pDispatchGetTypeInfoCount)DefaultGetTypeInfoCount, (pDispatchGetTypeInfo)DefaultGetTypeInfo, (pDispatchGetIDsOfNames)DefaultGetIDsOfNames, (pDispatchInvoke)DefaultInvoke
unsigned int olestrlen(LPOLESTR str);
char * ole2char(LPOLESTR torewrite);
short * char2ole(char * toconvert);
BSTR char2bstr(char * toconvert);
void AddCOMClass(COMClass * toregister);
void SetActiveObject(COMClass * _class, IUnknown * activeobject);
BinaryTree * GetKnownClasses();
HRESULT PASCAL DllRegisterServer();
HRESULT PASCAL DllUnregisterServer();
HRESULT PASCAL DllGetClassObject(REFCLSID objGuid, REFIID factoryGuid, void **factoryHandle);
HRESULT PASCAL DllCanUnloadNow(void);
typedef struct ClassFactoryStruct
{
COMObject header;
COMClass * forclass;
} ClassFactory;
COMObject * CreateFactory(COMClass * forclass);
//LinkedListEnumerator
typedef struct LinkedListEnumeratorStruct
{
COMObject header;
LinkedList * _list;
LinkedListEnum * _llenum;//enumerator
unsigned int datasize;//is data to be copied?
int type;//addref before return?
GUID * _iid;//overload of the default iid
} LinkedListEnumerator;
HRESULT STDMETHODCALLTYPE LLQueryInterface(COMObject * pThis,REFIID riid, COMObject ** ppv);
HRESULT STDMETHODCALLTYPE LLNext(COMObject * pThis, ULONG count, void ** arr_out, unsigned int * _count);
HRESULT STDMETHODCALLTYPE LLSkip(COMObject * pThis, ULONG count);
HRESULT STDMETHODCALLTYPE LLReset(COMObject * pThis);
HRESULT STDMETHODCALLTYPE LLClone(COMObject * pThis, void ** ppenum);
typedef struct ILinkedListEnumeratorVtblstruct
{
HRESULT (STDMETHODCALLTYPE * LLQueryInterface)(COMObject * pThis,REFIID riid, COMObject ** ppv);
ULONG (STDMETHODCALLTYPE * DefaultAddRef)(COMObject * pThis);
ULONG (STDMETHODCALLTYPE * DefaultRelease)(COMObject * pThis);
HRESULT (STDMETHODCALLTYPE * LLNext)(COMObject * pThis, ULONG count, void ** arr_out, unsigned int * _count);
HRESULT (STDMETHODCALLTYPE * LLSkip)(COMObject * pThis, ULONG count);
HRESULT (STDMETHODCALLTYPE * LLReset)(COMObject * pThis);
HRESULT (STDMETHODCALLTYPE * LLClone)(COMObject * pThis, void ** ppenum);
} ILinkedListEnumeratorVtbl;
COMObject * CreateLinkedListEnumerator(LinkedList * list, LinkedListEnum * llenum, unsigned int datasize, int type, GUID * iid);
//ConnectionPointContainer and ConnectionPoint (automation events)
typedef struct ConnectionPointStruct ConnectionPoint;
typedef struct ConnectionPointContainerStruct
{
COMObject header;
ConnectionPoint * _cp;
} ConnectionPointContainer;
typedef struct ConnectionPointContainerVtblStruct
{
pQueryInterface QueryInterface;
pAddRef AddRef;
pRelease Release;
HRESULT (STDMETHODCALLTYPE * EnumConnectionPoints)(ConnectionPointContainer * pThis,COMObject ** ppEnum);
HRESULT (STDMETHODCALLTYPE * FindConnectionPoint)(ConnectionPointContainer * pThis,REFIID riid,ConnectionPoint ** ppCP);
} ConnectionPointContainerVtbl;
HRESULT STDMETHODCALLTYPE EnumConnectionPoints(ConnectionPointContainer * pThis,COMObject ** ppEnum);
HRESULT STDMETHODCALLTYPE FindConnectionPoint(ConnectionPointContainer * pThis,REFIID riid,ConnectionPoint ** ppCP);
void ConnectionPointContainer_constructor(ConnectionPointContainer * pThis);
void ConnectionPointContainer_destructor(ConnectionPointContainer * pThis);
typedef struct CONNECTDATA_EX_struct
{
CONNECTDATA data;
BinaryTree * dispids;//original dispid -> actual dispid (<- don't know why, but C# COM wrappers generated by the interop code
//don't stick to the DISPIDs specified in the typelibrary...
//so we can't invoke events using dispids, but have to use names...
//by looking up the name->DISPID pair once whenever a sink get's advised...
//and storing "expected dispid -> actual dispid" pairs here, code should still run smoothely
} CONNECTDATA_EX;
typedef struct ConnectionPointStruct
{
COMObject header;
WeakReference * _cpc;
GUID * _iid;
BinaryTree * _connections;//CONNECTDATA structures
BinaryTree * name2dispid;//name->dispid from the event guids typeinfo
} ConnectionPoint;
typedef struct ConnectionPointVtblStruct
{
pQueryInterface QueryInterface;
pAddRef AddRef;
pRelease Release;
HRESULT (STDMETHODCALLTYPE * GetConnectionInterface)(ConnectionPoint * pThis,IID * pIID);
HRESULT (STDMETHODCALLTYPE * GetConnectionPointContainer)(ConnectionPoint * pThis,ConnectionPointContainer ** ppCPC);
HRESULT (STDMETHODCALLTYPE * Advise)(ConnectionPoint * pThis,IUnknown * pUnk,DWORD * pdwCookie);
HRESULT (STDMETHODCALLTYPE * Unadvise)(ConnectionPoint * pThis,DWORD dwCookie);
HRESULT (STDMETHODCALLTYPE * EnumConnections)(ConnectionPoint * pThis,COMObject ** ppEnum);
} ConnectionPointVtbl;
HRESULT STDMETHODCALLTYPE GetConnectionInterface(ConnectionPoint * pThis,IID * pIID);
HRESULT STDMETHODCALLTYPE GetConnectionPointContainer(ConnectionPoint * pThis,ConnectionPointContainer ** ppCPC);
HRESULT STDMETHODCALLTYPE Advise(ConnectionPoint * pThis,IUnknown * pUnk,DWORD * pdwCookie);
HRESULT STDMETHODCALLTYPE Unadvise(ConnectionPoint * pThis,DWORD dwCookie);
HRESULT STDMETHODCALLTYPE EnumConnections(ConnectionPoint * pThis,COMObject ** ppEnum);
void ConnectionPoint_constructor(ConnectionPoint * pThis);
void ConnectionPoint_destructor(ConnectionPoint * pThis);
COMObject * CreateConnectionPointContainer(COMObject * forobject);
COMObject * CreateConnectionPoint(GUID * outgoing_iid, ConnectionPointContainer * cpc);
HRESULT InvokeEvent(COMObject * onobject, DISPID eventid, DISPPARAMS * pars, VARIANT * result);
VARIANT * VByteArray(unsigned char * array, unsigned int size);
VARIANT * VInt(int value);
VARIANT * VString(char * str);
VARIANT * VObject(IUnknown * pDisp);
VARIANT * VDispObject(IDispatch * pUnk);
void DispPush(DISPPARAMS * dispstack, VARIANT * arg);
DISPPARAMS * DispStack(unsigned int stacksize);
COMObject * CreateObject(GUID * clsid);
#define COMCALL HRESULT STDMETHODCALLTYPE
#define pCOMCALL(name) HRESULT (STDMETHODCALLTYPE * name)
#define TO_VARIANT_BOOL(a) (a==0?VARIANT_FALSE:VARIANT_TRUE)
#define FROM_VARIANT_BOOL(a) (a==VARIANT_FALSE?0:1)
COMClass * FindClass(GUID * clsid);
HRESULT DoInvoke(IUnknown * onobject, DISPID methodid, DISPPARAMS * pars, VARIANT * result);
HRESULT DoPropGet(IUnknown * onobject, DISPID propid, DISPPARAMS * pars, VARIANT * result);
HRESULT DoPropSet(IUnknown * onobject, DISPID propid, DISPPARAMS * pars, VARIANT * result);
int InitializeCOM();//don't call this unless you know why... inits are typically done automatically from DllGetClassObject
//... however i need it to setup COM after injection (when the dll wasn't loaded through a CoCreateInstance call)
int doUnadvise(IUnknown * onobject, GUID * callbackiid, unsigned int cookie);
int doAdvise(IUnknown * onobject, GUID * callbackiid, IUnknown * callbackinterface, unsigned int * cookie);
void InternalLock();
void InternalUnlock();
void ForceUnload();
#endif