. CoMarshalInterface . , , , . , , . , . AddRef Release. . , . , , . , , (garbage collection) .
, (OID) , (OXID Resolver - OR). OR , - . , CoUnmarshalInterface OR , . , OR , OID - . OID - , OR (ping relationship) . OR RPC, , - . . (ping interval) OID, . , , .
Windows NT 4.0 , ( ) , OR , , - . OR , , . - , , , , .
, , - . , , . , CoUninitialize ( , ), . , OR , OR- . , 1.
. , , , , . , . , , , , - . , , MSHLFLAGS_NOPING. , , .
, . , . CoMarshalInterface MSHLFLAGS_NORMAL, n, . , , n . CoMarshalInterface , . , .
, . (Running Object Table), . MSHLFLAGS_NORMAL, - . , , MSHLFLAGS_TABLESTRONG, MSHLFLAGS_TABLEWEAK. .
(strong) (weak) . MSHLFLAGS_TABLEWEAK, . , , . . , , , , . , . , . , MSHLFLAGS_TABLESTRONG, , . , . , , . , . , . , - , , . API- CoReleaseMarshalData, , :
HRESULT CoReleaseMarshalData([in] IStream *pStm);
CoUnmarshalInterface, CoReleaseMarshalData IStream . , CoReleaseMarshalData. CoUnmarshalInterface, CoReleaseMarshalData.
, , . CoLockObjectExternal, :
HRESULT CoLockObjectExternal([in] IUnknown *pUnkObject, [in] BOOL bLock, [in] BOOL bLastUnlockKillsStub);
CoLockObjectExternal , . , bLock, , . , , . , CoLockObjectExternal, , (Running Object Table) . , , , . , , , , , . , :
STDMETHODIMP Monitor::StartMonitoring(void) { // ensure that stub manager/object stays alive // , / HRESULT hr = CoLockObjectExternal(this, TRUE, FALSE); // start hardware monitoring // if (SUCCEEDED(hr)) hr = this->EnableHardwareProbe(); return hr; }
, :
STDMETHODIMP Monitor::StopMonitoring(void) { // stop hardware monitoring // this->DisableHardwareProbe(); // allow stub manager/object to die when no clients exist // / // , hr = CoLockObjectExternal(this, FALSE, TRUE); return hr; }
, , , .
, , . API- CoDisconnectObject, , :
HRESULT CoDisconnectObject( [in] Unknown * pUnkObject, // ptr to object // [in] DWORD dwReserved // reserved, must be zero // , );
CoLockObjectExternal, CoDisconnectObject . CoDisconnectObject , , , . , , CoDisconnectObject, :
STDMETHODIMP Monitor::GetSample(/*[out]*/ SAMPLEDATA *ps) { HRESULT hr = this->GetSampleFromProbe(ps); if (FAILED(hr)) // probe or object may be corrupted // CoDisconnectObject(this, 0); return hr; }
CoDisconnectObject , , . CoDisconnectObject , , , ORPC- , . ORPC- , , , , . , .
- CoLockObjectExternal CoDisconnectObject . , - , (strong marshals). , , IExternalConnection, :
[ uuid(00000019-0000-0000-C000-000000000046), object, local ] interface IExternalConnection : IUnknown { DWORD AddConnection( [in] DWORD extconn, // type of reference // [in] DWORD reserved // reserved, must be zero // , ); DWORD ReleaseConnection( [in] DWORD extconn, // type of reference // [in] DWORD reserved, // reserved, must be zero // , [in] BOOL fLastReleaseCloses // should kill stub? // ? ); }
, , . IExternalConnection QueryInterface. IExternalConnection, , . IExternalConnection, , CoDisconnectObject.
, , IExternalConnection, , AddConnection ReleaseConnection. AddConnection , . , , AddConnection ReleaseConnection, . , , , , , . CoLockObjectExternal . , . , . , , , , , . , IExternalConnection , . , IExternalConnection :
class Monitor : public IExternalConnection, public IMonitor { LONG m_cRef; // normal COM reference count // LONG m_cExtRef; // external reference count // Monitor(void) : m_cRef(0), m_cExtRef(0) { ... } STDMETHODIMP_(DWORD) AddConnection(DWORD extconn, DWORD) { if (extconn & EXTCONN_STRONG) // must check for this bit // return InterlockedIncrement(&m_cExtRef); } STDMETHODIMP_(DWORD) ReleaseConnection(DWORD extconn, DWORD, BOOL bLastUnlockKillsStub) { DWORD res = 0; if (extconn & EXTCONN_STRONG) { // must check for this bit // res = InterlockedDecrement(&m_cExtRef); if (res == 0 && bLastUnlockKillsStub) CoDisconnectObject(this, 0); } return res; } } : : : : : : }
, , , :
DWORD WINAPI ThreadProc(void *pv) { // assume ptr to real object is passed to CreateThread // CreateThread Monitor *pm = (Monitor*)pv; while (1) { // sleep for 10 seconds // 10 Sleep(1OOOO); // if object is not in use, perform a log operation // , if (pm->m_cExtRef == 0) pm->TryToLogSampleData(); } return 0; }
, TryToLogSampleData , , ( , CoLockObjectExternal). , , . 6 .
1 OR , , .