-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDllMain.cpp
214 lines (180 loc) · 5.66 KB
/
DllMain.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
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
/****************************** Module Header ******************************\
Module Name: DllMain.cpp
Project: CppDllCOMServer
Copyright (c) Microsoft Corporation.
This source is subject to the Microsoft Public License.
See http://www.microsoft.com/en-us/openness/resources/licenses.aspx#MPL.
All other rights reserved.
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.
\***************************************************************************/
#pragma region Includes
#include <windows.h>
#include "Reg.h"
#include "CFDispatchedUiAutomation.h" // For the class factory
#include "DispatchedUiAutomation_i.c" // For component GUIDs
#pragma endregion
HINSTANCE g_hInst = NULL;
long g_cDllRef = 0;
BOOL APIENTRY DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
// Hold the instance of this DLL module, we will use it to get the
// path of the DLL to register the component.
g_hInst = hInstance;
DisableThreadLibraryCalls(hInstance);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
//
// FUNCTION: DllGetClassObject(REFCLSID, REFIID, void **)
//
// PURPOSE: Create the class factory and query to the specific interface.
//
// PARAMETERS:
// * rclsid - The CLSID that will associate the correct data and code.
// * riid - A reference to the identifier of the interface that the caller
// is to use to communicate with the class object.
// * ppv - The address of a pointer variable that receives the interface
// pointer requested in riid. Upon successful return, *ppv contains the
// requested interface pointer. If an error occurs, the interface pointer
// is NULL.
//
STDAPI ProxyDllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
{
HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
if (IsEqualCLSID(CLSID_UiAutomation, rclsid))
{
hr = E_OUTOFMEMORY;
CFDispatchedUiAutomation *pClassFactory = new CFDispatchedUiAutomation();
if (pClassFactory)
{
hr = pClassFactory->QueryInterface(riid, ppv);
pClassFactory->Release();
}
}
/*
else if (IsEqualCLSID(CLSID_UiAutomationElement, rclsid))
{
hr = E_OUTOFMEMORY;
CFDispatchedUiAutomationElement *pClassFactory = new CFDispatchedUiAutomationElement();
if (pClassFactory)
{
hr = pClassFactory->QueryInterface(riid, ppv);
pClassFactory->Release();
}
}
*/
return hr;
}
//
// FUNCTION: DllCanUnloadNow(void)
//
// PURPOSE: Check if we can unload the component from the memory.
//
// NOTE: The component can be unloaded from the memory when its reference
// count is zero (i.e. nobody is still using the component).
//
STDAPI ProxyDllCanUnloadNow(void)
{
return g_cDllRef > 0 ? S_FALSE : S_OK;
}
//
// FUNCTION: DllRegisterServer(void)
//
// PURPOSE: Register the COM server.
//
STDAPI DllRegisterServer(void)
{
HRESULT hr;
//MessageBox(NULL, L"Stop me here", L"A caption", MB_OK);
wchar_t szModule[MAX_PATH];
if (GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule)) == 0)
{
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// Register the components
hr = RegisterInprocServer(szModule,
CLSID_UiAutomation,
L"DispatchedUiAutomation.UiAutomation Class",
L"Apartment",
LIBID_DispatchedUiAutomation,
L"DispatchedUiAutomation.UiAutomation.1",
L"DispatchedUiAutomation.UiAutomation");
/*
hr2 = RegisterInprocServer(szModule,
CLSID_UiAutomationElement,
L"DispatchedUiAutomation.UiAutomationElement Class",
L"Apartment",
LIBID_DispatchedUiAutomation,
L"DispatchedUiAutomation.UiAutomationElement.1",
L"DispatchedUiAutomation.UiAutomationElement");
hr3 = RegisterInprocServer(szModule,
CLSID_UiAutomationPropertyCondition,
L"DispatchedUiAutomation.UiAutomationPropertyCondition Class",
L"Apartment",
LIBID_DispatchedUiAutomation,
L"DispatchedUiAutomation.UiAutomationPropertyCondition.1",
L"DispatchedUiAutomation.UiAutomationPropertyCondition");
hr4 = RegisterInprocServer(szModule,
CLSID_UiAndCondition,
L"DispatchedUiAutomation.UiAndCondition Class",
L"Apartment",
LIBID_DispatchedUiAutomation,
L"DispatchedUiAutomation.UiAndCondition.1",
L"DispatchedUiAutomation.UiAndCondition");
hr = S_FALSE;
if (SUCCEEDED(hr1) && SUCCEEDED(hr2) && SUCCEEDED(hr3) && SUCCEEDED(hr4))*/
if (SUCCEEDED(hr))
{
// Register the type library.
hr = RegisterTypeLib(szModule);
}
return hr;
}
//
// FUNCTION: DllUnregisterServer(void)
//
// PURPOSE: Unregister the COM server.
//
STDAPI DllUnregisterServer(void)
{
HRESULT hr = S_OK;
wchar_t szModule[MAX_PATH];
if (GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule)) == 0)
{
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
// Unregister the components
hr = UnregisterInprocServer(CLSID_UiAutomation,
L"DispatchedUiAutomation.UiAutomation.1",
L"DispatchedUiAutomation.UiAutomation");
/*
hr2 = UnregisterInprocServer(CLSID_UiAutomationElement,
L"DispatchedUiAutomation.UiAutomationElement.1",
L"DispatchedUiAutomation.UiAutomationElement");
hr3 = UnregisterInprocServer(CLSID_UiAutomationPropertyCondition,
L"DispatchedUiAutomation.UiAutomationPropertyCondition.1",
L"DispatchedUiAutomation.UiAutomationPropertyCondition");
hr4 = UnregisterInprocServer(CLSID_UiAndCondition,
L"DispatchedUiAutomation.UiAndCondition.1",
L"DispatchedUiAutomation.UiAndCondition");
hr = S_FALSE;
if (SUCCEEDED(hr1) && SUCCEEDED(hr2) && SUCCEEDED(hr3))*/
if (SUCCEEDED(hr))
{
// Unregister the type library.
hr = UnregisterTypeLib(szModule);
}
return hr;
}