// svc.cpp
//
// Copyright 1997, Don Box/Addison Wesley
//
// This code accompanies the book 'The Component
// Object Model' from Addison Wesley. Blah blah blah
//
//
#define _WIN32_WINNT 0x403
#include <windows.h>
#include <olectl.h>
#include <initguid.h>
#include <iaccess.h>
#include «ChatSession.h»
#include «../include/COMChat_i.c»
#if !defined(HAVE_IID_IACCESSCONTROL)
// there is a common bug is the SDK headers and libs
// that causes IID_IAccessControl to be undefined.
// We define it here to give the GUID linkage.
DEFINE_GUID(IID_IAccessControl,0xEEDD23E0, 0x8410, 0x11CE,
0xA1, 0xC3, 0x08, 0x00, 0x2B, 0x2B, 0x8D, 0x8F);
#endif
// standard MTA lifetime management helpers
HANDLE g_heventDone = CreateEvent(0, TRUE, FALSE, 0);
void ModuleLock(void)
{
CoAddRefServerProcess();
}
void ModuleUnlock(void)
{
if (CoReleaseServerProcess() == 0)
SetEvent(g_heventDone);
}
// standard self-registration table
const char *g_RegTable[][3] = {
{ «CLSID\{5223A053-2441-11d1-AF4F-0060976AA886}»,
0, «ChatSession» },
{ «CLSID\{5223A053-2441-11d1-AF4F-0060976AA886}»,
«AppId», «{5223A054-2441-11d1-AF4F-0060976AA886}»
},
{ «CLSID\{5223A053-2441-11d1-AF4F-0060976AA886}\LocalServer32»,
0, (const char*)-1 // rogue value indicating file name
},
{ «AppID\{5223A054-2441-11d1-AF4F-0060976AA886}»,
0, «ChatSession Server» },
{ «AppID\{5223A054-2441-11d1-AF4F-0060976AA886}»,
«RunAs», «Domain\ReplaceMe»
},
{ «AppID\{5223A054-2441-11d1-AF4F-0060976AA886}»,
«Chat Admins Group», «Domain\ReplaceMe»
},
{ «AppID\{5223A054-2441-11d1-AF4F-0060976AA886}»,
«Chat Users Group», «Domain\ReplaceMe»
},
{ «AppID\COMChat.exe»,
«AppId», «{5223A054-2441-11d1-AF4F-0060976AA886}»
},
};
// self-unregistration routine
STDAPI UnregisterServer(void) {
HRESULT hr = S_OK;
int nEntries = sizeof(g_RegTable)/sizeof(*g_RegTable);
for (int i = nEntries – 1; i >= 0; i–){
const char *pszKeyName = g_RegTable[i][0];
long err = RegDeleteKeyA(HKEY_CLASSES_ROOT, pszKeyName);
if (err != ERROR_SUCCESS)
hr = S_FALSE;
}
return hr;
}
// self-registration routine
STDAPI RegisterServer(HINSTANCE hInstance = 0) {
HRESULT hr = S_OK;
// look up server's file name
char szFileName[MAX_PATH];
GetModuleFileNameA(hInstance, szFileName, MAX_PATH);
// register entries from table
int nEntries = sizeof(g_RegTable)/sizeof(*g_RegTable);
for (int i = 0; SUCCEEDED(hr) && i < nEntries; i++) {
const char *pszKeyName = g_RegTable[i][0];
const char *pszValueName = g_RegTable[i][1];
const char *pszValue = g_RegTable[i][2];
// map rogue value to module file name
if (pszValue == (const char*)-1)
pszValue = szFileName;
HKEY hkey;
// create the key
long err = RegCreateKeyA(HKEY_CLASSES_ROOT,
pszKeyName, &hkey);
if (err == ERROR_SUCCESS) {
// set the value
err = RegSetValueExA(hkey, pszValueName, 0,
REG_SZ, (const BYTE*)pszValue,
(strlen(pszValue) + 1));
RegCloseKey(hkey);
}
if (err != ERROR_SUCCESS) {
// if cannot add key or value, back out and fail