5 * Created by Toshi Nagata on 10/04/29.
6 * Copyright 2010 __MyCompanyName__. All rights reserved.
10 #if defined(__WXMSW__)
12 #include "wx/wxprec.h"
18 #include <wx/msw/private.h> // includes <windows.h>
21 #include <wx/msw/missing.h> // CHARSET_HANGUL
23 // #include <wx/app.h>
29 #include <wx/msw/registry.h>
30 #include <wx/apptrait.h>
31 #include <wx/dynlib.h>
32 #include <wx/dynload.h>
33 #include <wx/scopeguard.h>
35 #include <wx/confbase.h> // for wxExpandEnvVars()
37 #if defined(__CYGWIN__)
38 //CYGWIN gives annoying warning about runtime stuff if we don't do this
39 # define USE_SYS_TYPES_FD_SET
40 # include <sys/types.h>
43 // Doesn't work with Cygwin at present
44 #if wxUSE_SOCKETS && (defined(__GNUWIN32_OLD__) || defined(__WXWINCE__) || defined(__CYGWIN32__))
45 // apparently we need to include winsock.h to get WSADATA and other stuff
46 // used in wxGetFullHostName() with the old mingw32 versions
50 #if !defined(__GNUWIN32__) && !defined(__SALFORDC__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
58 #if defined(__CYGWIN__)
59 #include <sys/unistd.h>
61 #include <sys/cygwin.h> // for cygwin_conv_to_full_win32_path()
64 #ifdef __BORLANDC__ // Please someone tell me which version of Borland needs
65 // this (3.1 I believe) and how to test for it.
66 // If this works for Borland 4.0 as well, then no worries.
70 // VZ: there is some code using NetXXX() functions to get the full user name:
71 // I don't think it's a good idea because they don't work under Win95 and
72 // seem to return the same as wxGetUserId() under NT. If you really want
73 // to use them, just #define USE_NET_API
80 #if defined(__WIN32__) && !defined(__WXMICROWIN__) && !defined(__WXWINCE__)
91 #if !(defined(_MSC_VER) && (_MSC_VER > 800))
96 // For wxKillAllChildren
99 // Taken from wxWidgets source, and modified so that recursive killing of
100 // child processes can be done
102 typedef HANDLE (WINAPI *CreateToolhelp32Snapshot_t)(DWORD,DWORD);
103 typedef BOOL (WINAPI *Process32_t)(HANDLE,LPPROCESSENTRY32);
105 static CreateToolhelp32Snapshot_t lpfCreateToolhelp32Snapshot;
106 static Process32_t lpfProcess32First, lpfProcess32Next;
108 static void InitToolHelp32()
110 static bool s_initToolHelpDone = false;
112 if (s_initToolHelpDone)
115 s_initToolHelpDone = true;
117 lpfCreateToolhelp32Snapshot = NULL;
118 lpfProcess32First = NULL;
119 lpfProcess32Next = NULL;
121 #if wxUSE_DYNLIB_CLASS
123 wxDynamicLibrary dllKernel(_T("kernel32.dll"), wxDL_VERBATIM);
125 // Get procedure addresses.
126 // We are linking to these functions of Kernel32
127 // explicitly, because otherwise a module using
128 // this code would fail to load under Windows NT,
129 // which does not have the Toolhelp32
130 // functions in the Kernel 32.
131 lpfCreateToolhelp32Snapshot =
132 (CreateToolhelp32Snapshot_t)dllKernel.RawGetSymbol(_T("CreateToolhelp32Snapshot"));
135 (Process32_t)dllKernel.RawGetSymbol(_T("Process32First"));
138 (Process32_t)dllKernel.RawGetSymbol(_T("Process32Next"));
140 #endif // wxUSE_DYNLIB_CLASS
149 /* Mark the process with pid and all its children */
151 sMarkProcessAndChildren(long pid, sPidListEntry *myList, int count, long *toKill, int *countToKill)
155 // Add the process with this pid to the list
156 for (i = 0, ep = myList; i < count; i++, ep++) {
159 if (ep->pid == pid && *countToKill < count) {
160 toKill[*countToKill] = pid;
166 // Look for the child process
167 for (i = 0, ep = myList; i < count; i++, ep++) {
170 if (ep->p_pid == pid)
171 sMarkProcessAndChildren(ep->pid, myList, count, toKill, countToKill);
176 myKillAllChildren(long pid, wxSignal sig, wxKillError *krc)
182 // If not implemented for this platform (e.g. NT 4.0), silently ignore
183 if (!lpfCreateToolhelp32Snapshot || !lpfProcess32First || !lpfProcess32Next)
186 // Take a snapshot of all processes in the system.
187 HANDLE hProcessSnap = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
188 if (hProcessSnap == INVALID_HANDLE_VALUE) {
194 //Fill in the size of the structure before using it.
197 pe.dwSize = sizeof(PROCESSENTRY32);
199 if (!lpfProcess32First(hProcessSnap, &pe)) {
200 // Can't get first process.
203 CloseHandle (hProcessSnap);
207 //Register the pids and parent pids
208 int count = 4, i = 0;
209 sPidListEntry *myList = (sPidListEntry *)malloc(sizeof(sPidListEntry) * count);
215 myList = (sPidListEntry *)realloc(myList, sizeof(sPidListEntry) * count);
219 myList[i].pid = pe.th32ProcessID;
220 myList[i].p_pid = pe.th32ParentProcessID;
223 } while (lpfProcess32Next (hProcessSnap, &pe));
224 CloseHandle (hProcessSnap);
227 // Mark the processes to kill
228 long *toKill = (long *)malloc(sizeof(long) * count);
230 sMarkProcessAndChildren(pid, myList, count, toKill, &i);
234 // Kill the processes
235 for (i = 0; i < count; i++) {
236 if (wxKill(toKill[i], sig, krc))