/
/
SleepDemo.cpp : 此文件包含
"main"
函数。程序执行将在此处开始并结束。
/
/
using namespace std;
void __declspec(dllexport) CalculateFunc(
int
count)
{
int
index
=
0
;
for
(
int
i
=
0
; i < count; i
+
+
)
{
printf(
"i=%d, %d...\n"
, i,
+
+
index);
Sleep(
1000
);
}
printf(
"Threader Over...\n"
);
}
void CalculateFuncAsync()
{
std::thread thread(CalculateFunc,
100
);
thread.join();
/
/
等待线程结束
}
int
main()
{
std::thread thread(CalculateFuncAsync);
Sleep(
3000
);
char instructBak[INSTRUCT_LEN]{
0
};
DWORD hookLength
=
INSTRUCT_LEN;
DWORD hookAddress
=
(DWORD)CalculateFunc
+
0x2F
;
SuspendThread(thread.native_handle());
DWORD curProtection;
VirtualProtect((void
*
)hookAddress, hookLength, PAGE_EXECUTE_READWRITE, &curProtection);
memcpy(instructBak, (void
*
)hookAddress, INSTRUCT_LEN);
memset((void
*
)hookAddress,
0x90
, hookLength);
/
/
相当于
break
ResumeThread(thread.native_handle());
thread.join();
printf(
"Main Over...\n"
);
/
/
还原
memcpy((void
*
)hookAddress, (void
*
)instructBak, INSTRUCT_LEN);
DWORD temp;
VirtualProtect((void
*
)hookAddress, hookLength, curProtection, &temp);
return
0
;
}