CHAPTER II RELEASE OF RESOURCES

Preface

Trojan horse files will use the resource release technology extensively to make the program more concise; If the program needs to load additional DLL files, pictures, etc., you can insert them into the program as resources, and after the program runs, the files are released locally; The advantage is that the compiled program already has an exe.

Function introduction

FindResource function

Determines the location of resources of the specified type and name in the specified module.

grammar

HRSRC FindResource(
  [in, optional] HMODULE hModule,
  [in]           LPCSTR  lpName,
  [in]           LPCSTR  lpType
);

parameter

[in, optional] hModule
Module handle to a portable executable resource. NULL loads resources from the current process module.

[in] lpName
Resource name.

[in] lpType
Resource type.

Return value

If the function succeeds, the return value is the handle to the specified resource information block.
If the function fails, the return value is NULL.

SizeofResource function

Retrieves the size of the specified resource.

grammar

DWORD SizeofResource(
  [in, optional] HMODULE hModule,
  [in]           HRSRC   hResInfo
);

parameter

[in, optional] hModule
The executable contains the handle to the module of the resource.

[in] hResInfo
Handle to the resource.

Return value

If the function succeeds, the return value is the number of bytes in the resource.
If the function fails, the return value is zero.

LoadResource function

Loads the specified resource to global storage.

grammar

HGLOBAL LoadResource(
  [in, optional] HMODULE hModule,
  [in]           HRSRC   hResInfo
);

parameter

[in, optional] hModule
The executable contains the handle to the module of the resource. If the hModule is NULL, the system loads resources from the module used to create the current process.

[in] hResInfo
Handle to the resource to load. Returned by the FindResource function.

Return value

If the function succeeds, the return value is the handle to the data associated with the resource.
If the function fails, the return value is NULL.

LockResource function

Lock the resource and get a pointer to the first byte of the resource in memory.

grammar

LPVOID LockResource(
  [in] HGLOBAL hResData
);

parameter

[in] hResData
Handle to the resource to access.

Return value

The loaded resource is available, and the return value is a pointer to the first byte of the resource.
Otherwise, it is NULL.

Example

Create the file test.txt locally:

Right-click Project ->Add ->Resource ->Custom Resource ->Resource just added ->Import ->Select File


Code

#include <Windows.h>
#include <stdio.h>
#include "resource.h"


// Extract Resources
BOOL FreeMyResourse(UINT uiResouceName, char* lpszResourceType, char* lpszSaveFileName)
{
	//Gets the resources of the specified module
	HRSRC hRsrc = FindResource(NULL, MAKEINTRESOURCE(uiResouceName), lpszResourceType);
	if (hRsrc == NULL)
	{
		printf("can't find the resource!\n");
		return FALSE;
	}
	//Get the size of the resource
	DWORD dwSize = SizeofResource(NULL, hRsrc);
	if (dwSize <= 0)
	{
		printf("the resource's size is error!\n");
		return FALSE;
	}
	//Load resources into memory
	HGLOBAL hGlobal = LoadResource(NULL, hRsrc);
	if (hGlobal == NULL)
	{
		printf("load resource error!\n");
		return FALSE;
	}
	//Lock Resources
	LPVOID lpVoid = LockResource(hGlobal);
	if (lpVoid == NULL)
	{
		printf("lock resource error!\n");
		return FALSE;
	}
	//Save resources as files
	FILE* fp = NULL;
	fopen_s(&fp, lpszSaveFileName, "wb+");
	if (fp == NULL)
	{
		printf("open file error!\n");
		return FALSE;
	}
	fwrite(lpVoid, sizeof(char), dwSize, fp);
	fclose(fp);
	return TRUE;
}

int main()
{
	char lpszResourceType[20] = "TEST";
	char szSaveFileName[20] = "test.txt";
	BOOL flag = FreeMyResourse(IDR_MYRES2, lpszResourceType, szSaveFileName);
	if (flag == TRUE)
	{
		printf("Release succeeded\n");
	}
	return 0;
}

test

Double-click Resource Release.exe to add the file "test.txt" to the directory

Principle Learning

Using the FindResource function, the resource handles are obtained by locating the resources in the program through the resource name and type.

Use SizeofResource to get the size of the resource based on the resource handle.

LoadReource loads resources into program memory.

LockResource locks the resources loaded into memory, returning the program's starting address in memory.

Depending on the size of the resource and the starting address in memory, the resource is read and saved as a local file.

Tags: C++ Programming

Posted on Wed, 01 Dec 2021 18:18:43 -0500 by flipis