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
|
//+-------------------------------------------------------------------
//
// File: srvmain.cxx
//
// Contents: Server entry points.
//
// Classes: none
//
// History: 23-Nov-92 Rickhi Created
//
//--------------------------------------------------------------------
#include <pch.cxx>
#pragma hdrstop
extern "C"
{
#include "wterm.h"
#include <memory.h>
#include <stdio.h>
}
// globals
LONG g_Usage = 0;
DWORD gTlsIndex = TlsAlloc();
//+-------------------------------------------------------------------
//
// typedef: SCLASSDATA - class data stored in TLS.
//
//+-------------------------------------------------------------------
typedef struct tagSCLASSDATA
{
ULONG cClasses;
SCLASSINFO *pClsInfo;
} SCLASSDATA;
//+-------------------------------------------------------------------
//
// Function: RememberClasses
//
// Synopsis: Stores the class info for this thread in TLS.
//
// History: 21-Nov-92 SarahJ Created
//
//--------------------------------------------------------------------
HRESULT RememberClasses(SCLASSINFO *pClsInfo, ULONG cClasses)
{
SCLASSDATA *pClsData = (SCLASSDATA *) new BYTE[sizeof(SCLASSDATA)];
if (pClsData)
{
pClsData->cClasses = cClasses;
pClsData->pClsInfo = pClsInfo;
TlsSetValue(gTlsIndex, pClsData);
return S_OK;
}
return E_OUTOFMEMORY;
}
//+-------------------------------------------------------------------
//
// Function: RecallClasses
//
// Synopsis: retrieves the class info for this thread from TLS.
//
// History: 21-Nov-92 SarahJ Created
//
//--------------------------------------------------------------------
void RecallClasses(SCLASSINFO **ppClsInfo, ULONG *pcClasses)
{
SCLASSDATA *pClsData = (SCLASSDATA *) TlsGetValue(gTlsIndex);
if (pClsData)
{
*pcClasses = pClsData->cClasses;
*ppClsInfo = pClsData->pClsInfo;
}
else
{
*pcClasses = 0;
*ppClsInfo = NULL;
}
}
//+-------------------------------------------------------------------
//
// Function: ForgetClasses
//
// Synopsis: removes the class info for this thread from TLS.
//
// History: 21-Nov-92 SarahJ Created
//
//--------------------------------------------------------------------
void ForgetClasses()
{
SCLASSDATA *pClsData = (SCLASSDATA *) TlsGetValue(gTlsIndex);
if (pClsData)
{
delete pClsData;
TlsSetValue(gTlsIndex, NULL);
}
}
//+-------------------------------------------------------------------
//
// Function: RegisterAllClasses
//
// Synopsis: loops, registering each class specified by the server.
//
// History: 21-Nov-92 SarahJ Created
//
//--------------------------------------------------------------------
void RegisterAllClasses(SCLASSINFO *pClsInfo, ULONG cClasses)
{
// store the class info in TLS so we can get it back later
// in GlobalRefs.
if (FAILED(RememberClasses(pClsInfo, cClasses)))
return;
HRESULT hr;
for (ULONG i=0; i<cClasses; i++, pClsInfo++)
{
// register the CF. Note that we do this AFTER creating the
// window, so that if we get a Release very quickly we have
// a valid window handle to send a message to.
hr = CoRegisterClassObject(pClsInfo->clsid,
pClsInfo->pCF,
pClsInfo->dwCtx,
pClsInfo->dwClsReg,
&pClsInfo->dwReg);
if (FAILED(hr))
{
Display(TEXT("ERROR: failed CoRegisterClassObject %x\n"), hr);
}
}
if (FAILED(hr))
{
Display(TEXT("ERROR: failed CoResumeClassObjects %x\n"), hr);
}
}
//+-------------------------------------------------------------------
//
// Function: RevokeAllClasses
//
// Synopsis: loops, revoking each class specified by the server.
//
// History: 21-Nov-92 SarahJ Created
//
//--------------------------------------------------------------------
void RevokeAllClasses()
{
// first, get the class information from TLS
SCLASSINFO *pClsInfo;
ULONG cClasses;
RecallClasses(&pClsInfo, &cClasses);
// now revoke each of the registered classes
for (ULONG i=0; i<cClasses; i++, pClsInfo++)
{
if (pClsInfo->dwReg != 0)
{
ULONG ulRef = CoRevokeClassObject(pClsInfo->dwReg);
Display(TEXT("Revoked ClassObject\n"));
pClsInfo->dwReg = 0;
}
}
}
//+-------------------------------------------------------------------
//
// Function: ReleaseAllClasses
//
// Synopsis: loops, releasing each class factory object.
//
// History: 21-Nov-92 SarahJ Created
//
//--------------------------------------------------------------------
void ReleaseAllClasses()
{
// first, get the class information from TLS
SCLASSINFO *pClsInfo;
ULONG cClasses;
RecallClasses(&pClsInfo, &cClasses);
// now release each of the class factories
for (ULONG i=0; i<cClasses; i++, pClsInfo++)
{
ULONG ulRef = pClsInfo->pCF->Release();
Display(TEXT("CF RefCnt = %x\n"), ulRef);
}
// now remove the class information from TLS
ForgetClasses();
}
//+-------------------------------------------------------------------------
//
// Function: GlobalRefs
//
// Synopsis: keeps track of global reference couting. Revokes all the
// classes when the global count reaches 0.
//
// History: 06-Aug-92 Ricksa Created
//
//--------------------------------------------------------------------------
void GlobalRefs(BOOL fAddRef)
{
if (fAddRef)
{
g_Usage = CoAddRefServerProcess();
Display(TEXT("Object Count: %ld\n"), g_Usage);
}
else
{
if (0 == (g_Usage = CoReleaseServerProcess()))
{
// No more objects so we can quit
Display(TEXT("Object Server Exiting\n"));
PostMessage((HWND)g_hMain, WM_TERM_WND, 0, 0);
}
}
}
//+-------------------------------------------------------------------
//
// Function: SrvMain
//
// Synopsis: Special entry point for registing just 1 class.
//
// History: 21-Nov-92 SarahJ Created
//
//--------------------------------------------------------------------
int SrvMain(
HANDLE hInstance,
REFCLSID rclsid,
DWORD dwClsReg,
TCHAR *pszAppName,
IClassFactory *pCF)
{
SCLASSINFO clsinfo;
clsinfo.clsid = rclsid;
clsinfo.dwCtx = CLSCTX_LOCAL_SERVER,
clsinfo.dwClsReg = dwClsReg;
clsinfo.pCF = pCF;
clsinfo.dwReg = 0;
STHREADINFO ThrdInfo;
ThrdInfo.hEventRun = CreateEvent(NULL, FALSE, FALSE, NULL);
ThrdInfo.hEventDone = CreateEvent(NULL, FALSE, FALSE, NULL);
ThrdInfo.hInstance = (HINSTANCE)hInstance;
ThrdInfo.dwTid = GetCurrentThreadId();
ThrdInfo.pszWindow = pszAppName;
ThrdInfo.dwFlags = SRVF_THREADMODEL_UNKNOWN;
ThrdInfo.cClasses = 1;
ThrdInfo.pClsInfo = &clsinfo;
return SrvMain2(&ThrdInfo);
}
//+-------------------------------------------------------------------
//
// Function: SrvMain2
//
// Synopsis: Main entry point for registering classes, entering modal
// loop, and cleaning up on exit.
//
// History: 21-Nov-92 SarahJ Created
//
//--------------------------------------------------------------------
int SrvMain2(STHREADINFO *pThrdInfo)
{
MakeTheWindow(pThrdInfo->hInstance, pThrdInfo->pszWindow);
// Initialize the OLE libraries
DWORD dwThreadMode = COINIT_APARTMENTTHREADED;
if (pThrdInfo->dwFlags & SRVF_THREADMODEL_UNKNOWN)
{
// Look up the thread mode from the win.ini file.
TCHAR buffer[80];
int len;
len = GetProfileString( TEXT("OleSrv"),
TEXT("ThreadMode"),
TEXT("MultiThreaded"),
buffer,
sizeof(buffer) );
if (lstrcmp(buffer, TEXT("ApartmentThreaded")) != 0)
dwThreadMode = COINIT_MULTITHREADED;
}
else if (pThrdInfo->dwFlags & SRVF_THREADMODEL_MULTI)
{
dwThreadMode = COINIT_MULTITHREADED;
}
HRESULT hr = CoInitializeEx(NULL, dwThreadMode);
if (SUCCEEDED(hr))
{
// registe all the classes
RegisterAllClasses(pThrdInfo->pClsInfo, pThrdInfo->cClasses);
}
else
{
Display(TEXT("ERROR: failed OleInitialize %x\n"), hr);
}
if (pThrdInfo->dwFlags & SRVF_REGISTER_RESUME)
{
hr = CoResumeClassObjects();
}
// notify the main thread we are running now
SetEvent(pThrdInfo->hEventRun);
// Message processing loop
MSG msg;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
// revoke the classes again incase we missed any (eg. no instances
// were created, the user just closed the app).
RevokeAllClasses();
// release the classes
ReleaseAllClasses();
// Tell OLE we are going away.
CoUninitialize();
// notify the main thread that we are done
SetEvent(pThrdInfo->hEventDone);
return (msg.wParam); /* Returns the value from PostQuitMessage */
}
|