Merge branch 'master' into msbuilddll2
[mono.git] / mono / metadata / process.c
old mode 100644 (file)
new mode 100755 (executable)
index 01a29bc..4b1c43b
 #include <mono/utils/strenc.h>
 #include <mono/utils/mono-proclib.h>
 #include <mono/io-layer/io-layer.h>
+#if defined (MINGW_CROSS_COMPILE) && defined (HAVE_GETPROCESSID)
+#undef HAVE_GETPROCESSID
+#endif
+#ifndef HAVE_GETPROCESSID
+#if defined(_MSC_VER) || defined(HAVE_WINTERNL_H)
+#include <winternl.h>
+#ifndef NT_SUCCESS
+#define NT_SUCCESS(status) ((NTSTATUS) (status) >= 0)
+#endif /* !NT_SUCCESS */
+#else /* ! (defined(_MSC_VER) || defined(HAVE_WINTERNL_H)) */
+#include <ddk/ntddk.h>
+#include <ddk/ntapi.h>
+#endif /* (defined(_MSC_VER) || defined(HAVE_WINTERNL_H)) */
+#endif /* !HAVE_GETPROCESSID */
 /* FIXME: fix this code to not depend so much on the inetrnals */
 #include <mono/metadata/class-internals.h>
 
@@ -65,7 +79,7 @@ void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject *thi
        g_message ("%s: Closing process %p, handle %p", __func__, this, process);
 #endif
 
-#if TARGET_WIN32
+#if defined(TARGET_WIN32) || defined(HOST_WIN32)
        CloseHandle (process);
 #else
        CloseProcess (process);
@@ -514,10 +528,6 @@ complete_path (const gunichar2 *appname, gchar **completed)
        return TRUE;
 }
 
-#if defined (MINGW_CROSS_COMPILE) && defined (HAVE_GETPROCESSID)
-#undef HAVE_GETPROCESSID
-#endif
-
 #ifndef HAVE_GETPROCESSID
 /* Run-time GetProcessId detection for Windows */
 #ifdef TARGET_WIN32
@@ -594,7 +604,7 @@ MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoPr
        gboolean ret;
 
        shellex.cbSize = sizeof(SHELLEXECUTEINFO);
-       shellex.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE;
+       shellex.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_UNICODE;
        shellex.nShow = proc_start_info->window_style;
        shellex.nShow = (shellex.nShow == 0) ? 1 : (shellex.nShow == 1 ? 0 : shellex.nShow);
        
@@ -624,22 +634,6 @@ MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoPr
        }
 
        ret = ShellExecuteEx (&shellex);
-       if (ret == FALSE) {
-               process_info->pid = -GetLastError ();
-       } else {
-               process_info->process_handle = shellex.hProcess;
-               process_info->thread_handle = NULL;
-               /* It appears that there's no way to get the pid from a
-                * process handle before windows xp.  Really.
-                */
-#if defined(HAVE_GETPROCESSID) && !defined(MONO_CROSS_COMPILE)
-               process_info->pid = GetProcessId (shellex.hProcess);
-#else
-               process_info->pid = 0;
-#endif
-               process_info->tid = 0;
-       }
-
        return (ret);
 }
 
@@ -737,7 +731,7 @@ MonoBoolean ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoPro
        /* The default dir name is "".  Turn that into NULL to mean
         * "current directory"
         */
-       if(mono_string_length (proc_start_info->working_directory)==0) {
+       if(proc_start_info->working_directory == NULL || mono_string_length (proc_start_info->working_directory)==0) {
                dir=NULL;
        } else {
                dir=mono_string_chars (proc_start_info->working_directory);
@@ -891,25 +885,38 @@ MonoArray *ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
        MonoArray *procs;
        gboolean ret;
        DWORD needed;
-       guint32 count, i;
-       DWORD pids[1024];
+       guint32 count;
+       guint32 *pids;
 
        MONO_ARCH_SAVE_REGS;
 
-       ret=EnumProcesses (pids, sizeof(pids), &needed);
-       if(ret==FALSE) {
-               /* FIXME: throw an exception */
-               return(NULL);
-       }
-       
-       count=needed/sizeof(DWORD);
-       procs=mono_array_new (mono_domain_get (), mono_get_int32_class (),
-                             count);
-       for(i=0; i<count; i++) {
-               mono_array_set (procs, guint32, i, pids[i]);
-       }
+       count = 512;
+       do {
+               pids = g_new0 (guint32, count);
+               ret = EnumProcesses (pids, count * sizeof (guint32), &needed);
+               if (ret == FALSE) {
+                       MonoException *exc;
+
+                       g_free (pids);
+                       pids = NULL;
+                       exc = mono_get_exception_not_supported ("This system does not support EnumProcesses");
+                       mono_raise_exception (exc);
+                       g_assert_not_reached ();
+               }
+               if (needed < (count * sizeof (guint32)))
+                       break;
+               g_free (pids);
+               pids = NULL;
+               count = (count * 3) / 2;
+       } while (TRUE);
+
+       count = needed / sizeof (guint32);
+       procs = mono_array_new (mono_domain_get (), mono_get_int32_class (), count);
+       memcpy (mono_array_addr (procs, guint32, 0), pids, needed);
+       g_free (pids);
+       pids = NULL;
        
-       return(procs);
+       return procs;
 }
 
 MonoBoolean ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process, guint32 *min, guint32 *max)