Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

A Safer Alternative to TerminateProcess()


July 1999/A Safer Alternative to TerminateProcess()/Listing 3

Listing 3: stptest.c — A demonstration of SafeTerminateProcess()

/*
    Sample test code for SafeTerminateProcess
*/

#define STRICT
#include <windows.h>
#include <stdio.h>
#include "safetp.h"


int main()
{
    STARTUPINFO si = { sizeof(si) };
    PROCESS_INFORMATION pi;

    BOOL b = CreateProcess("Sample.exe", NULL, 
                            NULL, NULL, FALSE, 0, NULL, NULL, 
                            &si, &pi);

    if ( b )
    {
        // let the target process start running...
        Sleep(1000);

        if ( SafeTerminateProcess(pi.hProcess, 0xDEADBEEF) )
        {
            DWORD dwT, dwP;

            GetExitCodeProcess(pi.hProcess, &dwP);
            GetExitCodeThread(pi.hThread, &dwT);

            if ( dwT != 0xDEADBEEF )
                printf("error: incorrect process exit code\n");

            if ( dwT != dwP )
                printf("error: incorrect thread exit code\n");

            CloseHandle(pi.hProcess);
            CloseHandle(pi.hThread);
        }

        else
        {
            DWORD dwErr = GetLastError();
            printf("error = %d\n", dwErr);
        }
    }
    else
        printf("error launching sample.exe\n");


    return 0;
}
/* End of File */

Related Reading


More Insights