How did I crash my application for debugging purpose?

As I said in my previous posts, I’ve been dealing with Crash Dumps for debugging my application. It’s hectic that no debugging available environment is available in the product machine and the software can be executed only in that Ultra Sound Machine). Enough about my story.

I just had to generate the mini dump for my application and after code review from customer, I was not allowed to put some buggy code in my source generate any exception for the application.

Thanks a lot “CreateRemoteThread” API to inject some code to a target application. It was really helpful for me to generate exception on my application. I just wrote some buggy code and injected to the target process using the above specified API. It’s not a big deal. Just have a look at the following code. Before that this API can be used for many useful functionality not just to put some buggy code and also if you pass wrong PID it may destroy other application and can affect the stability of your machine.

Just create a sample application and use the following code. Pass your target PID to the function and execute at your own risk!!!

DWORD WINAPI ExceptionThread( LPVOID
pVoid )
{
     // Put some buggy code
     RaiseException( EXCEPTION_ACCESS_VIOLATION,
0,0,0);
     return 0;
}

void InjectBuggyCode( DWORD dwPID )
{
    // Process access parameters
    const DWORD PROC_ACCESS = PROCESS_CREATE_THREAD |
    PROCESS_QUERY_INFORMATION | 

    PROCESS_VM_OPERATION |
    PROCESS_VM_WRITE |
    PROCESS_VM_READ ;
    // Get the process handle
    HANDLE hProcess = OpenProcess( PROC_ACCESS, FALSE, dwPID );
    if ( hProcess )
    {
        // Create remote thread in target
process
        CreateRemoteThread( hProcess, NULL,
                                       
0, ExceptionThread,
                                        0,
0, 0 );
        CloseHandle( hProcess ); // Close
the handle after use
     }
     else
     {
          cerr<<”\nFailed
to open the process. Error code:”<< GetLastError();
     }
}

Technorati Tags: , , ,

Respond to this post