-
Notifications
You must be signed in to change notification settings - Fork 0
/
dll.cpp
133 lines (114 loc) · 2.65 KB
/
dll.cpp
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
#include "dll.h"
#include "ExplorerExtension.h"
#include <atomic>
#include <ctime>
#include <cstdlib>
std::atomic<long> g_refModule = 0;
// Standard DLL functions
STDAPI_(BOOL) DllMain(HINSTANCE hInstance, DWORD dwReason, void *)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
DisableThreadLibraryCalls(hInstance);
}
return TRUE;
}
STDAPI DllCanUnloadNow()
{
// Only allow the DLL to be unloaded after all outstanding references have been released
return (g_refModule == 0) ? S_OK : S_FALSE;
}
void DllAddRef()
{
g_refModule++;
}
void DllRelease()
{
g_refModule--;
}
class CClassFactory : public IClassFactory
{
public:
static HRESULT CreateInstance(REFCLSID clsid, /*const CLASS_OBJECT_INIT *pClassObjectInits, size_t cClassObjectInits,*/ REFIID riid, void **ppv)
{
*ppv = NULL;
HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
if (clsid == __uuidof(ExplorerExtension))
{
IClassFactory *pClassFactory = new (std::nothrow) CClassFactory();
hr = pClassFactory ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
hr = pClassFactory->QueryInterface(riid, ppv);
pClassFactory->Release();
}
}
return hr;
}
CClassFactory() : m_refCount(1)
{
DllAddRef();
}
// IUnknown
IFACEMETHODIMP QueryInterface(REFIID riid, void ** ppv)
{
static const QITAB qit[] =
{
QITABENT(CClassFactory, IClassFactory),
{ 0 }
};
return QISearch(this, qit, riid, ppv);
}
IFACEMETHODIMP_(ULONG) AddRef()
{
return ++m_refCount;
}
IFACEMETHODIMP_(ULONG) Release()
{
long cRef = --m_refCount;
if (cRef == 0)
{
delete this;
}
return cRef;
}
// IClassFactory
IFACEMETHODIMP CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
{
return punkOuter ? CLASS_E_NOAGGREGATION : ExplorerExtension::CreateInstance(riid, ppv);
}
IFACEMETHODIMP LockServer(BOOL fLock)
{
if (fLock)
{
DllAddRef();
}
else
{
DllRelease();
}
return S_OK;
}
private:
~CClassFactory()
{
DllRelease();
}
std::atomic<long> m_refCount;
};
STDAPI DllGetClassObject(REFCLSID clsid, REFIID riid, void **ppv)
{
return CClassFactory::CreateInstance(clsid, /*c_rgClassObjectInit, ARRAYSIZE(c_rgClassObjectInit),*/ riid, ppv);
}
HRESULT RegisterUnregister(bool fRegister)
{
return ExplorerExtension::RegisterUnRegister(fRegister);
}
STDAPI DllRegisterServer()
{
return RegisterUnregister(true);
}
STDAPI DllUnregisterServer()
{
return RegisterUnregister(false);
}