我测试发现,CFileDialog要在安装VC的环境上能弹出来,需要调用AfxOleInit();
而我的这个程序是DCOM的客户端,为了保证与服务端交互数据,又需要调用如下代码:HRESULT hr;    hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    _ASSERTE(SUCCEEDED(hr));但是,如果AfxOleInit();放前面调用,就会影响DCOM的使用,如果AfxOleInit();不使用或放在CoInitializeEx后面,就会影响CFileDialog的使用,两者之间有冲突,请问哪位高手有办法解决?感觉DCOM一定得用CoInitializeEx(NULL, COINIT_MULTITHREADED),那么是否CFileDialog不使用AfxOleInit()用其他方式可以?

解决方案 »

  1.   

    不行呀,如果只使用AfxOleInit(),此DCOM就会出现异常情况,由Windows弹出的请求重试或切换进程。
    如果只使用CoInitializeEx(NULL, COINIT_MULTITHREADED)就会出现CFileDialog弹不出对话框,两个同时使用,谁在前,谁有效,所以不知道有什么方法解决?
      

  2.   

    你不要把CFileDialog放到线程中去处理,让线程发消息给主线程让它来处理CFileDialog
      

  3.   

    不是,CFileDialog是菜单操作才有的,线程中并没有
      

  4.   

    那你为什么要用这个函数
    CoInitializeEx(NULL, COINIT_MULTITHREADED)
      

  5.   

    用它是因为程序是DCOM的客户端,如果没有它,大量的数量交互就会出问题。
      

  6.   

    你既然用了CoInitializeEx,为什么还要用AfxOleInit?
      

  7.   

    我也不想用呀,老兄,但是用它,CFileDialog就能用,你说奇怪不?而且发现更奇怪的情况,我把程序放不同的目录运行,情况可能不一样,有的不用AfxOleInit()能运行,有的不行?
      

  8.   

    /////////////////////////////////////////////////////////////////////////////
    // OLE initialization & terminationBOOL AFXAPI AfxOleInit()
    {
    _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);
    if (FAILED(sc))
    {
    // warn about non-NULL success codes
    #ifdef _DEBUG
    TRACE(traceOle, 0, _T("Warning: OleInitialize returned scode = %s.\n"),
    AfxGetFullScodeString(sc));
    #endif
    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();
    ASSERT(pThread);
    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();
    }
    return TRUE;InitFailed:
    AfxOleTerm();
    return FALSE;
    }Applications that do not require the additional functionality enabled by OleInitialize should call CoInitializeEx instead of OleInitialize.
      

  9.   

    谢谢你贴出AfxOleInit()的内部实现,请问这种问题,怎么样解决?
      

  10.   

    是不是只需要用OleInitialize(NULL)就成?我刚才试了一下,好象都可以用了,一会再把数据加大一点试试.
      

  11.   

    谢谢,好象不出现那种错误了,出错的原因是下面的代码引起:
    pThread->m_pMessageFilter = new COleMessageFilter;
    ASSERT(AfxOleGetMessageFilter() != NULL);
    AfxOleGetMessageFilter()->Register();但是,也有一个问题,OleInitialize(NULL)与CoInitializeEx()有什么区别?对于CFileDialog的处理有什么不同?