3,245
社区成员
发帖
与我相关
我的任务
分享
BOOL AFXAPI AfxOleInit2()
{
_AFX_THREAD_STATE* pState = AfxGetThreadState();
ASSERT(!pState->m_bNeedTerm); // calling it twice?
// Special case DLL context to assume that the calling app initializes OLE.
// For DLLs where this is not the case, those DLLs will need to initialize
// OLE for themselves via OleInitialize. This is done since MFC cannot provide
// automatic uninitialize for DLLs because it is not valid to shutdown OLE
// during a DLL_PROCESS_DETACH.
if (afxContextIsDLL)
{
pState->m_bNeedTerm = -1; // -1 is a special flag
return TRUE;
}
// first, initialize OLE
//SCODE sc = ::OleInitialize(NULL);
SCODE sc = ::CoInitializeEx(NULL,COINIT_MULTITHREADED);
//SCODE sc = ::CoInitializeEx(NULL,COINIT_APARTMENTTHREADED);
if (FAILED(sc))
{
// warn about non-NULL success codes
TRACE1("Warning: OleInitialize returned scode = %s.\n",
AfxGetFullScodeString(sc));
goto InitFailed;
}
// termination required when OleInitialize does not fail
pState->m_bNeedTerm = TRUE;
// hook idle time and exit time for required OLE cleanup
CWinThread* pThread; pThread = AfxGetThread();
pThread->m_lpfnOleTermOrFreeLib = AfxOleTermOrFreeLib;
// allocate and initialize default message filter
if (pThread->m_pMessageFilter == NULL)
{
pThread->m_pMessageFilter = new COleMessageFilter;
ASSERT(AfxOleGetMessageFilter() != NULL);
AfxOleGetMessageFilter()->Register();
}
#ifdef _MAC
CWinApp* pApp; pApp = AfxGetApp();
#ifndef _WINDLL
// Mac MFC uses a static version of ole2ui which must be initialized
if (pState->m_bNeedTerm && !::OleUIInitialize(pApp->m_hInstance,
pApp->m_hPrevInstance, SZCLASSICONBOX, SZCLASSRESULTIMAGE))
goto InitFailed;
#endif
_afxPfnOleAuto = NewAEEventHandlerProc(_AfxOleAutoHandler);
if (_afxPfnOleAuto != NULL)
{
AEInstallEventHandler('OLE2', 'AUTO', _afxPfnOleAuto, (long) pApp, false);
}
#endif
return TRUE;
InitFailed:
AfxOleTerm();
return FALSE;
}
// allocate and initialize default message filter
if (pThread->m_pMessageFilter == NULL)
{
pThread->m_pMessageFilter = new COleMessageFilter;
ASSERT(AfxOleGetMessageFilter() != NULL);
AfxOleGetMessageFilter()->Register();
}
跟你贴的源码区别不大,我Debug的时候,单步到这里跳出那个断言失败窗口。
BOOL COleLinkingDoc::Register(COleObjectFactory* pFactory, LPCTSTR lpszPathName)
{
USES_CONVERSION;
ASSERT_VALID(this);
// 程序在这里跳出断言失败窗口
ASSERT(pFactory == NULL ||
AfxIsValidAddress(pFactory, sizeof(COleObjectFactory)));
ASSERT(lpszPathName == NULL || AfxIsValidString(lpszPathName));
ASSERT(m_dwRegister == 0);
// attach the document to the server
ASSERT(m_pFactory == NULL || m_pFactory == pFactory);
m_pFactory = pFactory;
BOOL bResult = TRUE;
// create file moniker based on path name
RELEASE(m_lpMonikerROT);
m_strMoniker.Empty();
if (lpszPathName != NULL)
{
if (CreateFileMoniker(T2COLE(lpszPathName), &m_lpMonikerROT) != S_OK)
bResult = FALSE;
}
// register file moniker as running
if (m_lpMonikerROT != NULL)
{
// see if the object is already running in the ROT
LPRUNNINGOBJECTTABLE lpROT = NULL;
VERIFY(GetRunningObjectTable(0, &lpROT) == S_OK);
ASSERT(lpROT != NULL);
LPUNKNOWN lpUnk;
if (lpROT->GetObject(m_lpMonikerROT, &lpUnk) == S_OK)
{
// fatal error -- can't register same moniker twice!
lpUnk->Release();
RELEASE(m_lpMonikerROT);
return FALSE;
}
// not already running -- so ok to attempt registration
SCODE sc = lpROT->Register(NULL, (LPUNKNOWN)
GetInterface(&IID_IUnknown), m_lpMonikerROT, &m_dwRegister);
lpROT->Release();
m_strMoniker = lpszPathName;
if (sc != S_OK)
bResult = FALSE;
}
// update all objects with new moniker
POSITION pos = GetStartPosition();
COleClientItem* pItem;
while ((pItem = GetNextClientItem(pos)) != NULL)
{
if (pItem->m_bMoniker)
{
ASSERT(pItem->m_lpObject != NULL);
pItem->m_lpObject->SetMoniker(OLEWHICHMK_CONTAINER,
m_lpMonikerROT);
}
}
return bResult;
}