// HwDemo.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include "HijackTingMix.h"

using namespace std;

ULONG_PTR bp1, bp2, bp3, bp4;

LONG NTAPI ExceptionHandler(EXCEPTION_POINTERS* ExceptionInfo)
{
	cout << "异常地址:" << ExceptionInfo->ExceptionRecord->ExceptionAddress << endl;

	if ((ULONG_PTR)ExceptionInfo->ExceptionRecord->ExceptionAddress == bp2)
	{
		cout << "bp2异常" << endl;
		//DelHwBreakpoint(ExceptionInfo->ContextRecord, bp2);
		ExceptionInfo->ContextRecord->EFlags |= 1 << 16; //0x10000
		return EXCEPTION_CONTINUE_EXECUTION;
	}
	else if ((ULONG_PTR)ExceptionInfo->ExceptionRecord->ExceptionAddress == bp3)
	{
		cout << "bp3异常" << endl;
		//DelHwBreakpoint(ExceptionInfo->ContextRecord, bp3);
		ExceptionInfo->ContextRecord->EFlags |= 1 << 16; //0x10000
		return EXCEPTION_CONTINUE_EXECUTION;
	}
	else if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_SINGLE_STEP) {//STATUS_SINGLE_STEP
		cout << "写入异常" << endl;
		DelHwBreakpoint(ExceptionInfo->ContextRecord, bp1);
		//ExceptionInfo->ContextRecord->EFlags |= 1 << 16; //0x10000
		return EXCEPTION_CONTINUE_EXECUTION;
	}

	return EXCEPTION_CONTINUE_SEARCH;
}

int main()
{
	AddVectoredExceptionHandler(1, ExceptionHandler);

	std::string str = "123456789";
	bp1 = (ULONG_PTR)str.c_str();
	bp2 = (ULONG_PTR)GetProcAddress(GetModuleHandle(L"user32.dll"), "IsWindow");
	bp3 = (ULONG_PTR)GetProcAddress(GetModuleHandle(L"user32.dll"), "GetWindow");

	cout << hex << "bp1:" << bp1 << endl;
	SetHwBreakpoint(0, TYPE_WRITE, SIZE_4, bp1);
	SetHwBreakpoint(0, TYPE_EXECUTE, SIZE_1, bp2);
	SetHwBreakpoint(0, TYPE_EXECUTE, SIZE_1, bp3);

	*(BYTE*)(bp1 + 1) = 0x35;
	IsWindow(NULL);
	GetWindow(NULL, 1);

	*(BYTE*)(bp1 + 1) = 0x36;
	IsWindow(NULL);
	GetWindow(NULL, 1);
	system("pause");
	return 0;
}