Сущность технологии СОМ. Библиотека программиста :: Бокс Дональд
Страница:
525 из 528
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
UnregisterServer();
hr = SELFREG_E_CLASS;
}
}
return hr;
}
// these point to standard access control objects
// used to protect particular methods
IAccessControl *g_pacUsers = 0;
IAccessControl *g_pacAdmins = 0;
// this routine is called at process init time
// to build access control objects and to allow
// anonymous access to server by default
HRESULT InitializeApplicationSecurity(void)
{
// load groupnames from registry
static OLECHAR wszAdminsGroup[1024];
static OLECHAR wszUsersGroup[1024];
HKEY hkey;
long err = RegOpenKeyEx(HKEY_CLASSES_ROOT,
__TEXT(«AppID\\{5223A054-2441-11d1-AF4F-0060976AA886}»),
0, KEY_QUERY_VALUE,
&hkey);
if (err == ERROR_SUCCESS)
{
DWORD cb = sizeof(wszAdminsGroup);
err = RegQueryValueExW(hkey, L"Chat Admins Group",
0, 0, (BYTE*)wszAdminsGroup,
&cb);
cb = sizeof(wszAdminsGroup);
if (err == ERROR_SUCCESS)
err = RegQueryValueExW(hkey,
L"Chat Users Group",
0, 0, (BYTE*)wszUsersGroup,
&cb);
RegCloseKey(hkey);
}
if (err != ERROR_SUCCESS)
return MAKE_HRESULT(SEVERITY_ERROR, FACILITY_WIN32,
GetLastError());
// declare vectors of user/groups for 2 access
// control objects
ACTRL_ACCESS_ENTRYW rgaaeUsers[] = {
{ {0, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_NAME,
TRUSTEE_IS_GROUP, wszUsersGroup },
ACTRL_ACCESS_ALLOWED, COM_RIGHTS_EXECUTE, 0,
NO_INHERITANCE, 0 },
};
ACTRL_ACCESS_ENTRY_LISTW aaelUsers = {
sizeof(rgaaeUsers)/sizeof(*rgaaeUsers),
rgaaeUsers
};
ACTRL_PROPERTY_ENTRYW apeUsers = { 0, &aaelUsers, 0 };
ACTRL_ACCESSW aaUsers = { 1, &apeUsers };
ACTRL_ACCESS_ENTRYW rgaaeAdmins[] = {
{ {0, NO_MULTIPLE_TRUSTEE, TRUSTEE_IS_NAME,
TRUSTEE_IS_GROUP, wszAdminsGroup },
ACTRL_ACCESS_ALLOWED, COM_RIGHTS_EXECUTE, 0,
NO_INHERITANCE, 0 },
};
ACTRL_ACCESS_ENTRY_LISTW aaelAdmins = {
sizeof(rgaaeAdmins)/sizeof(*rgaaeAdmins),
rgaaeAdmins
};
ACTRL_PROPERTY_ENTRYW apeAdmins = { 0, &aaelAdmins, 0 };
ACTRL_ACCESSW aaAdmins = { 1, &apeAdmins };
HRESULT hr = CoInitializeSecurity(0, -1, 0, 0,
RPC_C_AUTHN_LEVEL_NONE,
RPC_C_IMP_LEVEL_ANONYMOUS,
0,
EOAC_NONE,
0);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_DCOMAccessControl,
0, CLSCTX_ALL, IID_IAccessControl,
<
|< Пред. 523 524 525 526 527 След. >|