////////////////////////////////////////////
//
// Simple Module Locking Routines for
// out-of-process servers
//
#include <windows.h>
// how many reasons do we have to remain running?
LONG g_cLocks = 0;
// Routine to call from instance constructors
void LockModule(void)
{
InterlockedIncrement(&g_cLocks);
}
// Routine to call from instance destructors
void UnlockModule(void)
{
if (InterlockedDecrement(&g_cLocks) == 0)
{
// lock count is now zero, so shutdown nicely
PostQuitMessage(0);
}
}
Figure 2 Creating and Registering Objects
int WINAPI WinMain(HINSTANCE, HINSTANCE,
LPSTR, int) {
CoInitialize(0);
CClassFactory<CFoo> cfFoo;
CClassFactory<CBar> cfBar;
DWORD dwFoo, dwBar;
CoRegisterClassObject(CLSID_Foo, &cfFoo,
CLSCTX_SERVER,
REGCLS_MULTIPLEUSE,
&dwFoo);
CoRegisterClassObject(CLSID_Bar, &cfBar,
CLSCTX_SERVER,
REGCLS_MULTIPLEUSE,
&dwBar);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
DispatchMessage(&msg);
CoRevokeClassObject(dwFoo);
CoRevokeClassObject(dwBar);
CoUninitialize();
return 0;
}
Figure 5 Sample Freethreaded Server
#define _WIN32_DCOM
#include <windows.h>
#include <initguid.h>
HANDLE g_heventDone = CreateEvent(0, TRUE, FALSE, 0);
void LockModule(void)
{
CoAddRefServerProcess();
}
void UnlockModule(void)
{
if (CoReleaseServerProcess() == 0)
SetEvent(g_heventDone);
}
•
•
•
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR szCmdParam, int)
{
•
•
•
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
return int(hr);
DWORD dw1, dw2;
CClassFactory cf1(CMTObject1::Create);
CClassFactory cf2(CMTObject2::Create);
hr = CoRegisterClassObject(CLSID_MTObject1, &cf1,
CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE|REGCLS_SUSPENDED,
&dw1);
if (SUCCEEDED(hr))
{
hr = CoRegisterClassObject(CLSID_MTObject2, &cf2,
CLSCTX_LOCAL_SERVER,
REGCLS_MULTIPLEUSE|REGCLS_SUSPENDED,
&dw2);
if (SUCCEEDED(hr))
{
// all class objects registered suspended, so tell the SCM we are
// ready and wait for our internal shutdown notification
hr = CoResumeClassObjects();
if (SUCCEEDED(hr))
WaitForSingleObject(g_heventDone, INFINITE);
hr = CoRevokeClassObject(dw2);
}
hr = CoRevokeClassObject(dw1);
}
Sleep(500);
CoUninitialize();
return 0;
}
Figure 7 Avoiding a Race Condition
////////////////////////////////////////////
// Simple Module Locking Routines for
// in-process servers
#include <windows.h>
// how many reasons do we have to remain running?
LONG g_cLocks = 0;
// when was the final unlock called?
DWORD g_dwLastUnlockTime = 0;
// how long should we postpone unloading?
enum { SAFETY_WINDOW = 1000 };
// Routine to call from instance constructors
void LockModule(void)
{
InterlockedIncrement(&g_cLocks);
}
// Routine to call from instance destructors
void UnlockModule(void)
{
// record current time
InterlockedExchange((LONG*)&g_dwLastUnlockTime, GetTickCount());
InterlockedDecrement(&g_cLocks);
}
STDAPI DllCanUnloadNow(void)
{
// only say "unload" if no locks and SAFETY_WINDOW msecs
// have elapsed since the last unlock
if (g_cLocks == 0 &&
g_dwLastUnlockTime + SAFETY_WINDOW < GetTickCount())
return S_OK;
else
return S_FALSE;
}