]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/utils.cpp
wxUniversal fix.
[wxWidgets.git] / src / msw / utils.cpp
index eb91843d616c5aef2a67f8ed3144457ea7b0479a..901aa039ed6942778303a2c7742ec53bb8995524 100644 (file)
     #endif
 #endif
 
-// 260 was taken from windef.h
-#ifndef MAX_PATH
-    #define MAX_PATH  260
-#endif
+// For wxKillAllChildren
+#include <tlhelp32.h>
 
 // ----------------------------------------------------------------------------
 // constants
@@ -434,12 +432,8 @@ const wxChar* wxGetHomeDir(wxString *pstr)
     }
     else // fall back to the program directory
     {
-        wxString strPath;
-        ::GetModuleFileName(::GetModuleHandle(NULL),
-                            wxStringBuffer(strPath, MAX_PATH), MAX_PATH);
-
-        // extract the dir name
-        wxSplitPath(strPath, &strDir, NULL, NULL);
+        // extract the directory component of the program file name
+        wxSplitPath(wxGetFullModuleName(), &strDir, NULL, NULL);
     }
 #endif  // UNIX/Win
 
@@ -658,8 +652,13 @@ BOOL CALLBACK wxEnumFindByPidProc(HWND hwnd, LPARAM lParam)
     return TRUE;
 }
 
-int wxKill(long pid, wxSignal sig, wxKillError *krc)
+int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc);
+
+int wxKill(long pid, wxSignal sig, wxKillError *krc, int flags)
 {
+    if (flags & wxKILL_CHILDREN)
+        wxKillAllChildren(pid, sig, krc);
+    
     // get the process handle to operate on
     HANDLE hProcess = ::OpenProcess(SYNCHRONIZE |
                                     PROCESS_TERMINATE |
@@ -814,6 +813,111 @@ int wxKill(long pid, wxSignal sig, wxKillError *krc)
     return -1;
 }
 
+HANDLE (WINAPI *lpfCreateToolhelp32Snapshot)(DWORD,DWORD) ;
+BOOL (WINAPI *lpfProcess32First)(HANDLE,LPPROCESSENTRY32) ;
+BOOL (WINAPI *lpfProcess32Next)(HANDLE,LPPROCESSENTRY32) ;
+  
+static void InitToolHelp32()
+{
+    static bool s_initToolHelpDone = false;
+    
+    if (s_initToolHelpDone)
+        return;
+    
+    s_initToolHelpDone = true;
+
+    lpfCreateToolhelp32Snapshot = NULL;
+    lpfProcess32First = NULL;
+    lpfProcess32Next = NULL;
+    
+    HINSTANCE hInstLib = LoadLibrary( wxT("Kernel32.DLL") ) ;
+    if( hInstLib == NULL )
+        return ;
+    
+    // Get procedure addresses.
+    // We are linking to these functions of Kernel32
+    // explicitly, because otherwise a module using
+    // this code would fail to load under Windows NT,
+    // which does not have the Toolhelp32
+    // functions in the Kernel 32.
+    lpfCreateToolhelp32Snapshot=
+        (HANDLE(WINAPI *)(DWORD,DWORD))
+        GetProcAddress( hInstLib,
+#ifdef __WXWINCE__
+        wxT("CreateToolhelp32Snapshot")
+#else
+        "CreateToolhelp32Snapshot"
+#endif
+        ) ;
+    
+    lpfProcess32First=
+        (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
+        GetProcAddress( hInstLib,
+#ifdef __WXWINCE__
+        wxT("Process32First")
+#else
+        "Process32First"
+#endif
+        ) ;
+
+    lpfProcess32Next=
+        (BOOL(WINAPI *)(HANDLE,LPPROCESSENTRY32))
+        GetProcAddress( hInstLib,
+#ifdef __WXWINCE__
+        wxT("Process32Next")
+#else
+        "Process32Next"
+#endif
+        ) ;
+
+    FreeLibrary( hInstLib ) ;
+}
+
+// By John Skiff
+int wxKillAllChildren(long pid, wxSignal sig, wxKillError *krc)
+{
+    InitToolHelp32();
+    
+    if (krc)
+        *krc = wxKILL_OK;
+    
+    // If not implemented for this platform (e.g. NT 4.0), silently ignore
+    if (!lpfCreateToolhelp32Snapshot || !lpfProcess32First || !lpfProcess32Next)
+        return 0;
+    
+    // Take a snapshot of all processes in the system.
+    HANDLE hProcessSnap = lpfCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
+    if (hProcessSnap == INVALID_HANDLE_VALUE) {
+        if (krc)
+            *krc = wxKILL_ERROR;
+        return -1;
+    }
+    
+    //Fill in the size of the structure before using it.
+    PROCESSENTRY32 pe = {0};
+    pe.dwSize = sizeof(PROCESSENTRY32);
+    
+    // Walk the snapshot of the processes, and for each process,
+    // kill it if its parent is pid.
+    if (!lpfProcess32First(hProcessSnap, &pe)) {
+        // Can't get first process.
+        if (krc)
+            *krc = wxKILL_ERROR;
+        CloseHandle (hProcessSnap);
+        return -1;
+    }
+    
+    do {
+        if (pe.th32ParentProcessID == (DWORD) pid) {
+            if (wxKill(pe.th32ProcessID, sig, krc))
+                return -1;
+        }
+    } while (lpfProcess32Next (hProcessSnap, &pe));
+    
+    
+    return 0;
+}
+
 // Execute a program in an Interactive Shell
 bool wxShell(const wxString& command)
 {
@@ -946,8 +1050,39 @@ wxString wxGetOsDescription()
                 break;
 
             case VER_PLATFORM_WIN32_WINDOWS:
-                str.Printf(_("Windows 9%c"),
-                           info.dwMinorVersion == 0 ? _T('5') : _T('8'));
+                switch (info.dwMinorVersion)
+                {
+                    case 0:
+                        if ( info.szCSDVersion[1] == 'B' ||
+                             info.szCSDVersion[1] == 'C' )
+                        {
+                            str = _("Windows 95 OSR2");
+                        }
+                        else
+                        {
+                            str = _("Windows 95");
+                        }
+                        break;
+                    case 10:
+                        if ( info.szCSDVersion[1] == 'B' ||
+                             info.szCSDVersion[1] == 'C' )
+                        {
+                            str = _("Windows 98 SE");
+                        }
+                        else
+                        {
+                            str = _("Windows 98");
+                        }
+                        break;
+                    case 90:
+                        str = _("Windows ME");
+                        break;
+                    default:
+                        str.Printf(_("Windows 9x (%d.%d)"),
+                                   info.dwMajorVersion,
+                                   info.dwMinorVersion);
+                        break;
+                }
                 if ( !wxIsEmpty(info.szCSDVersion) )
                 {
                     str << _T(" (") << info.szCSDVersion << _T(')');
@@ -955,10 +1090,31 @@ wxString wxGetOsDescription()
                 break;
 
             case VER_PLATFORM_WIN32_NT:
-                str.Printf(_T("Windows NT %lu.%lu (build %lu"),
+                if ( info.dwMajorVersion == 5 )
+                {
+                    switch ( info.dwMinorVersion )
+                    {
+                        case 0:
+                            str.Printf(_("Windows 2000 (build %lu"),
+                                       info.dwBuildNumber);
+                            break;
+                        case 1:
+                            str.Printf(_("Windows XP (build %lu"),
+                                       info.dwBuildNumber);
+                            break;
+                        case 2:
+                            str.Printf(_("Windows Server 2003 (build %lu"),
+                                       info.dwBuildNumber);
+                            break;
+                    }
+                }
+                if ( wxIsEmpty(str) )
+                {
+                    str.Printf(_("Windows NT %lu.%lu (build %lu"),
                            info.dwMajorVersion,
                            info.dwMinorVersion,
                            info.dwBuildNumber);
+                }
                 if ( !wxIsEmpty(info.szCSDVersion) )
                 {
                     str << _T(", ") << info.szCSDVersion;