summaryrefslogtreecommitdiffstats
path: root/private/oleutest/balls/srv/mixed/mixedsrv.cxx
blob: a12b279f6ca38faf249fe3b3ef3e732ff24f201c (plain) (blame)
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
//+-------------------------------------------------------------------
//
//  File:	srvmain.cxx
//
//  Contents:	This file contins the EXE entry points
//
//  History:	28-Feb-96   Rickhi  Created
//
//---------------------------------------------------------------------
#include    <common.h>
#include    <mixedcf.hxx>


extern "C" const GUID CLSID_QI;
extern "C" const GUID CLSID_Balls;
extern "C" const GUID CLSID_Loop;
extern "C" const GUID CLSID_Cubes;

int WINAPI RegisterSameThread(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow,
    DWORD dwThreadModel);

int WINAPI RegisterDifferentThreads(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow,
    DWORD dwThreadModel);


void RunServerThread(STHREADINFO *pThrdInfo);
DWORD _stdcall ServerThread(void *param);
void WakeupAllThreads();


TCHAR *pszWindow[] = { TEXT("QI Server"),
		       TEXT("Balls Server"),
		       TEXT("Cubes Server"),
		       TEXT("Loops Server")};


STHREADINFO *gpThrdInfo = NULL;
ULONG	     gcThrds	= 0;
BOOL	     gfWokenUpAllThreads = FALSE;


//+-------------------------------------------------------------------
//
//  Function:	MakeClassInfo
//
//  Synopsis:	fills in a SCLASSINFO structure
//
//  History:	28-Feb-96   Rickhi  Created
//
//--------------------------------------------------------------------
HRESULT MakeClassInfo(REFCLSID rclsid, SCLASSINFO *pClsInfo)
{
    pClsInfo->clsid	= rclsid;
    pClsInfo->pCF	= (IClassFactory *) new CMixedClassFactory(rclsid);
    pClsInfo->dwCtx	= CLSCTX_LOCAL_SERVER;
    pClsInfo->dwClsReg	= REGCLS_MULTIPLEUSE | REGCLS_SUSPENDED;
    pClsInfo->dwReg	= 0;

    return (pClsInfo->pCF != NULL) ? S_OK : E_OUTOFMEMORY;
}

//+-------------------------------------------------------------------
//
//  Function:	WinMain
//
//  Synopsis:	Entry point. Creates the classinfo and enters the main
//		server loop.
//
//  History:	28-Feb-96   Rickhi  Created
//
//--------------------------------------------------------------------
int WINAPI WinMain(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow)
{
    if (lpCmdLine == NULL)
    {
	// ShowHelp();
	return -1;
    }

    if (!(strncmp(lpCmdLine, "ApartmentSameThread", 19)))
    {
	return RegisterSameThread(hInstance, hPrevInstance, lpCmdLine, nCmdShow,
				  SRVF_THREADMODEL_APARTMENT);
    }
    else if (!(strncmp(lpCmdLine, "FreeSameThread", 14)))
    {
	return RegisterSameThread(hInstance, hPrevInstance, lpCmdLine, nCmdShow,
				  SRVF_THREADMODEL_MULTI);
    }
    else if (!(strncmp(lpCmdLine, "ApartmentDifferentThread", 24)))
    {
	return RegisterDifferentThreads(hInstance, hPrevInstance, lpCmdLine, nCmdShow,
				  SRVF_THREADMODEL_APARTMENT);
    }
    else if (!(strncmp(lpCmdLine, "FreeDifferentThread", 24)))
    {
	return RegisterDifferentThreads(hInstance, hPrevInstance, lpCmdLine, nCmdShow,
				  SRVF_THREADMODEL_MULTI);

    }

    return -1;
}

//+-------------------------------------------------------------------
//
//  Function:	RegisterSameThread
//
//  Synopsis:	Entry point. Creates the classinfo and enters the main
//		server loop.
//
//  History:	28-Feb-96   Rickhi  Created
//
//--------------------------------------------------------------------
int WINAPI RegisterSameThread(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow,
    DWORD dwThreadModel)
{
    SCLASSINFO	ClsInfo[4];
    STHREADINFO	ThrdInfo;
    HRESULT	hr[4];

    hr[0] = MakeClassInfo(CLSID_QI,    &ClsInfo[0]);
    hr[1] = MakeClassInfo(CLSID_Balls, &ClsInfo[1]);
    hr[2] = MakeClassInfo(CLSID_Cubes, &ClsInfo[2]);
    hr[3] = MakeClassInfo(CLSID_Loop,  &ClsInfo[3]);

    ThrdInfo.hEventRun	= CreateEvent(NULL, FALSE, FALSE, NULL);
    ThrdInfo.hEventDone = CreateEvent(NULL, FALSE, FALSE, NULL);
    ThrdInfo.hInstance	= hInstance;
    ThrdInfo.pszWindow	= pszWindow[0];
    ThrdInfo.dwFlags	= dwThreadModel;
    ThrdInfo.cClasses	= 4;
    ThrdInfo.pClsInfo	= &ClsInfo[0];

    // this thread is the one that should call resume.
    ThrdInfo.dwFlags   |= SRVF_REGISTER_RESUME;
    ThrdInfo.dwTid	= GetCurrentThreadId();

    // stuff the thrd pointers and count into globals so we can
    // wake the threads up whenever 1 single thread exits.
    gpThrdInfo = &ThrdInfo;
    gcThrds    = 1;

    hr[0] = SrvMain2(&ThrdInfo);

    return hr[0];
}


//+-------------------------------------------------------------------
//
//  Function:	RegisterDifferentThreads
//
//  Synopsis:	Entry point. Creates the classinfo and enters the main
//		server loop.
//
//  History:	28-Feb-96   Rickhi  Created
//
//--------------------------------------------------------------------
int WINAPI RegisterDifferentThreads(
    HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int nCmdShow,
    DWORD dwThreadModel)
{
    SCLASSINFO	ClsInfo[4];
    STHREADINFO	ThrdInfo[4];
    HRESULT	hr[4];

    hr[0] = MakeClassInfo(CLSID_QI,    &ClsInfo[0]);
    hr[1] = MakeClassInfo(CLSID_Balls, &ClsInfo[1]);
    hr[2] = MakeClassInfo(CLSID_Cubes, &ClsInfo[2]);
    hr[3] = MakeClassInfo(CLSID_Loop,  &ClsInfo[3]);

    for (int i=0; i<4; i++)
    {
	if (SUCCEEDED(hr[i]))
	{
	    ThrdInfo[i].hEventRun  = CreateEvent(NULL, FALSE, FALSE, NULL);
	    ThrdInfo[i].hEventDone = CreateEvent(NULL, FALSE, FALSE, NULL);
	    ThrdInfo[i].hInstance  = hInstance;
	    ThrdInfo[i].pszWindow  = pszWindow[i];
	    ThrdInfo[i].dwFlags	   = dwThreadModel;
	    ThrdInfo[i].cClasses   = 1;
	    ThrdInfo[i].pClsInfo   = &ClsInfo[i];

	    if (i > 0)
	    {
		// run the thread and wait for it signal it's ready
		RunServerThread(&ThrdInfo[i]);
		WaitForSingleObject(ThrdInfo[i].hEventRun, 0xffffffff);
		CloseHandle(ThrdInfo[i].hEventRun);
	    }
	}
    }

    // this thread is the one that should call resume.
    ThrdInfo[0].dwFlags |= SRVF_REGISTER_RESUME;
    ThrdInfo[0].dwTid	 = GetCurrentThreadId();

    // stuff the thrd pointers and count into globals so we can
    // wake the threads up whenever 1 single thread exits.
    gpThrdInfo = &ThrdInfo[0];
    gcThrds = 4;


    hr[0] = SrvMain2(&ThrdInfo[0]);
    WakeupAllThreads();

    for (i=0; i<4; i++)
    {
	if (SUCCEEDED(hr[i]))
	{
	    // wait for other thread to complete before exiting.
	    WaitForSingleObject(ThrdInfo[i].hEventDone, 0xffffffff);
	    CloseHandle(ThrdInfo[i].hEventDone);
	}
    }

    return hr[0];
}

//+-------------------------------------------------------------------
//
//  Function:	WakeupAllThreads
//
//  Synopsis:	Wakes up all the other threads in the process when
//		one thread decides to exit.
//
//  History:	28-Feb-96   Rickhi  Created
//
//--------------------------------------------------------------------
void WakeupAllThreads()
{
    if (gfWokenUpAllThreads)
	return;

    gfWokenUpAllThreads = TRUE;
    STHREADINFO *pThrdInfo = gpThrdInfo;

    for (ULONG i=0; i<gcThrds; i++, pThrdInfo++)
    {
	PostThreadMessage(pThrdInfo->dwTid, WM_QUIT, 0, 0);
    }
}

//+-------------------------------------------------------------------
//
//  Function:	ServerThread
//
//  Synopsis:	Thread Entry point. Registers a class and waits for calls
//		on it.
//
//  History:	28-Feb-96   Rickhi  Created
//
//--------------------------------------------------------------------
DWORD _stdcall ServerThread(void *param)
{
    STHREADINFO *pThrdInfo = (STHREADINFO *)param;

    HRESULT hr = SrvMain2(pThrdInfo);

    // wake up the other threads.
    WakeupAllThreads();

    return hr;
}

//+-------------------------------------------------------------------
//
//  Function:	RunServerThread
//
//  Synopsis:	Spins up a thread to act as a class server.
//
//  History:	28-Feb-96   Rickhi  Created
//
//--------------------------------------------------------------------
void RunServerThread(STHREADINFO *pThrdInfo)
{
    HANDLE hThrd = CreateThread(NULL, 0, ServerThread, pThrdInfo,
				0, &(pThrdInfo->dwTid));
    if (hThrd)
    {
	CloseHandle(hThrd);
    }

    return;
}