-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathdllmain.cpp
More file actions
376 lines (310 loc) · 9.35 KB
/
dllmain.cpp
File metadata and controls
376 lines (310 loc) · 9.35 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
//////////////////////////////////////////////////////////////////////////
//
// dllmain.cpp : Implements DLL exports and COM class factory
//
// 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.
//
// Note: This source file implements the class factory for the transform,
// plus the following DLL functions:
// - DllMain
// - DllCanUnloadNow
// - DllRegisterServer
// - DllUnregisterServer
// - DllGetClassObject
//
//////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "common.h"
#include "multipinmft.h"
#include <initguid.h>
#ifdef MF_WPP
#include "dllmain.tmh" //--REF_ANALYZER_DONT_REMOVE--
#endif
//
// The static variable needed to check the object count of the deviceMFts loaded.
//
volatile long CDMFTModuleLifeTimeManager::s_lObjectCount = 0;
HRESULT RegisterObject(HMODULE hModule, REFGUID guid, PCWSTR pszDescription, PCWSTR pszThreadingModel);
HRESULT UnregisterObject(const GUID& guid);
// Module Ref count
long g_cRefModule = 0;
// Handle to the DLL's module
HMODULE g_hModule = NULL;
void DllAddRef()
{
InterlockedIncrement(&g_cRefModule);
}
void DllRelease()
{
InterlockedDecrement(&g_cRefModule);
}
//
// IClassFactory implementation
//
typedef HRESULT (*PFNCREATEINSTANCE)(REFIID riid, void **ppvObject);
struct CLASS_OBJECT_INIT
{
const CLSID *pClsid;
PFNCREATEINSTANCE pfnCreate;
};
// Classes supported by this module:
const CLASS_OBJECT_INIT c_rgClassObjectInit[] =
{
{ &CLSID_MultiPinMFT, MFT_CreateInstance },
};
class CClassFactory : public IClassFactory
{
public:
static HRESULT CreateInstance(
REFCLSID clsid, // The CLSID of the object to create (from DllGetClassObject)
const CLASS_OBJECT_INIT *pClassObjectInits, // Array of class factory data.
size_t cClassObjectInits, // Number of elements in the array.
REFIID riid, // The IID of the interface to retrieve (from DllGetClassObject)
void **ppv // Receives a pointer to the interface.
)
{
*ppv = NULL;
HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
for (size_t i = 0; i < cClassObjectInits; i++)
{
if (clsid == *pClassObjectInits[i].pClsid)
{
IClassFactory *pClassFactory = new (std::nothrow) CClassFactory(pClassObjectInits[i].pfnCreate);
if (pClassFactory)
{
hr = pClassFactory->QueryInterface(riid, ppv);
pClassFactory->Release();
}
else
{
hr = E_OUTOFMEMORY;
}
break; // match found
}
}
return hr;
}
// IUnknown methods
IFACEMETHODIMP QueryInterface(REFIID riid, void ** ppv)
{
#if 0
static const QITAB qit[] =
{
QITABENT(CClassFactory, IClassFactory),
{ 0 }
};
return QISearch(this, qit, riid, ppv);
#else
if (riid == __uuidof(IClassFactory))
{
*ppv = static_cast< IClassFactory* >(this);
AddRef();
}
return S_OK;
#endif
}
IFACEMETHODIMP_(ULONG) AddRef()
{
return InterlockedIncrement(&m_cRef);
}
IFACEMETHODIMP_(ULONG) Release()
{
long cRef = InterlockedDecrement(&m_cRef);
if (cRef == 0)
{
delete this;
}
return cRef;
}
// IClassFactory methods
IFACEMETHODIMP CreateInstance(IUnknown *punkOuter, REFIID riid, void **ppv)
{
return punkOuter ? CLASS_E_NOAGGREGATION : m_pfnCreate(riid, ppv);
}
IFACEMETHODIMP LockServer(BOOL fLock)
{
if (fLock)
{
DllAddRef();
}
else
{
DllRelease();
}
return S_OK;
}
private:
CClassFactory(PFNCREATEINSTANCE pfnCreate) : m_cRef(1), m_pfnCreate(pfnCreate)
{
DllAddRef();
}
~CClassFactory()
{
DllRelease();
}
long m_cRef;
PFNCREATEINSTANCE m_pfnCreate;
};
//
// Standard DLL functions
//
STDMETHODIMP_(BOOL) WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void *)
{
if (dwReason == DLL_PROCESS_ATTACH)
{
g_hModule = (HMODULE)hInstance;
DisableThreadLibraryCalls(hInstance);
#ifdef MF_WPP
WPP_INIT_TRACING(L"MultiPinMft");
// Hook up WIL tracing to our trace provider.
wil::SetResultLoggingCallback(
[](const wil::FailureInfo &failure) noexcept
{
wchar_t debugString[2048];
if (SUCCEEDED(wil::GetFailureLogString(debugString, ARRAYSIZE(debugString), failure)))
{
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_ERROR, L"%S", debugString);
}
else
{
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_ERROR,
L"File: %s, Line: %u, Error: 0x%08X - %S\n",
failure.pszFile,
failure.uLineNumber,
failure.hr,
failure.pszMessage);
}
}
);
#endif
}
else
if (dwReason == DLL_PROCESS_DETACH)
{
#ifdef MF_WPP
wil::SetResultLoggingCallback(nullptr);
WPP_CLEANUP();
#endif
}
return TRUE;
}
STDMETHODIMP DllCanUnloadNow()
{
HRESULT hr = ((g_cRefModule == 0) && (CDMFTModuleLifeTimeManager::GetDMFTObjCount() == 0)) ? S_OK : S_FALSE;
//
// Debug object lifetimes
//
DMFTRACE(DMFT_GENERAL, TRACE_LEVEL_INFORMATION, "%!FUNC! returning %d %d %!HRESULT!",
g_cRefModule,
CDMFTModuleLifeTimeManager::GetDMFTObjCount(),
hr);
return hr;
}
_Check_return_
STDAPI DllGetClassObject(_In_ REFCLSID clsid, _In_ REFIID riid, _Outptr_ LPVOID FAR* ppv)
{
return CClassFactory::CreateInstance(clsid, c_rgClassObjectInit, ARRAYSIZE(c_rgClassObjectInit), riid, ppv);
}
STDMETHODIMP DllRegisterServer()
{
assert(g_hModule != NULL);
// Register the CLSID for CoCreateInstance.
HRESULT hr = RegisterObject(g_hModule, CLSID_MultiPinMFT, TEXT("Multiple MFTs"), TEXT("Both"));
return hr;
}
STDMETHODIMP DllUnregisterServer()
{
// Unregister the CLSID.
UnregisterObject(CLSID_MultiPinMFT);
return S_OK;
}
// Converts a CLSID into a string with the form "CLSID\{clsid}"
STDMETHODIMP CreateObjectKeyName(REFGUID guid, _Out_writes_(cchMax) PWSTR pszName, DWORD cchMax)
{
const DWORD chars_in_guid = 39;
// convert CLSID uuid to string
OLECHAR szCLSID[chars_in_guid];
HRESULT hr = StringFromGUID2(guid, szCLSID, chars_in_guid);
if (SUCCEEDED(hr))
{
// Create a string of the form "CLSID\{clsid}"
hr = StringCchPrintf((STRSAFE_LPWSTR)pszName, cchMax, TEXT("Software\\Classes\\CLSID\\%ls"), szCLSID);
}
return hr;
}
// Creates a registry key (if needed) and sets the default value of the key
STDMETHODIMP CreateRegKeyAndValue(HKEY hKey, PCWSTR pszSubKeyName, PCWSTR pszValueName,
PCWSTR pszData, PHKEY phkResult)
{
*phkResult = NULL;
LONG lRet = RegCreateKeyExW(
hKey, pszSubKeyName,
0, NULL, REG_OPTION_NON_VOLATILE,
KEY_ALL_ACCESS, NULL, phkResult, NULL);
if (lRet == ERROR_SUCCESS)
{
lRet = RegSetValueExW(
(*phkResult),
pszValueName, 0, REG_SZ,
(LPBYTE) pszData,
((DWORD) wcslen(pszData) + 1) * sizeof(WCHAR)
);
if (lRet != ERROR_SUCCESS)
{
RegCloseKey(*phkResult);
}
}
return HRESULT_FROM_WIN32(lRet);
}
// Creates the registry entries for a COM object.
HRESULT RegisterObject(HMODULE hModule, const GUID& guid, const TCHAR *pszDescription, const TCHAR *pszThreadingModel)
{
HKEY hKey = NULL;
HKEY hSubkey = NULL;
TCHAR achTemp[MAX_PATH];
// Create the name of the key from the object's CLSID
HRESULT hr = CreateObjectKeyName(guid, achTemp, MAX_PATH);
// Create the new key.
if (SUCCEEDED(hr))
{
hr = CreateRegKeyAndValue(HKEY_LOCAL_MACHINE, achTemp, NULL, pszDescription,&hKey);
}
if (SUCCEEDED(hr))
{
(void)GetModuleFileName(hModule, achTemp, MAX_PATH);
hr = HRESULT_FROM_WIN32(GetLastError());
}
// Create the "InprocServer32" subkey
if (SUCCEEDED(hr))
{
hr = CreateRegKeyAndValue(hKey, L"InProcServer32", NULL, achTemp, &hSubkey);
RegCloseKey(hSubkey);
}
// Add a new value to the subkey, for "ThreadingModel" = <threading model>
if (SUCCEEDED(hr))
{
hr = CreateRegKeyAndValue(hKey, L"InProcServer32", L"ThreadingModel", pszThreadingModel, &hSubkey);
RegCloseKey(hSubkey);
}
// close hkeys
RegCloseKey(hKey);
return hr;
}
// Deletes the registry entries for a COM object.
HRESULT UnregisterObject(const GUID& guid)
{
WCHAR achTemp[MAX_PATH];
HRESULT hr = CreateObjectKeyName(guid, achTemp, MAX_PATH);
if (SUCCEEDED(hr))
{
// Delete the key recursively.
LONG lRes = RegDeleteTree(HKEY_LOCAL_MACHINE, achTemp);
hr = HRESULT_FROM_WIN32(lRes);
}
return hr;
}