我试图了解自解压 PE 文件的工作原理。有人可以解释为什么我的代码不起作用,或者修复 main() 部分。
#include <iostream>
#include <Windows.h>
using namespace std;
void ExtractResource(const HINSTANCE hInstance, WORD resourceID, const char* outputFilename);
int main()
{
HINSTANCE hInst = GetModuleHandle (0);
ExtractResource(hInst, 101, "101.dll");
ExtractResource(hInst, 102, "102.dll");
ExtractResource(hInst, 103, "103.dll");
ExtractResource(hInst, 104, "104.dll");
cout << "Files are now extracted!";
Sleep(INFINITE);
}
void ExtractResource(const HINSTANCE hInstance, WORD resourceID, const char* outputFilename){
// First find and load the required resource
HRSRC hResource = FindResource(hInstance, MAKEINTRESOURCE(resourceID), "BINARY");
if(hResource==NULL)
return;
HGLOBAL hFileResource = LoadResource(hInstance, hResource);
// Now open and map this to a disk file
LPVOID lpFile = LockResource(hFileResource);
DWORD dwSize = SizeofResource(hInstance, hResource);
// Open the file and filemap
HANDLE hFile = CreateFileA(outputFilename, GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW,
FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN|FILE_ATTRIBUTE_READONLY, NULL);
HANDLE hFilemap = CreateFileMapping(hFile, NULL, PAGE_READWRITE, 0, dwSize, NULL);
LPVOID lpBaseAddress = MapViewOfFile(hFilemap, FILE_MAP_WRITE, 0, 0, 0);
// Write the file
CopyMemory(lpBaseAddress, lpFile, dwSize);
// Unmap the file and close the handles
UnmapViewOfFile(lpBaseAddress);
CloseHandle(hFilemap);
CloseHandle(hFile);
}
我在资源中有 4 个 dll 文件,但我无法使用它来提取它们。资源 ID 应该是正确的,我从资源标题中检查了它。
问题出在哪里,hInst
或者还有什么问题?我希望有人可以帮助我:) 我刚刚开始学习 C 和 C++,所以请原谅我。