FreeRDP
Loading...
Searching...
No Matches
com.h
Go to the documentation of this file.
1
19
20#ifndef WINPR_COM_H
21#define WINPR_COM_H
22
23#include <string.h>
24
25#include <winpr/platform.h>
26#include <winpr/winpr.h>
27#include <winpr/wtypes.h>
28#include <winpr/error.h>
29
77
79#if defined(_WIN32)
80#define WINPR_COM_CALL __stdcall
81#else
82#define WINPR_COM_CALL
83#endif
84
86#if defined(__cplusplus) && !defined(WINPR_COM_CINTERFACE)
87#define WINPR_COM_CXX
88#endif
89
92#ifdef __cplusplus
93#define WINPR_COM_CAST(t, val) reinterpret_cast<t>(val)
94#else
95#define WINPR_COM_CAST(t, val) ((t)(val))
96#endif
97
101#define WINPR_COM_CXX_INTERFACE(name) \
102 protected: \
103 name() = default; \
104 ~name() = default; \
105 \
106 public: \
107 name(const name&) = delete; \
108 name(name&&) = delete; \
109 name& operator=(const name&) = delete; \
110 name& operator=(name&&) = delete;
111
112#ifdef __cplusplus
113extern "C"
114{
115#endif
116
117 typedef struct wIUnknown wIUnknown;
118
120 typedef struct
121 {
122 HRESULT(WINPR_COM_CALL* QueryInterface)
123 (wIUnknown* This, const IID* riid, void** ppvObject);
124 ULONG(WINPR_COM_CALL* AddRef)(wIUnknown* This);
125 ULONG(WINPR_COM_CALL* Release)(wIUnknown* This);
127
128#ifdef __cplusplus
129}
130#endif
131
132#ifdef WINPR_COM_CXX
134struct wIUnknown
135{
136 virtual HRESULT WINPR_COM_CALL QueryInterface(const IID* riid, void** ppvObject) = 0;
137 virtual ULONG WINPR_COM_CALL AddRef() = 0;
138 virtual ULONG WINPR_COM_CALL Release() = 0;
139
140 WINPR_COM_CXX_INTERFACE(wIUnknown)
141};
142#endif
143
144#ifndef WINPR_COM_CXX
147{
148 const wIUnknownVtbl* lpVtbl;
149};
150#endif
151
152#ifdef __cplusplus
153extern "C"
154{
155#endif
156
158#define WINPR_COM_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
159 static const GUID name = { l, w1, w2, { b1, b2, b3, b4, b5, b6, b7, b8 } }
160
162 WINPR_COM_DEFINE_GUID(WINPR_IID_IUnknown, 0x00000000, 0x0000, 0x0000, 0xc0, 0x00, 0x00, 0x00,
163 0x00, 0x00, 0x00, 0x46);
164
166 static inline BOOL winpr_com_guid_equal(const GUID* a, const GUID* b)
167 {
168 return memcmp(a, b, sizeof(*a)) == 0;
169 }
170
173#define WINPR_COM_GUID_FMT \
174 "{%08" PRIX32 "-%04" PRIX16 "-%04" PRIX16 "-%02" PRIX8 "%02" PRIX8 "-%02" PRIX8 "%02" PRIX8 \
175 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "}"
176#define WINPR_COM_GUID_ARGS(g) \
177 (UINT32)(g)->Data1, (UINT16)(g)->Data2, (UINT16)(g)->Data3, (g)->Data4[0], (g)->Data4[1], \
178 (g)->Data4[2], (g)->Data4[3], (g)->Data4[4], (g)->Data4[5], (g)->Data4[6], (g)->Data4[7]
179
183#ifdef WINPR_COM_CXX
184#define WINPR_COM_QUERY_INTERFACE(obj, iid, ppv) \
185 ((obj)->QueryInterface((iid), WINPR_COM_CAST(void**, ppv)))
186#define WINPR_COM_ADDREF(obj) ((obj)->AddRef())
187#define WINPR_COM_RELEASE(obj) ((obj)->Release())
188#else
189#define WINPR_COM_IUNKNOWN_VTBL(obj) WINPR_COM_CAST(const wIUnknownVtbl*, (obj)->lpVtbl)
190
191#define WINPR_COM_QUERY_INTERFACE(obj, iid, ppv) \
192 (WINPR_COM_IUNKNOWN_VTBL(obj)->QueryInterface(WINPR_COM_CAST(wIUnknown*, obj), (iid), \
193 WINPR_COM_CAST(void**, ppv)))
194#define WINPR_COM_ADDREF(obj) \
195 (WINPR_COM_IUNKNOWN_VTBL(obj)->AddRef(WINPR_COM_CAST(wIUnknown*, obj)))
196#define WINPR_COM_RELEASE(obj) \
197 (WINPR_COM_IUNKNOWN_VTBL(obj)->Release(WINPR_COM_CAST(wIUnknown*, obj)))
198#endif
199
200 /* ---- implementing COM objects in C (or in C++ with WINPR_COM_CINTERFACE) ----
201 *
202 * An object starts with a wComBase, whose first member is lpVtbl: a pointer to the object is
203 * the pointer to its first interface, and methods cast This back to the object type.
204 *
205 * typedef struct {
206 * wComBase com; // IWTSFooService
207 * ...state...
208 * } CFooService;
209 *
210 * WINPR_COM_IUNKNOWN_THUNKS(CFooService, &IID_IWTSFooService)
211 * static const IWTSFooServiceVtbl vtbl = {
212 * .iunknown = WINPR_COM_IUNKNOWN_ENTRIES(CFooService),
213 * .DoSomething = CFooService_DoSomething,
214 * };
215 * ...
216 * winpr_com_base_init(&foo->com, "CFooService", &vtbl.iunknown, CFooService_free);
217 * return (IWTSFooService*)foo;
218 *
219 * WINPR_COM_IUNKNOWN_THUNKS answers QueryInterface for IUnknown and one interface. An object
220 * with several interfaces uses WINPR_COM_REFCOUNT_THUNKS and writes its own QueryInterface:
221 * each interface not derived from the first one needs its own vtable pointer in the object,
222 * QueryInterface hands out the address of that member, and the methods of that interface get
223 * the object back from it. QueryInterface(IID_IUnknown) must always give the same pointer.
224 */
225
226 typedef struct s_wComBase wComBase;
227
230 typedef void (*pfnwComBaseFree)(wComBase* object);
231
233 {
234 const wIUnknownVtbl* lpVtbl; /* the object's first interface, must stay first */
235 volatile LONG refCount;
236 const char* className; /* for logs */
237 pfnwComBaseFree Free;
238 };
239
243 WINPR_API void winpr_com_base_init(wComBase* object, const char* className,
244 const wIUnknownVtbl* vtbl, pfnwComBaseFree Free);
245
248 WINPR_API ULONG winpr_com_base_add_ref(wComBase* object);
249
252 WINPR_API ULONG winpr_com_base_release(wComBase* object);
253
257 WINPR_API HRESULT winpr_com_base_query(wComBase* object, const IID* iid, const IID* riid,
258 void** ppv);
259
260/* The IUnknown entries of a vtable, as static functions of the including file: a vtable is then
261 * a constant initializer (the address of a function imported from a DLL is not one for MSVC). */
262
264#define WINPR_COM_REFCOUNT_THUNKS(prefix) \
265 static ULONG WINPR_COM_CALL prefix##_AddRef(wIUnknown* This) \
266 { \
267 return winpr_com_base_add_ref(WINPR_COM_CAST(wComBase*, This)); \
268 } \
269 static ULONG WINPR_COM_CALL prefix##_Release(wIUnknown* This) \
270 { \
271 return winpr_com_base_release(WINPR_COM_CAST(wComBase*, This)); \
272 }
273
276#define WINPR_COM_IUNKNOWN_THUNKS(prefix, iid) \
277 static HRESULT WINPR_COM_CALL prefix##_QueryInterface(wIUnknown* This, const IID* riid, \
278 void** ppvObject) \
279 { \
280 return winpr_com_base_query(WINPR_COM_CAST(wComBase*, This), (iid), riid, ppvObject); \
281 } \
282 WINPR_COM_REFCOUNT_THUNKS(prefix)
283
286#define WINPR_COM_IUNKNOWN_ENTRIES(prefix) \
287 { prefix##_QueryInterface, prefix##_AddRef, prefix##_Release }
288
289#ifdef __cplusplus
290}
291#endif
292
293#endif /* WINPR_COM_H */
void(* pfnwComBaseFree)(wComBase *object)
called when the last reference goes away: releases what the object holds and frees it
Definition com.h:230
static BOOL winpr_com_guid_equal(const GUID *a, const GUID *b)
IsEqualGUID.
Definition com.h:166
#define WINPR_COM_DEFINE_GUID(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8)
defines a GUID constant visible in the including file only (nothing to link)
Definition com.h:158
WINPR_API void winpr_com_base_init(wComBase *object, const char *className, const wIUnknownVtbl *vtbl, pfnwComBaseFree Free)
sets the vtable and the reference count to 1, the caller owns that first reference.
Definition com.c:30
WINPR_API ULONG winpr_com_base_add_ref(wComBase *object)
AddRef of an object starting with a wComBase.
Definition com.c:43
#define WINPR_COM_CXX_INTERFACE(name)
ends the declaration of a C++ interface name: only an implementation creates it and Release() frees i...
Definition com.h:101
#define WINPR_COM_CALL
the calling convention of COM methods (STDMETHODCALLTYPE)
Definition com.h:82
WINPR_API HRESULT winpr_com_base_query(wComBase *object, const IID *iid, const IID *riid, void **ppv)
QueryInterface of an object whose only interface is iid (nullptr: IUnknown only). On success *ppv is ...
Definition com.c:63
WINPR_API ULONG winpr_com_base_release(wComBase *object)
Release of an object starting with a wComBase: calls its Free at 0.
Definition com.c:51
IUnknown, C form.
Definition com.h:147
the vtable of IUnknown, the first entries of every COM vtable
Definition com.h:121