逆向 | 使用资源表的代码模板

逆向 | 使用资源表的代码模板

代码如下:

// 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";
}





posted @   Mz1  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
点击右上角即可分享
微信分享提示