首页
社区
课程
招聘
哪位大侠帮忙!DEBUG API应用想让被调试程序单步执行,可调试时怎么实现不了呢
2007-1-19 19:28 5408

哪位大侠帮忙!DEBUG API应用想让被调试程序单步执行,可调试时怎么实现不了呢

2007-1-19 19:28
5408
#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元/年,续费同价!

收藏
点赞7
打赏
分享
最新回复 (2)
雪    币: 139
活跃值: (111)
能力值: ( LV8,RANK:130 )
在线值:
发帖
回帖
粉丝
bookworm 3 2007-1-20 13:54
2
0
under VS2005:

#define ARRAY_SIZE(__x)  ( sizeof(__x) / sizeof(__x[0]) )

int wmain( __in int argc,
           __in_ecount(argc) WCHAR* argv[] )
{
...

    WCHAR    wszCommand[MAX_PATH] = {0};

    // construct commandline parameters
    StringCchCopyW( wszCommand, ARRAY_SIZE(wszCommand), argv[1] );
    for( int i = 2; i < argc; i++ )                                 // any optional parameters?
    {
        StringCchPrintfW( wszCommand, MAX_PATH, L" %s", argv[i] );
    }

    BOOL bDebug = CreateProcess( argv[1],
                                 wszCommand,    // it's the program you wanna debug, not your debugger. Pass it as argv[1] with more parameters, if it's necessary
                                 0,
                                 0,
                                 TRUE,
                                 DEBUG_PROCESS,
                                 0,
                                 0,
                                 &startinfo,
                                 &pi );

    if( TRUE != bDebug )
    {
        // your error handling code
    }

    for(;;)
    {
        WaitForDebugEvent(...);
        ....
    }
雪    币: 207
活跃值: (10)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
forabdm 2007-1-22 09:46
3
0
谢谢指点,我一时领悟不了,我再好好看看,呵呵
游客
登录 | 注册 方可回帖
返回