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 */