Сущность технологии СОМ. Библиотека программиста
Шрифт:
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,
(void**)&g_pacUsers);
if (SUCCEEDED(hr))
hr = g_pacUsers->SetAccessRights(&aaUsers);
if (SUCCEEDED(hr))
{
hr = CoCreateInstance(CLSID_DCOMAccessControl,
0, CLSCTX_ALL,
IID_IAccessControl,
(void**)&g_pacAdmins);
if (SUCCEEDED(hr))
hr = g_pacAdmins->SetAccessRights(&aaAdmins);
}
if (FAILED(hr))
{
if (g_pacAdmins)
{
g_pacAdmins->Release;
g_pacAdmins = 0;
}
if (g_pacUsers)
{
g_pacUsers->Release;
g_pacUsers = 0;
}
}
}
return hr;
}
// the main thread routine that simply registers the class
// object and waits to die
int WINAPI WinMain(HINSTANCE, HINSTANCE,
LPSTR szCmdParam, int)
{
const TCHAR *pszPrompt =
__TEXT("Ensure that you have properly ")
__TEXT("configured the application to ")
__TEXT("run as a particular user and that ")
__TEXT("you have manually changed the ")
__TEXT("Users and Admins Group registry ")
__TEXT(«settings under this server's AppID.»);
HRESULT hr = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hr))
return hr;
// look for self-registration flags
if (strstr(szCmdParam,
|| strstr(szCmdParam, «-UnregServer») != 0)
{
hr = UnregisterServer;
CoUninitialize;
return hr;
}
else if (strstr(szCmdParam, «/RegServer») != 0
|| strstr(szCmdParam, «-RegServer») != 0)
{
hr = RegisterServer;
MessageBox(0, pszPrompt, __TEXT(«COMChat»),
MB_SETFOREGROUND);
CoUninitialize;
return hr;
}
// set up process security
hr = InitializeApplicationSecurity;
if (SUCCEEDED(hr))
{
// register class object and wait to die
DWORD dwReg;
static ChatSessionClass cmc;
hr = CoRegisterClassObject(CLSID_ChatSession,
static_cast<IExternalConnection*>(&cmc),
CLSCTX_LOCAL_SERVER
REGCLS_SUSPENDED|REGCLS_MULTIPLEUSE,
&dwReg);
if (SUCCEEDED(hr))
{
hr = CoResumeClassObjects;
if (SUCCEEDED(hr))
WaitForSingleObject(g_heventDone, INFINITE);
CoRevokeClassObject(dwReg);
}
g_pacUsers->Release;
g_pacAdmins->Release;
}
if (FAILED(hr))
MessageBox(0, pszPrompt, __TEXT(«Error»),
MB_SETFOREGROUND);
CoUninitialize;
return 0;
}
Source Code
COM Chat Compilable versions of the source code in Appendix B of the book.
YACL Yet another COM library. Contains the various macros and C++ classes described in the book.
IGlobalInterfaceTable Wrapper/Sample A simplification of apartment-independent pointer use.
HostHook A custom channel hook that allows you to find out the caller's and callee's network address.
APTSUR A custom surrogate that spawns distinct STA threads for each activation request.
Custom Moniker Stuff I worked with some friends on a custom moniker framework. Here is some of the 1st bits and pieces.
Yet Another COM Library (YACL) – Preview
This is my first drop. It contains a family of preprocessor macros that automate the boilerplate activities used in COM programming.
Please send comments and bug reports to cbbugs@braintrust.com
The freshest drop is always available at http://www.develop.com/dbox/yacl.zip
Design Goals (in Order)
Easily used without Wizard support
Easily kept in one's head
Modular (use only what you need and nothing else)
Extensible
Small Code
Fast Code
No DLL ever
Compiler-friendly
Platform-neutral where possible (including 16-bit Windows)
Current Feature Set
Anal-rententive Smart Pointer
Efficient and intuitive Unicode handling
Table-driven QueryInterface
Table-driven Registration
Table-driven Class management
Generic Class Factory implementation.
Preprocessor macros for de facto IUnknown implementation techniques.
Preprocessor macros for de facto module management routines.
Preprocessor macros for de facto DllXXX routines.
Preprocessor macros for de facto out-of-proc CRCO/Wait/CRCO sequence.
Planned Work
Performance/size tuning
Compiler/Platform testing
Verify ATL/MFC interoperation
Macro-ization of smart pointer for 16-bit windows