]> git.saurik.com Git - wxWidgets.git/blobdiff - src/msw/utils.cpp
wxUniversal fix.
[wxWidgets.git] / src / msw / utils.cpp
index 696fdc5fa83dbf57dde14075abcecb940246155e..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
@@ -119,17 +117,17 @@ static const wxChar eUSERNAME[]  = wxT("UserName");
 bool wxGetHostName(wxChar *buf, int maxSize)
 {
 #if defined(__WXWINCE__)
-    return FALSE;
+    return false;
 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
     DWORD nSize = maxSize;
     if ( !::GetComputerName(buf, &nSize) )
     {
         wxLogLastError(wxT("GetComputerName"));
 
-        return FALSE;
+        return false;
     }
 
-    return TRUE;
+    return true;
 #else
     wxChar *sysname;
     const wxChar *default_host = wxT("noname");
@@ -139,7 +137,7 @@ bool wxGetHostName(wxChar *buf, int maxSize)
     } else
         wxStrncpy(buf, sysname, maxSize - 1);
     buf[maxSize] = wxT('\0');
-    return *buf ? TRUE : FALSE;
+    return *buf ? true : false;
 #endif
 }
 
@@ -218,7 +216,7 @@ bool wxGetFullHostName(wxChar *buf, int maxSize)
             {
                 wxStrncpy(buf, host, maxSize);
 
-                return TRUE;
+                return true;
             }
         }
     }
@@ -231,7 +229,7 @@ bool wxGetFullHostName(wxChar *buf, int maxSize)
 bool wxGetUserId(wxChar *buf, int maxSize)
 {
 #if defined(__WXWINCE__)
-    return FALSE;
+    return false;
 #elif defined(__WIN32__) && !defined(__WXMICROWIN__)
     DWORD nSize = maxSize;
     if ( ::GetUserName(buf, &nSize) == 0 )
@@ -241,11 +239,11 @@ bool wxGetUserId(wxChar *buf, int maxSize)
         if ( res == 0 )
         {
             // not found
-            return FALSE;
+            return false;
         }
     }
 
-    return TRUE;
+    return true;
 #else   // __WXMICROWIN__
     wxChar *user;
     const wxChar *default_id = wxT("anonymous");
@@ -263,7 +261,7 @@ bool wxGetUserId(wxChar *buf, int maxSize)
         wxStrncpy(buf, user, maxSize - 1);
     }
 
-    return *buf ? TRUE : FALSE;
+    return *buf ? true : false;
 #endif
 }
 
@@ -271,11 +269,11 @@ bool wxGetUserId(wxChar *buf, int maxSize)
 bool wxGetUserName(wxChar *buf, int maxSize)
 {
 #if defined(__WXWINCE__)
-    return FALSE;
+    return false;
 #elif defined(USE_NET_API)
     CHAR szUserName[256];
     if ( !wxGetUserId(szUserName, WXSIZEOF(szUserName)) )
-        return FALSE;
+        return false;
 
     // TODO how to get the domain name?
     CHAR *szDomain = "";
@@ -332,12 +330,12 @@ bool wxGetUserName(wxChar *buf, int maxSize)
     WideCharToMultiByte( CP_ACP, 0, ui2->usri2_full_name, -1,
             buf, maxSize, NULL, NULL );
 
-    return TRUE;
+    return true;
 
 error:
     wxLogError(wxT("Couldn't look up full user name."));
 
-    return FALSE;
+    return false;
 #else  // !USE_NET_API
     // Could use NIS, MS-Mail or other site specific programs
     // Use wxWidgets configuration data
@@ -353,7 +351,7 @@ error:
     }
 #endif // Win32/16
 
-    return TRUE;
+    return true;
 }
 
 const wxChar* wxGetHomeDir(wxString *pstr)
@@ -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
 
@@ -469,10 +463,10 @@ bool wxDirExists(const wxString& dir)
 bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
 {
 #ifdef __WXWINCE__
-    return FALSE;
+    return false;
 #else
     if ( path.empty() )
-        return FALSE;
+        return false;
 
 // old w32api don't have ULARGE_INTEGER
 #if defined(__WIN32__) && \
@@ -507,7 +501,7 @@ bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
         {
             wxLogLastError(_T("GetDiskFreeSpaceEx"));
 
-            return FALSE;
+            return false;
         }
 
         // ULARGE_INTEGER is a union of a 64 bit value and a struct containing
@@ -550,7 +544,7 @@ bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
         {
             wxLogLastError(_T("GetDiskFreeSpace"));
 
-            return FALSE;
+            return false;
         }
 
         wxLongLong lBytesPerCluster = lSectorsPerCluster;
@@ -569,7 +563,7 @@ bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
         }
     }
 
-    return TRUE;
+    return true;
 #endif
     // __WXWINCE__
 }
@@ -581,14 +575,14 @@ bool wxGetDiskSpace(const wxString& path, wxLongLong *pTotal, wxLongLong *pFree)
 bool wxGetEnv(const wxString& var, wxString *value)
 {
 #ifdef __WXWINCE__
-    return FALSE;
+    return false;
 #else // Win32
     // first get the size of the buffer
     DWORD dwRet = ::GetEnvironmentVariable(var, NULL, 0);
     if ( !dwRet )
     {
         // this means that there is no such variable
-        return FALSE;
+        return false;
     }
 
     if ( value )
@@ -597,7 +591,7 @@ bool wxGetEnv(const wxString& var, wxString *value)
                                        dwRet);
     }
 
-    return TRUE;
+    return true;
 #endif // WinCE/32
 }
 
@@ -610,12 +604,12 @@ bool wxSetEnv(const wxString& var, const wxChar *value)
     {
         wxLogLastError(_T("SetEnvironmentVariable"));
 
-        return FALSE;
+        return false;
     }
 
-    return TRUE;
+    return true;
 #else // no way to set env vars
-    return FALSE;
+    return false;
 #endif
 }
 
@@ -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 |
@@ -683,7 +682,7 @@ int wxKill(long pid, wxSignal sig, wxKillError *krc)
         return -1;
     }
 
-    bool ok = TRUE;
+    bool ok = true;
     switch ( sig )
     {
         case wxSIGKILL:
@@ -699,7 +698,7 @@ int wxKill(long pid, wxSignal sig, wxKillError *krc)
                     *krc = wxKILL_ERROR;
                 }
 
-                ok = FALSE;
+                ok = false;
             }
             break;
 
@@ -736,7 +735,7 @@ int wxKill(long pid, wxSignal sig, wxKillError *krc)
                     {
                         wxLogLastError(_T("EnumWindows"));
 
-                        ok = FALSE;
+                        ok = false;
                     }
                 }
                 else // no windows for this PID
@@ -746,7 +745,7 @@ int wxKill(long pid, wxSignal sig, wxKillError *krc)
                         *krc = wxKILL_ERROR;
                     }
 
-                    ok = FALSE;
+                    ok = false;
                 }
             }
     }
@@ -814,17 +813,123 @@ 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)
 {
+    wxString cmd;
+
 #ifdef __WXWINCE__
-    return FALSE;
+    cmd = command;
 #else
     wxChar *shell = wxGetenv(wxT("COMSPEC"));
     if ( !shell )
         shell = (wxChar*) wxT("\\COMMAND.COM");
 
-    wxString cmd;
     if ( !command )
     {
         // just the shell
@@ -835,18 +940,18 @@ bool wxShell(const wxString& command)
         // pass the command to execute to the command processor
         cmd.Printf(wxT("%s /c %s"), shell, command.c_str());
     }
+#endif
 
     return wxExecute(cmd, wxEXEC_SYNC) == 0;
-#endif
 }
 
 // Shutdown or reboot the PC
 bool wxShutdown(wxShutdownFlags wFlags)
 {
 #ifdef __WXWINCE__
-    return FALSE;
+    return false;
 #elif defined(__WIN32__)
-    bool bOK = TRUE;
+    bool bOK = true;
 
     if ( wxGetOsVersion(NULL, NULL) == wxWINDOWS_NT ) // if is NT or 2K
     {
@@ -890,7 +995,7 @@ bool wxShutdown(wxShutdownFlags wFlags)
 
             default:
                 wxFAIL_MSG( _T("unknown wxShutdown() flag") );
-                return FALSE;
+                return false;
         }
 
         bOK = ::ExitWindowsEx(flags, 0) != 0;
@@ -945,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(')');
@@ -954,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;
@@ -1147,7 +1304,7 @@ extern long wxCharsetToCodepage(const wxChar *name)
     if ( !name )
         return -1;
 
-    wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(name, FALSE);
+    wxFontEncoding enc = wxFontMapper::Get()->CharsetToEncoding(name, false);
     if ( enc == wxFONTENCODING_SYSTEM )
         return -1;