FreeRDP
Loading...
Searching...
No Matches
com.c
1
20#include <winpr/config.h>
21
22#include <winpr/assert.h>
23#include <winpr/interlocked.h>
24#include <winpr/wlog.h>
25#include <winpr/com.h>
26
27#include "../log.h"
28#define TAG WINPR_TAG("utils.com")
29
30void winpr_com_base_init(wComBase* object, const char* className, const wIUnknownVtbl* vtbl,
31 pfnwComBaseFree Free)
32{
33 WINPR_ASSERT(object);
34 WINPR_ASSERT(vtbl);
35 WINPR_ASSERT(Free);
36
37 object->lpVtbl = vtbl;
38 object->refCount = 1;
39 object->className = className ? className : "wComBase";
40 object->Free = Free;
41}
42
43ULONG winpr_com_base_add_ref(wComBase* object)
44{
45 WINPR_ASSERT(object);
46 const LONG count = InterlockedIncrement(&object->refCount);
47 WLog_VRB(TAG, "%s::AddRef() -> %" PRId32, object->className, (INT32)count);
48 return (ULONG)count;
49}
50
51ULONG winpr_com_base_release(wComBase* object)
52{
53 WINPR_ASSERT(object);
54 const LONG count = InterlockedDecrement(&object->refCount);
55 WLog_VRB(TAG, "%s::Release() -> %" PRId32, object->className, (INT32)count);
56 if (count == 0)
57 object->Free(object);
58 else if (count < 0)
59 WLog_ERR(TAG, "%s: released more often than referenced", object->className);
60 return (ULONG)count;
61}
62
63HRESULT winpr_com_base_query(wComBase* object, const IID* iid, const IID* riid, void** ppv)
64{
65 WINPR_ASSERT(object);
66
67 if (!ppv)
68 return E_POINTER;
69 *ppv = nullptr;
70 if (!riid)
71 return E_POINTER;
72
73 if (!winpr_com_guid_equal(riid, &WINPR_IID_IUnknown) &&
74 (!iid || !winpr_com_guid_equal(riid, iid)))
75 {
76 WLog_DBG(TAG, "%s::QueryInterface(" WINPR_COM_GUID_FMT ") -> E_NOINTERFACE",
77 object->className, WINPR_COM_GUID_ARGS(riid));
78 return E_NOINTERFACE;
79 }
80
81 (void)winpr_com_base_add_ref(object);
82 *ppv = object;
83 return S_OK;
84}
#define WINPR_COM_GUID_FMT
printf a GUID as {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}: WLog_DBG(TAG, "id=" WINPR_COM_GUID_FMT,...
Definition com.h:137
void(* pfnwComBaseFree)(wComBase *object)
called when the last reference goes away: releases what the object holds and frees it
Definition com.h:185
static BOOL winpr_com_guid_equal(const GUID *a, const GUID *b)
IsEqualGUID.
Definition com.h:130
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
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
the vtable of IUnknown, the first entries of every COM vtable
Definition com.h:85