#include "windows.h"
#include "iostream.h"
void main()
{
STARTUPINFO startinfo={0};
PROCESS_INFORMATION pi={0};
DEBUG_EVENT DBEvent;
#pragma pack(4)
CONTEXT context;
DWORD TotalInstruction=0;
startinfo.cb=sizeof(startinfo);
if(!CreateProcess(NULL,"\"C:\\Program Files\\Microsoft Visual Studio\\MyProjects\\Msg\\Debug\\Msg.exe\"",NULL,NULL,NULL,
DEBUG_PROCESS|DEBUG_ONLY_THIS_PROCESS,
NULL,NULL,&startinfo,&pi))
cout<<GetLastError()<<endl;
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
while(true){
WaitForDebugEvent(&DBEvent, INFINITE);
switch(DBEvent.dwDebugEventCode)
{
case EXCEPTION_DEBUG_EVENT:
{
if(DBEvent.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT)
{
context.ContextFlags=CONTEXT_CONTROL;
GetThreadContext(pi.hThread,&context);
context.EFlags=(context.EFlags|0x100);
SetThreadContext(pi.hThread,&context);
ContinueDebugEvent(DBEvent.dwProcessId, DBEvent.dwThreadId,DBG_CONTINUE);
continue;
}
if(DBEvent.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP)
{
TotalInstruction+=1;
GetThreadContext(pi.hThread,&context);
context.EFlags=context.EFlags|0x100;
SetThreadContext(pi.hThread, &context);
ContinueDebugEvent(DBEvent.dwProcessId, DBEvent.dwThreadId,DBG_CONTINUE );
continue;
}
break;
}
case EXIT_PROCESS_DEBUG_EVENT:
MessageBox(NULL,"Example program", NULL, MB_OK|MB_ICONINFORMATION);
break;
case CREATE_PROCESS_DEBUG_EVENT:
break;
}
ContinueDebugEvent(DBEvent.dwProcessId, DBEvent.dwThreadId, DBG_EXCEPTION_NOT_HANDLED);
}
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
ExitProcess(0);
}
阿里云助力开发者!2核2G 3M带宽不限流量!6.18限时价,开
发者可享99元/年,续费同价!