int
Exception::SetHardWareBreakPoint(const wchar_t
*
main_modulename, DWORD64 dr7_statu, DWORD64 dr0, DWORD64 dr1, DWORD64 dr2, DWORD64 dr3)
{
this
-
>_dr0
=
dr0;
this
-
>_dr1
=
dr1;
this
-
>_dr2
=
dr2;
this
-
>_dr3
=
dr3;
/
/
遍历线程 通过openthread获取到线程环境后设置硬件断点
HANDLE hTool32
=
CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD,
0
);
if
(hTool32 !
=
INVALID_HANDLE_VALUE)
{
THREADENTRY32 thread_entry32;
/
/
线程环境结构体
thread_entry32.dwSize
=
sizeof(THREADENTRY32);
HANDLE h_hook_thread
=
NULL;
MODULEINFO module_info
=
{
0
};
/
/
模块信息
HANDLE target_modulehandle
=
GetModuleHandleW(main_modulename);
/
/
从 ntdll.dll 中取出 ZwQueryInformationThread
(FARPROC&)ZwQueryInformationThread
=
::GetProcAddress(GetModuleHandleA(
"ntdll"
),
"ZwQueryInformationThread"
);
if
(target_modulehandle !
=
0
)
{
/
/
获取模块结束地址
GetModuleInformation(GetCurrentProcess(), (HMODULE)target_modulehandle, &module_info, sizeof(MODULEINFO));
__int64 target_modulehandle_endaddress
=
((__int64)module_info.lpBaseOfDll
+
module_info.SizeOfImage);
/
/
遍历线程
if
(Thread32First(hTool32, &thread_entry32))
{
do
{
/
/
如果线程父进程
ID
为当前进程
ID
if
(thread_entry32.th32OwnerProcessID
=
=
GetCurrentProcessId())
{
h_hook_thread
=
OpenThread(THREAD_ALL_ACCESS, FALSE, thread_entry32.th32ThreadID);
/
/
获取线程入口地址
PVOID startaddr;
/
/
用来接收线程入口地址
ZwQueryInformationThread(h_hook_thread, (THREADINFOCLASS)ThreadQuerySetWin32StartAddress, &startaddr, sizeof(startaddr), NULL);
if
(((__int64)startaddr >
=
(__int64)target_modulehandle) && ((__int64)startaddr <
=
target_modulehandle_endaddress))
{
/
/
暂停线程
ULONG previous_count
=
NULL;
NtSuspendThread(h_hook_thread, &previous_count);
/
/
设置硬件断点
CONTEXT thread_context
=
{ CONTEXT_DEBUG_REGISTERS };
thread_context.ContextFlags
=
CONTEXT_ALL;
/
/
得到指定线程的环境(上下文)
if
(!GetThreadContext(h_hook_thread, &thread_context))
return
3
;
thread_context.Dr0
=
dr0;
thread_context.Dr1
=
dr1;
thread_context.Dr2
=
dr2;
thread_context.Dr3
=
dr3;
thread_context.Dr7
=
dr7_statu;
if
(NtSetContextThread(h_hook_thread, &thread_context) !
=
NULL)
return
4
;
if
(!GetThreadContext(h_hook_thread, &thread_context))
return
3
;
/
/
恢复线程
NtResumeThread(h_hook_thread, &previous_count);
}
CloseHandle(h_hook_thread);
}
}
while
(Thread32Next(hTool32, &thread_entry32));
}
CloseHandle(hTool32);
return
true;
}
else
return
2
;
/
/
模块句柄获取失败
}
return
0
;
}