代码改变世界

Visual C++ 2011-8-15

  Clingingboy  阅读(711)  评论(0编辑  收藏  举报

 

1.SetWindowLongPtr && GetWindowLongPtr

SetWindowLong和GetWindowLong,兼容32位和64位

This function supersedes the SetWindowLong function. To write code that is compatible with both 32-bit and 64-bit versions of Windows, use SetWindowLongPtr.

2.资源相关函数

(1)SizeofResource

Returns the size, in bytes, of the specified resource.

(2)LoadResource

Returns a handle to be used to obtain a pointer to the first byte of the resource in memory.

(3)LockResource

Obtains a pointer to the specified resource in memory.

Note In Windows XP and later, LockResource does not actually lock memory; it is just used to obtain a pointer to the memory containing the resource data.

(4)FreeResource

Decrements (decreases by one) the reference count of a loaded resource. When the reference count reaches zero, the memory occupied by the resource is freed.

以下示例载入一个xml文件

static BOOL _LoadEmbedResource(UINT uResID, CStringA &strRet, LPCTSTR lpszResType)
{
    HRSRC hRsrc = ::FindResource((HINSTANCE)&__ImageBase, MAKEINTRESOURCE(uResID), lpszResType);

    if (NULL == hRsrc)
        return FALSE;

    DWORD dwSize = ::SizeofResource((HINSTANCE)&__ImageBase, hRsrc); 
    if (0 == dwSize)
        return FALSE;

    HGLOBAL hGlobal = ::LoadResource((HINSTANCE)&__ImageBase, hRsrc); 
    if (NULL == hGlobal)
        return FALSE;

    LPVOID pBuffer = ::LockResource(hGlobal); 
    if (NULL == pBuffer)
        return FALSE;

    memcpy(strRet.GetBuffer(dwSize + 1), pBuffer, dwSize);
    strRet.ReleaseBuffer(dwSize);

    ::FreeResource(hGlobal);

    return TRUE;
}

3.关于CString的GetBuffer和ReleaseBuffer

http://blog.sina.com.cn/s/blog_4b3c1f950100ksy2.html

http://blog.csdn.net/philip1106/article/details/1823135

http://blog.csdn.net/philip1106/article/details/1823701

4.CoInitialize和CoInitializeEx区别

(1)CoInitialize:Initializes the COM library on the current thread and identifies the concurrency model as single-thread apartment (STA).

(2)CoInitializeEx:Initializes the COM library for use by the calling thread, sets the thread's concurrency model, and creates a new apartment for the thread if one is required.(COINIT_APARTMENTTHREADED and COINIT_MULTITHREADED )

5.c++ Singleton模式的使用

这个例子使用BkString的_Instance() 载入xml字符串

class BkString
{
protected:

    typedef CAtlMap<UINT, CString> _TypeStringResPool;

public:
    BkString()
    {
    }
    virtual ~BkString()
    {
        m_mapString.RemoveAll();
    }

    static LPCTSTR Get(UINT uResID)
    {
        const _TypeStringResPool::CPair* pPair = _Instance()->m_mapString.Lookup(uResID);
        if (!pPair)
        {
            BKRES_ASSERT(FALSE, L"Failed loading string %u", uResID);

            return NULL;
        }

        return pPair->m_value;
    }

    static BOOL Load(UINT uResID)
    {
        CStringA strXml;
        BOOL bRet = FALSE;
        
        bRet = BkResManager::LoadResource(uResID, strXml);
        if (!bRet)
            return FALSE;

        TiXmlDocument xmlDoc;

        xmlDoc.Parse(strXml, NULL, TIXML_ENCODING_UTF8);

        if (xmlDoc.Error())
            return FALSE;

        LPCSTR lpszStringID = NULL;
        UINT uStringID = 0;

        TiXmlElement *pXmlStringRootElem = xmlDoc.RootElement();

        if (!pXmlStringRootElem)
            return FALSE;

        if (strcmp(pXmlStringRootElem->Value(), "string") != 0)
            return FALSE;

        for (TiXmlElement* pXmlChild = pXmlStringRootElem->FirstChildElement("s"); NULL != pXmlChild; pXmlChild = pXmlChild->NextSiblingElement("s"))
        {
            lpszStringID = pXmlChild->Attribute("id");
            if (!lpszStringID)
                continue;

            uStringID = (UINT)(ULONG)::StrToIntA(lpszStringID);

            {
                _Instance()->m_mapString[uStringID] = CA2T(pXmlChild->GetText(), CP_UTF8);
            }
        }

        return TRUE;
    }

    static size_t GetCount()
    {
        return _Instance()->m_mapString.GetCount();
    }

protected:

    _TypeStringResPool m_mapString;

    static BkString* ms_pInstance;

    static BkString* _Instance()
    {
        if (!ms_pInstance)
            ms_pInstance = new BkString;
        return ms_pInstance;
    }
};
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 25岁的心里话
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2010-08-15 数据结构-图(带权图)(js)
2010-08-15 数据结构-图(非带权图)(js)
点击右上角即可分享
微信分享提示