zoukankan      html  css  js  c++  java
  • 使用RichEdit程序无法启动

    在MFC或者WTL工程里,如果使用了RichEdit控件,会发现程序无法启动,编译器却没给出任何警告或错误。

    在网上找了下资料,原来是没有加载支持RichEdit的动态库。

    在MFC下面添加:

    AfxInitRichEdit();

    在WTL下面添加:

    HMODULE hRichDll = LoadLibrary(_T("riched20.dll"));

    程序结束时:

    if(hRichDll)
    {
        FreeLibrary(hRichDll);
        hRichDll= NULL;
    }

    -------------------------------------------我是分割线----------------------------------------------------

    很好奇AfxInitRichEdit();是不是也同样LoadLibrary呢?还是有其他什么做法?

    BOOL PASCAL AfxInitRichEdit()
    {
        _AFX_RICHEDIT_STATE* pState = _afxRichEditState;
        if (pState->m_hInstRichEdit == NULL)
            pState->m_hInstRichEdit = AfxCtxLoadLibraryW(L"RICHED32.DLL");
        return pState->m_hInstRichEdit != NULL;
    }

    看到主要是调用了AfxCtxLoadLibraryW(L"RICHED32.DLL");

    RICHED32.DLL和riched20.dll只是不同的版本而已,先不做深究。

    继续F12竟然到这一行

    AFX_ISOLATIONAWARE_STATICLINK_FUNC(HMODULE,LoadLibraryW,(LPCWSTR lpLibFileName),(lpLibFileName),NULL)

    是一个宏,MFC的惯用手法

    里面还有两层的宏定义,最后翻译过来结果如下:

     1 inline HMODULE AfxCtxLoadLibraryW(LPCWSTR lpLibFileName)
     2 { 
     3     ULONG_PTR ulActCtxCookie = 0;
     4     BOOL fActCtxSucceeded = ActivateActCtx(AfxGetModuleState()->m_hActCtx, &ulActCtxCookie);
     5     HMODULE result=(NULL);
     6     if (!fActCtxSucceeded)
     7     {
     8         return result;
     9     }
    10     __try {
    11     result=LoadLibraryW(lpLibFileName); 
    12     }
    13     __finally
    14     {
    15         const BOOL fPreserveLastError = (result == (NULL) );
    16         const DWORD dwLastError = fPreserveLastError ? GetLastError() : NO_ERROR;
    17         DeactivateActCtx(0,ulActCtxCookie);
    18         if (fPreserveLastError)
    19         {
    20             SetLastError(dwLastError);
    21         }
    22     }
    23     return result;
    24 }

    主要工作还是调用LoadLibrary,不过多了一对函数的调用,具体参考MSDN:

    ActivateActCtx

    DeactivateActCtx
  • 相关阅读:
    (转)dubbo远程调用细节
    (转)Dubbo扩展点实现细节
    (转)dubbo design
    (转) java中try/catch性能和原理
    mybatis入门基础(九)----逆向工程
    客观评价下软件培训机构
    mybatis入门基础(八)-----查询缓存
    mybatis入门基础(七)----延迟加载
    mybatis入门基础(六)----高级映射(一对一,一对多,多对多)
    为什么我不推荐大家去外包公司
  • 原文地址:https://www.cnblogs.com/aishangxue/p/3502472.html
Copyright © 2011-2022 走看看