c++ 写入并读取资源文件

// ResourceManager.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <Windows.h>
#include <atlconv.h>

#define LANGUAGEID 1033

HANDLE hUpdate;
char currentPath[MAX_PATH];
char deadCode[] = "0xDEADC0DE";
unsigned int error = 0;

int _tmain(int argc, _TCHAR* argv[])
{
    USES_CONVERSION;

    GetCurrentDirectoryA(MAX_PATH, currentPath);
    lstrcatA(currentPath, "\stub.exe");

    

    hUpdate = BeginUpdateResourceW(A2W(currentPath), FALSE);

    if(hUpdate == NULL)
    {
        MessageBox(0, L"BeginUpdateResource failed.", 0, MB_OK+MB_ICONERROR);
        error = 1;
    }

    if(UpdateResourceW(hUpdate, RT_STRING, TEXT("CURRENT"), LANGUAGEID, &deadCode, 11) == FALSE)
    {
        MessageBox(0, L"UpdateResource failed.", 0, MB_OK+MB_ICONERROR);
        error = 1;
    }

    if(EndUpdateResourceW(hUpdate, FALSE) == FALSE)
    {
        MessageBox(0, L"EndUpdateResource failed.", 0, MB_OK+MB_ICONERROR);
        error = 1;
    }

    HMODULE hFile = LoadLibrary(A2W(currentPath));
    HRSRC hRes = FindResourceExW(hFile, RT_STRING, TEXT("CURRENT"), LANGUAGEID);
    if(hRes)
    {
        char *charData = (char*)LockResource(LoadResource(hFile,hRes));
        printf("%s",charData);
    }

    if(error == 0)
    {
        MessageBox(0, L"stub.exe - Resource added.", L"Info", 0);
        return EXIT_SUCCESS;
    }
    else
    {
        MessageBox(0, L"stub.exe - Adding resource failed.", L"Info", 0);
        return EXIT_FAILURE;
    }

    

    return 0;
}
原文地址:https://www.cnblogs.com/nanfei/p/14090892.html