逆向 | 使用资源表的代码模板
逆向 | 使用资源表的代码模板
代码如下:
// resourcetest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <Windows.h>
#include "resource.h"
HINSTANCE g_hinstance;
bool ExportToFile(const std::wstring& exportFilePath, const void* pBuffer, DWORD bufferLength)
{
if (pBuffer == NULL || bufferLength <= 0)
{
return false;
}
HANDLE hFile = ::CreateFile(exportFilePath.c_str(),
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (hFile == NULL)
{
return false;
}
DWORD writetem = -1;
BOOL ret = ::WriteFile(hFile, pBuffer, bufferLength, &writetem, NULL);
if (writetem != bufferLength)
{
::CloseHandle(hFile);
return false;
}
::CloseHandle(hFile);
return true;
}
/**
* exportPath:文件路径,
* resourceId:资源ID :Resource.h中
* 导出资源包转成指定文件
*/
bool ExportRes(const std::wstring& exportPath, DWORD resourceId)
{
HINSTANCE m_hInstance = g_hinstance;
// "ZIP" 是自定义资源类型,可以自己决定
HRSRC hrSrc = FindResource(m_hInstance, MAKEINTRESOURCE(resourceId), L"MZFILE");
if (hrSrc == NULL)
{
return false;
}
HGLOBAL hGlobalResource = LoadResource(m_hInstance, hrSrc);
if (hGlobalResource == NULL)
{
return false;
}
const void* pResourceData = ::LockResource(hGlobalResource);
if (!pResourceData)
{
return false;
}
DWORD resLength = SizeofResource(m_hInstance, hrSrc);
bool ret = ExportToFile(exportPath, pResourceData, resLength);
FreeResource(hGlobalResource);
return ret;
}
int main()
{
g_hinstance = GetModuleHandle(0);
std::wstring wpath1 = L"ucrtbased.dll";
std::wstring wpath2 = L"vcruntime140d.dll";
ExportRes(wpath1, IDR_MZFILE2);
ExportRes(wpath2, IDR_MZFILE3);
std::cout << "Hello World!\n";
}
本文来自博客园,作者:Mz1,转载请注明原文链接:https://www.cnblogs.com/Mz1-rc/p/18722701
如果有问题可以在下方评论或者email:mzi_mzi@163.com