forked from microsoft/PowerToys
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcom_object_factory.h
More file actions
60 lines (53 loc) · 1.24 KB
/
com_object_factory.h
File metadata and controls
60 lines (53 loc) · 1.24 KB
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
#pragma once
#include <Unknwn.h>
#include <winrt/base.h>
#include <atomic>
template<typename T>
class com_object_factory : public IClassFactory
{
public:
HRESULT __stdcall QueryInterface(const IID& riid, void** ppv) override
{
static const QITAB qit[] = {
QITABENT(com_object_factory, IClassFactory),
{ 0 }
};
return QISearch(this, qit, riid, ppv);
}
ULONG __stdcall AddRef() override
{
return ++_refCount;
}
ULONG __stdcall Release() override
{
LONG refCount = --_refCount;
return refCount;
}
HRESULT __stdcall CreateInstance(IUnknown* punkOuter, const IID& riid, void** ppv)
{
*ppv = nullptr;
HRESULT hr;
if (punkOuter)
{
hr = CLASS_E_NOAGGREGATION;
}
else
{
T* psrm = new (std::nothrow) T();
HRESULT hr = psrm ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
hr = psrm->QueryInterface(riid, ppv);
psrm->Release();
}
return hr;
}
return hr;
}
HRESULT __stdcall LockServer(BOOL)
{
return S_OK;
}
private:
std::atomic<long> _refCount;
};