windows

windows下释放资源中的文件到任意目录

2017-09-05  本文已影响0人  fcc_load

其实本函数还可以增加一个参数为,fileName来指定资源释放后最后的名字

//ptszlPath为资源释放的路径
//nResID为资源id
//resName为资源类型

// 查找并释放资源文件
BOOL UseCustomResource(TCHAR *ptszlPath, int nResID,TCHAR *resName)
{
    //g_hModule是模块基地址
    HRSRC hRsrc = FindResource(g_hModule, MAKEINTRESOURCE(nResID), TEXT(resName));
    if (NULL == hRsrc)
    {
        return FALSE;
    }

    //获取资源的大小
    DWORD dwSize = SizeofResource(g_hModule, hRsrc);
    if (0 == dwSize)
    {
        return FALSE;
    }

    //加载资源
    HGLOBAL hGlobal = LoadResource(g_hModule, hRsrc);
    if (NULL == hGlobal)
    {
        return FALSE;
    }

    //锁定资源
    LPVOID pBuffer = LockResource(hGlobal);
    if (NULL == pBuffer)
    {
        UnlockResource(hGlobal);
        FreeResource(hGlobal);
    }

    BOOL bRt = FALSE; 
    FILE* fp = NULL;
    errno_t err;

    if (nResID == IDR_MYRES2)
    {
        _tcscat_s(ptszlPath, MAX_PATH, TEXT("app.xml"));
        err = _tfopen_s(&fp, ptszlPath, TEXT("wb+"));
    }
    else if(nResID == IDB_PNG1)
    {
        _tcscat_s(ptszlPath, MAX_PATH, TEXT("back.png"));
        err = _tfopen_s(&fp, ptszlPath, TEXT("wb+"));
    }else if(nResID == IDB_PNG2){
        _tcscat_s(ptszlPath, MAX_PATH, TEXT("btn_sys_close.png"));
        err = _tfopen_s(&fp, ptszlPath, TEXT("wb+"));
    }


    if (fp != NULL)
    {
        if (dwSize == fwrite(pBuffer, sizeof(char), dwSize, fp))
        {
            bRt = TRUE;
        }

        fclose(fp);
    }

    UnlockResource(hGlobal);
    FreeResource(hGlobal);

    return bRt;
}

上一篇下一篇

猜你喜欢

热点阅读