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;
}
复制代码

 

posted on   空明流光  阅读(828)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示