Сущность технологии СОМ. Библиотека программиста - страница 197

Шрифт
Интервал

стр.

{

if (m_pCloneSource)

m_pCloneSource->Release();

else if (m_pStrings)

delete m_pStrings;

DeleteCriticalSection(&m_csLock);

}

// IUnknown methods

STDMETHODIMP

SessionNamesEnumerator::QueryInterface(REFIID riid, void **ppv)

{

if (riid == IID_IUnknown)

*ppv = static_cast(this);

else if (riid == IID_IEnumString)

*ppv = static_cast(this);

else

return (*ppv = 0), E_NOINTERFACE;

reinterpret_cast(*ppv)->AddRef();

return S_OK;

}

STDMETHODIMP_(ULONG)

SessionNamesEnumerator::AddRef(void)

{

ModuleLock();

return InterlockedIncrement(&m_cRef);

}

STDMETHODIMP_(ULONG)

SessionNamesEnumerator::Release(void)

{

LONG res = InterlockedDecrement(&m_cRef);

if (res == 0)

delete this;

ModuleUnlock();

return res;

}

// IEnumString methods

STDMETHODIMP

SessionNamesEnumerator::Next(ULONG cElems, OLECHAR **rgElems,

ULONG *pcFetched)

{

if (cElems > 1 && pcFetched == 0)

return E_INVALIDARG;

ULONG cActual = 0;

vector &rstrings = Strings();

Lock();

while (cActual < cElems

&& m_cursor != rstrings.end())

{

if (rgElems[cActual] = OLESTRDUP((*m_cursor).c_str()))

{

m_cursor++;

cActual++;

}

else // allocation error, unwind

{

while (cActual > 0)

{

cActual–;

CoTaskMemFree(rgElems[cActual]);

rgElems[cActual] = 0;

}

break;

}

}

Unlock();

if (cActual)

*pcFetched = cActual;

return cActual == cElems ? S_OK : S_FALSE;

}

STDMETHODIMP

SessionNamesEnumerator::Skip(ULONG cElems)

{

ULONG cActual = 0;

vector &rstrings = Strings();

Lock();

while (cActual < cElems

&& m_cursor != rstrings.end())

{

m_cursor++;

cActual++;

}

Unlock();

return cActual == cElems ? S_OK : S_FALSE;

}

STDMETHODIMP

SessionNamesEnumerator::Reset(void)

{

Lock();

m_cursor = Strings().begin();

Unlock();

return S_OK;

}

STDMETHODIMP


SessionNamesEnumerator::Clone(IEnumString **ppes)

{

if (ppes == 0)

return E_INVALIDARG;

SessionNamesEnumerator *pCloneSource = m_pCloneSource;

if (pCloneSource == 0) // we are the source

m_pCloneSource = this;

*ppes = new SessionNamesEnumerator(pCloneSource);

if (*ppes)

{

(*ppes)->AddRef();

return S_OK;

}

return E_OUTOFMEMORY;

}

svc.cpp

/////////////////////////////////////////////////////

//

// 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

#include

#include

#include

#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) {


стр.

Похожие книги