1 /* based on pausep by Daniel Turini
2  */
3 
4 #define WIN32_LEAN_AND_MEAN
5 #define _WIN32_WINNT 0x0502
6 #include <windows.h>
7 #include <tchar.h>
8 #include <tlhelp32.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 
12 static BOOL
resume_process(DWORD dwOwnerPID)13 resume_process (DWORD dwOwnerPID)
14 {
15   HANDLE        hThreadSnap = NULL;
16   BOOL          bRet        = FALSE;
17   THREADENTRY32 te32        = { 0 };
18 
19   hThreadSnap = CreateToolhelp32Snapshot (TH32CS_SNAPTHREAD, 0);
20   if (hThreadSnap == INVALID_HANDLE_VALUE)
21     return FALSE;
22 
23   te32.dwSize = sizeof (THREADENTRY32);
24 
25   if (Thread32First (hThreadSnap, &te32))
26     {
27       do
28         {
29           if (te32.th32OwnerProcessID == dwOwnerPID)
30             {
31               HANDLE hThread = OpenThread (THREAD_SUSPEND_RESUME, FALSE, te32.th32ThreadID);
32               printf ("Resuming Thread: %u\n", (unsigned int) te32.th32ThreadID);
33               ResumeThread (hThread);
34               CloseHandle (hThread);
35             }
36         }
37       while (Thread32Next (hThreadSnap, &te32));
38       bRet = TRUE;
39     }
40   else
41     bRet = FALSE;
42 
43   CloseHandle (hThreadSnap);
44 
45   return bRet;
46 }
47 
48 static BOOL
process_list(void)49 process_list (void)
50 {
51   HANDLE         hProcessSnap = NULL;
52   BOOL           bRet      = FALSE;
53   PROCESSENTRY32 pe32      = {0};
54 
55   hProcessSnap = CreateToolhelp32Snapshot (TH32CS_SNAPPROCESS, 0);
56 
57   if (hProcessSnap == INVALID_HANDLE_VALUE)
58     return FALSE;
59 
60   pe32.dwSize = sizeof (PROCESSENTRY32);
61 
62   if (Process32First (hProcessSnap, &pe32))
63     {
64       do
65         {
66           printf ("PID:\t%u\t%s\n",
67                   (unsigned int) pe32.th32ProcessID,
68                   pe32.szExeFile);
69         }
70       while (Process32Next (hProcessSnap, &pe32));
71       bRet = TRUE;
72     }
73   else
74     bRet = FALSE;
75 
76   CloseHandle (hProcessSnap);
77 
78   return bRet;
79 }
80 
81 int
main(int argc,char * argv[])82 main (int   argc,
83       char* argv[])
84 {
85   DWORD pid;
86 
87   if (argc <= 1)
88     {
89       process_list ();
90     }
91   else if (argc == 2)
92     {
93       pid = atoi (argv[1]);
94       if (pid == 0)
95         {
96           printf ("invalid: %lu\n", pid);
97           return 1;
98         }
99       else
100         {
101           printf ("process: %lu\n", pid);
102           resume_process (pid);
103         }
104     }
105   else
106     {
107       printf ("Usage:\n"
108               "resume    : show processlist\n"
109               "resume PID: resuming thread\n");
110     }
111 
112   return 0;
113 }
114