-
Notifications
You must be signed in to change notification settings - Fork 21
/
Dll.cpp
169 lines (148 loc) · 3.86 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
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
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Standard dll required functions and class factory implementation.
#include <windows.h>
#include <unknwn.h>
#include "Dll.h"
#include "helpers.h"
static long g_cRef = 0; // global dll reference count
HINSTANCE g_hinst = NULL; // global dll hinstance
extern HRESULT CSample_CreateInstance(__in REFIID riid, __deref_out void** ppv);
extern HRESULT CLMSFilter_CreateInstance(__in REFIID riid, __deref_out void** ppv);
EXTERN_C GUID CLSID_CSample;
class CClassFactory : public IClassFactory
{
public:
CClassFactory() : _cRef(1)
{
}
// IUnknown
IFACEMETHODIMP QueryInterface(__in REFIID riid, __deref_out void **ppv)
{
static const QITAB qit[] =
{
QITABENT(CClassFactory, IClassFactory),
{ 0 },
};
return QISearch(this, qit, riid, ppv);
}
IFACEMETHODIMP_(ULONG) AddRef()
{
return InterlockedIncrement(&_cRef);
}
IFACEMETHODIMP_(ULONG) Release()
{
long cRef = InterlockedDecrement(&_cRef);
if (!cRef)
delete this;
return cRef;
}
// IClassFactory
IFACEMETHODIMP CreateInstance(__in IUnknown* pUnkOuter, __in REFIID riid, __deref_out void **ppv)
{
HRESULT hr;
if (!pUnkOuter)
{
//hr = CSample_CreateInstance(riid, ppv);
if (IID_ICredentialProvider == riid) {
if (DEVELOPING) PrintLn("invoke IID_ICredentialProvider");
hr = CSample_CreateInstance(riid, ppv);
}
else if (IID_ICredentialProviderFilter == riid) {
if (DEVELOPING) PrintLn("invoke IID_ICredentialProviderFilter");
hr = CLMSFilter_CreateInstance(riid, ppv);
}
else {
*ppv = NULL;
hr = CLASS_E_NOAGGREGATION;
if (DEVELOPING) PrintLn("invoke unknown object");
}
}
else
{
*ppv = NULL;
hr = CLASS_E_NOAGGREGATION;
}
return hr;
}
IFACEMETHODIMP LockServer(__in BOOL bLock)
{
if (bLock)
{
DllAddRef();
}
else
{
DllRelease();
}
return S_OK;
}
private:
~CClassFactory()
{
}
long _cRef;
};
HRESULT CClassFactory_CreateInstance(__in REFCLSID rclsid, __in REFIID riid, __deref_out void **ppv)
{
*ppv = NULL;
HRESULT hr;
if (CLSID_CSample == rclsid)
{
CClassFactory* pcf = new CClassFactory();
if (pcf)
{
hr = pcf->QueryInterface(riid, ppv);
pcf->Release();
}
else
{
hr = E_OUTOFMEMORY;
}
}
else
{
hr = CLASS_E_CLASSNOTAVAILABLE;
}
return hr;
}
void DllAddRef()
{
if (DEVELOPING) PrintLn("DllAddRef");
InterlockedIncrement(&g_cRef);
}
void DllRelease()
{
if (DEVELOPING) PrintLn("DllRelease");
InterlockedDecrement(&g_cRef);
}
STDAPI DllCanUnloadNow()
{
if (DEVELOPING) PrintLn("DllCanUnloadNow?");
return (g_cRef > 0) ? S_FALSE : S_OK;
}
STDAPI DllGetClassObject(__in REFCLSID rclsid, __in REFIID riid, __deref_out void** ppv)
{
return CClassFactory_CreateInstance(rclsid, riid, ppv);
}
STDAPI_(BOOL) DllMain(__in HINSTANCE hinstDll, __in DWORD dwReason, __in void *)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
DisableThreadLibraryCalls(hinstDll);
break;
case DLL_PROCESS_DETACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
break;
}
g_hinst = hinstDll;
return TRUE;
}