代码改变世界

Visual C++ 2011-8-15

2011-08-15 19:45  Clingingboy  阅读(707)  评论(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;
    }
};