#pragma once


constexpr int Byte_Length = 6;
class inlinehook
{
private:
	using uchar = unsigned char;

	//原始函数处的汇编指令
	uchar m_original_byte[Byte_Length];
	//我们构造的汇编指令
	uchar m_self_byte[Byte_Length];

	//原始函数地址
	int m_original_address;
	//我们函数地址
	int m_self_address;

	DWORD Motify_memory_attributes(int address, DWORD attributes = PAGE_EXECUTE_READWRITE);


public:

	inlinehook(int original_address, int self_address) :m_original_address(original_address), m_self_address(self_address)
	{
		//构造jmp
		m_self_byte[0] = '\xe9';
		//计算偏移
		int offset = self_address + 1 - (original_address + Byte_Length);

		//构造跳转到我们的函数BYTE
		memcpy(&m_self_byte[1], &offset, Byte_Length - 2);

		m_self_byte[5] = '\x90';
		//修改内存属性
		DWORD attributes = Motify_memory_attributes(original_address);

		//保存原始的函数地址的BYTE
		memcpy(&m_original_byte, reinterpret_cast<void*>(original_address), Byte_Length);

		//恢复内存属性
		Motify_memory_attributes(original_address, attributes);
	}


	//修改地址
	void Motify_address();

	//还原地址
	void Restore_address();
};

