UpDateResource

本文介绍了一种更新和删除资源的方法,包括将资源从一个文件更新到另一个文件的过程,并提供了具体的实现代码示例。此外还讨论了如何通过不同方式来替换资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

资源操作

1.更新 / 删除资源


DWORD UpDateFilResource(char* pFileName)
{
    DWORD dwSize = MultiByteToWideChar(CP_ACP, 0, pFileName, -1, NULL, 0);
    int dn = 2 * dwSize;
    wchar_t *wpFileName = (wchar_t*)malloc(dn);

    if (!wpFileName)
    {
        free(wpFileName);
    }
    charTowchar(pFileName, wpFileName, 2 * dwSize);


    char cExeRes[MAX_PATH] = "E:\\Hash.exe";
    DWORD dwSize1 = MultiByteToWideChar(CP_ACP, 0, cExeRes, -1, NULL, 0);
    int dn1 = 2 * dwSize1;
    wchar_t *wpFileName1 = (wchar_t*)malloc(dn1);

    if (!wpFileName1)
    {
        free(wpFileName1);
    }
    charTowchar(cExeRes, wpFileName1, 2 * dwSize1);



    HANDLE hUpdateRes;
    BOOL result;
    /*-----------1-查找 exe 或者 dll 中的资源更新更新到文件
    HGLOBAL hResLoad;   
    HMODULE hExe;       
    HRSRC hRes;         
    LPVOID lpResLock;   

    hExe = LoadLibrary(wpFileName);
    if (hExe == NULL)
    {
        return 0;
    }

    hRes = FindResource(hExe, MAKEINTRESOURCE(7801), MAKEINTRESOURCE(8005));
    if (hRes == NULL)
    {
        return 0;
    }

    hResLoad = LoadResource(hExe, hRes);
    if (hResLoad == NULL)
    {
        return 0;
    }

    lpResLock = LockResource(hResLoad);
    if (lpResLock == NULL)
    {
        OutputDebugString(TEXT("Could not lock dialog box."));
        return 0;
    }*/

    /* --------2-将 exe 或者 dll 作为资源更新更新到文件
    HANDLE hFile = CreateFile(wpFileName1, GENERIC_READ | GENERIC_WRITE,
        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile == INVALID_HANDLE_VALUE)
    {
        return 0;
    }

    // 读入文件
    HANDLE hMap = CreateFileMapping(hFile, NULL, 2, NULL, NULL, NULL);
    LPVOID  lpBaseAddr = MapViewOfFile(hMap, OPEN_ALWAYS, 0, 0, 0);
    if (NULL == lpBaseAddr)
    {
        return 0;
    }


    hUpdateRes = BeginUpdateResource(wpFileName, FALSE);
    if (hUpdateRes == NULL)
    {
        OutputDebugString(TEXT("Could not open file for writing."));
        return 0;
    }

    result = UpdateResource(hUpdateRes,    
        MAKEINTRESOURCE(8005),             
        MAKEINTRESOURCE(7801),              
        0x804,  
        lpBaseAddr,                         
        GetFileSize(hFile,0));       

    if (result == FALSE)
    {
        return 0;
    }

    if (!EndUpdateResource(hUpdateRes, FALSE))
    {
        return 0;
    }
    */

    //---------------3-删除资源------------------
    hUpdateRes = BeginUpdateResource(wpFileName, FALSE);
    if (hUpdateRes == NULL)
    {
        OutputDebugString(TEXT("Could not open file for writing."));
        return 0;
    }

    result = UpdateResource(hUpdateRes,    
        MAKEINTRESOURCE(8005),             
        MAKEINTRESOURCE(7801),              
        0x804,  
        NULL,                   // 主要是这两个参数。         
        NULL);                  // 如果上面为NULL,这不为Null ,抛出异常,可以作为反调试。

    if (result == FALSE)
    {
        return 0;
    }

    if (!EndUpdateResource(hUpdateRes, FALSE))
    {
        return 0;
    }



    /*if (!FreeLibrary(hExe))
    {
        OutputDebugString(TEXT("Could not free executable."));
        return 0;
    }*/

    return 1;
}
//char to wchar_t
void charTowchar( char *chr, wchar_t *wchar, int size)
{
    /*
    DWORD dwSize = MultiByteToWideChar(CP_ACP, 0, chr, -1, NULL, 0);
    int dn = 2 * dwSize;
    wchar_t *wchr = (wchar_t*)malloc(dn);

    if (!wchr)
    {
    free(wchr);
    }
    charTowchar(chr, wchr, 2 * dwSize);
    */

    MultiByteToWideChar(CP_ACP, 0, chr,
        -1/*strlen(chr) + 1*/, wchar, size / sizeof(wchar[0]));
}

2.替换资源

3.枚举

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值