Merge branch 'master' into msbuilddll2
[mono.git] / mono / metadata / process.c
old mode 100644 (file)
new mode 100755 (executable)
index 89bbbf6..4b1c43b
@@ -4,8 +4,8 @@
  * Author:
  *     Dick Porter (dick@ximian.com)
  *
- * (C) 2002 Ximian, Inc.
- * Copyright (c) 2002-2006 Novell, Inc.
+ * Copyright 2002 Ximian, Inc.
+ * Copyright 2002-2006 Novell, Inc.
  */
 
 #include <config.h>
@@ -13,7 +13,7 @@
 #include <glib.h>
 #include <string.h>
 
-#include <mono/metadata/object.h>
+#include <mono/metadata/object-internals.h>
 #include <mono/metadata/process.h>
 #include <mono/metadata/assembly.h>
 #include <mono/metadata/appdomain.h>
 #include <mono/metadata/cil-coff.h>
 #include <mono/metadata/exception.h>
 #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>
 
-#undef DEBUG
+#define LOGDEBUG(...)  
+/* define LOGDEBUG(...) g_message(__VA_ARGS__)  */
+
 
 HANDLE ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid)
 {
@@ -59,11 +76,14 @@ void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject *thi
        MONO_ARCH_SAVE_REGS;
 
 #ifdef THREAD_DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Closing process %p, handle %p",
-                  this, process);
+       g_message ("%s: Closing process %p, handle %p", __func__, this, process);
 #endif
 
+#if defined(TARGET_WIN32) || defined(HOST_WIN32)
        CloseHandle (process);
+#else
+       CloseProcess (process);
+#endif
 }
 
 #define STASH_SYS_ASS(this) \
@@ -90,15 +110,11 @@ static void process_set_field_object (MonoObject *obj, const gchar *fieldname,
 {
        MonoClassField *field;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to object at %p",
-                  fieldname, data);
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to object at %p", __func__, fieldname, data));
 
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
-       /* FIXME: moving GC */
-       *(MonoObject **)(((char *)obj) + field->offset)=data;
+       mono_gc_wbarrier_generic_store (((char *)obj) + field->offset, data);
 }
 
 static void process_set_field_string (MonoObject *obj, const gchar *fieldname,
@@ -107,17 +123,13 @@ static void process_set_field_string (MonoObject *obj, const gchar *fieldname,
        MonoClassField *field;
        MonoString *string;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to [%s]",
-                  fieldname, g_utf16_to_utf8 (val, len, NULL, NULL, NULL));
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to [%s]", __func__, fieldname, g_utf16_to_utf8 (val, len, NULL, NULL, NULL)));
 
        string=mono_string_new_utf16 (mono_object_domain (obj), val, len);
        
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
-       /* FIXME: moving GC */
-       *(MonoString **)(((char *)obj) + field->offset)=string;
+       mono_gc_wbarrier_generic_store (((char *)obj) + field->offset, (MonoObject*)string);
 }
 
 static void process_set_field_int (MonoObject *obj, const gchar *fieldname,
@@ -125,10 +137,7 @@ static void process_set_field_int (MonoObject *obj, const gchar *fieldname,
 {
        MonoClassField *field;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to %d",
-                  fieldname, val);
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to %d", __func__,fieldname, val));
        
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
@@ -140,9 +149,7 @@ static void process_set_field_intptr (MonoObject *obj, const gchar *fieldname,
 {
        MonoClassField *field;
 
-#ifdef DEBUG
-       g_message ("%s: Setting field %s to %p", __func__, fieldname, val);
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to %p", __func__, fieldname, val));
        
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
@@ -154,10 +161,7 @@ static void process_set_field_bool (MonoObject *obj, const gchar *fieldname,
 {
        MonoClassField *field;
 
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": Setting field %s to %s",
-                  fieldname, val?"TRUE":"FALSE");
-#endif
+       LOGDEBUG (g_message ("%s: Setting field %s to %s", __func__, fieldname, val?"TRUE":"FALSE"));
        
        field=mono_class_get_field_from_name (mono_object_class (obj),
                                              fieldname);
@@ -176,6 +180,7 @@ static void process_set_field_bool (MonoObject *obj, const gchar *fieldname,
 #define SFI_PRODUCTNAME                "\\StringFileInfo\\%02X%02X%02X%02X\\ProductName"
 #define SFI_PRODUCTVERSION     "\\StringFileInfo\\%02X%02X%02X%02X\\ProductVersion"
 #define SFI_SPECIALBUILD       "\\StringFileInfo\\%02X%02X%02X%02X\\SpecialBuild"
+#define EMPTY_STRING           (gunichar2*)"\000\000"
 
 static void process_module_string_read (MonoObject *filever, gpointer data,
                                        const gchar *fieldname,
@@ -184,22 +189,20 @@ static void process_module_string_read (MonoObject *filever, gpointer data,
 {
        gchar *lang_key_utf8;
        gunichar2 *lang_key, *buffer;
-       UINT bytes;
+       UINT chars;
 
        lang_key_utf8 = g_strdup_printf (key, lang_lo, lang_hi, 0x04, 0xb0);
 
-#ifdef DEBUG
-       g_message ("%s: asking for [%s]", __func__, lang_key_utf8);
-#endif
+       LOGDEBUG (g_message ("%s: asking for [%s]", __func__, lang_key_utf8));
 
        lang_key = g_utf8_to_utf16 (lang_key_utf8, -1, NULL, NULL, NULL);
 
-       if (VerQueryValue (data, lang_key, (gpointer *)&buffer, &bytes)) {
-#ifdef DEBUG
-               g_message ("%s: found %d bytes of [%s]", __func__, bytes,
-                          g_utf16_to_utf8 (buffer, bytes, NULL, NULL, NULL));
-#endif
-               process_set_field_string (filever, fieldname, buffer, bytes);
+       if (VerQueryValue (data, lang_key, (gpointer *)&buffer, &chars) && chars > 0) {
+               LOGDEBUG (g_message ("%s: found %d chars of [%s]", __func__, chars, g_utf16_to_utf8 (buffer, chars, NULL, NULL, NULL)));
+               /* chars includes trailing null */
+               process_set_field_string (filever, fieldname, buffer, chars - 1);
+       } else {
+               process_set_field_string (filever, fieldname, EMPTY_STRING, 0);
        }
 
        g_free (lang_key);
@@ -245,7 +248,8 @@ static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
        gunichar2 *query;
        UINT ffi_size, trans_size;
        BOOL ok;
-       int i;
+       gunichar2 lang_buf[128];
+       guint32 lang, lang_count;
 
        datalen = GetFileVersionInfoSize (filename, &verinfohandle);
        if (datalen) {
@@ -261,9 +265,7 @@ static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
                        
                        if (VerQueryValue (data, query, (gpointer *)&ffi,
                            &ffi_size)) {
-#ifdef DEBUG
-                               g_message (G_GNUC_PRETTY_FUNCTION ": recording assembly: FileName [%s] FileVersionInfo [%d.%d.%d.%d]", g_utf16_to_utf8 (filename, -1, NULL, NULL, NULL), HIWORD (ffi->dwFileVersionMS), LOWORD (ffi->dwFileVersionMS), HIWORD (ffi->dwFileVersionLS), LOWORD (ffi->dwFileVersionLS));
-#endif
+                               LOGDEBUG (g_message ("%s: recording assembly: FileName [%s] FileVersionInfo [%d.%d.%d.%d]", __func__, g_utf16_to_utf8 (filename, -1, NULL, NULL, NULL), HIWORD (ffi->dwFileVersionMS), LOWORD (ffi->dwFileVersionMS), HIWORD (ffi->dwFileVersionLS), LOWORD (ffi->dwFileVersionLS)));
        
                                process_set_field_int (filever, "filemajorpart", HIWORD (ffi->dwFileVersionMS));
                                process_set_field_int (filever, "fileminorpart", LOWORD (ffi->dwFileVersionMS));
@@ -292,50 +294,82 @@ static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
                        if (VerQueryValue (data, query,
                                           (gpointer *)&trans_data,
                                           &trans_size)) {
-                               /* Look for neutral or en_US language data
-                                * (or should we use the first language ID we
-                                * see?)
+                               /* use the first language ID we see
                                 */
-                               for (i = 0; i < trans_size; i += 4) {
-#ifdef DEBUG
-                                       g_message("%s: %s has 0x%0x 0x%0x 0x%0x 0x%0x", __func__, g_utf16_to_utf8 (filename, -1, NULL, NULL, NULL), trans_data[i], trans_data[i+1], trans_data[i+2], trans_data[i+3]);
-#endif
-
-                                       if ((trans_data[i] == 0x09 &&
-                                            trans_data[i+1] == 0x04 &&
-                                            trans_data[i+2] == 0xb0 &&
-                                            trans_data[i+3] == 0x04) ||
-                                           (trans_data[i] == 0x00 &&
-                                            trans_data[i+1] == 0x00 &&
-                                            trans_data[i+2] == 0xb0 &&
-                                            trans_data[i+3] == 0x04) ||
-                                           (trans_data[i] == 0x7f &&
-                                            trans_data[i+1] == 0x00 &&
-                                            trans_data[i+2] == 0xb0 &&
-                                            trans_data[i+3] == 0x04)) {
-                                               gunichar2 lang_buf[128];
-                                               guint32 lang, lang_count;
-
-                                               lang = (trans_data[i]) |
-                                                      (trans_data[i+1] << 8) |
-                                                      (trans_data[i+2] << 16) |
-                                                      (trans_data[i+3] << 24);
-                                               lang_count = VerLanguageName (lang, lang_buf, 128);
-                                               if (lang_count) {
-                                                       process_set_field_string (filever, "language", lang_buf, lang_count);
-                                               }
-                                               process_module_stringtable (filever, data, trans_data[i], trans_data[i+1]);
+                               if (trans_size >= 4) {
+                                       LOGDEBUG (g_message("%s: %s has 0x%0x 0x%0x 0x%0x 0x%0x", __func__, g_utf16_to_utf8 (filename, -1, NULL, NULL, NULL), trans_data[0], trans_data[1], trans_data[2], trans_data[3]));
+                                       lang = (trans_data[0]) |
+                                               (trans_data[1] << 8) |
+                                               (trans_data[2] << 16) |
+                                               (trans_data[3] << 24);
+                                       /* Only give the lower 16 bits
+                                        * to VerLanguageName, as
+                                        * Windows gets confused
+                                        * otherwise
+                                        */
+                                       lang_count = VerLanguageName (lang & 0xFFFF, lang_buf, 128);
+                                       if (lang_count) {
+                                               process_set_field_string (filever, "language", lang_buf, lang_count);
                                        }
+                                       process_module_stringtable (filever, data, trans_data[0], trans_data[1]);
+                               }
+                       } else {
+                               /* No strings, so set every field to
+                                * the empty string
+                                */
+                               process_set_field_string (filever,
+                                                         "comments",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "companyname",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "filedescription",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "fileversion",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "internalname",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "legalcopyright",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "legaltrademarks",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "originalfilename",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "privatebuild",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "productname",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "productversion",
+                                                         EMPTY_STRING, 0);
+                               process_set_field_string (filever,
+                                                         "specialbuild",
+                                                         EMPTY_STRING, 0);
+
+                               /* And language seems to be set to
+                                * en_US according to bug 374600
+                                */
+                               lang_count = VerLanguageName (0x0409, lang_buf, 128);
+                               if (lang_count) {
+                                       process_set_field_string (filever, "language", lang_buf, lang_count);
                                }
                        }
+                       
                        g_free (query);
                }
                g_free (data);
        }
 }
 
-static void process_add_module (GPtrArray *modules, HANDLE process, HMODULE mod,
-                               gunichar2 *filename, gunichar2 *modulename)
+static MonoObject* process_add_module (HANDLE process, HMODULE mod, gunichar2 *filename, gunichar2 *modulename)
 {
        MonoClass *proc_class, *filever_class;
        MonoObject *item, *filever;
@@ -374,50 +408,47 @@ static void process_add_module (GPtrArray *modules, HANDLE process, HMODULE mod,
                                  unicode_chars (modulename));
        process_set_field_object (item, "version_info", filever);
 
-       /* FIXME: moving GC */
-       g_ptr_array_add (modules, item);
+       return item;
 }
 
 /* Returns an array of System.Diagnostics.ProcessModule */
 MonoArray *ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject *this, HANDLE process)
 {
-       GPtrArray *modules_list=g_ptr_array_new ();
+       MonoArray *temp_arr = NULL;
        MonoArray *arr;
        HMODULE mods[1024];
        gunichar2 filename[MAX_PATH];
        gunichar2 modname[MAX_PATH];
        DWORD needed;
-       guint32 count;
-       guint32 i;
-       
-       MONO_ARCH_SAVE_REGS;
+       guint32 count = 0;
+       guint32 i, num_added = 0;
 
        STASH_SYS_ASS (this);
 
        if (EnumProcessModules (process, mods, sizeof(mods), &needed)) {
                count = needed / sizeof(HMODULE);
+               temp_arr = mono_array_new (mono_domain_get (), mono_get_object_class (), count);
                for (i = 0; i < count; i++) {
-                       if (GetModuleBaseName (process, mods[i], modname,
-                                              MAX_PATH) &&
-                           GetModuleFileNameEx (process, mods[i], filename,
-                                                MAX_PATH)) {
-                               process_add_module (modules_list, process,
-                                                   mods[i], filename, modname);
+                       if (GetModuleBaseName (process, mods[i], modname, MAX_PATH) &&
+                                       GetModuleFileNameEx (process, mods[i], filename, MAX_PATH)) {
+                               MonoObject *module = process_add_module (process, mods[i],
+                                               filename, modname);
+                               mono_array_setref (temp_arr, num_added++, module);
                        }
                }
        }
 
-       /* Build a MonoArray out of modules_list */
-       arr=mono_array_new (mono_domain_get (), mono_get_object_class (),
-                           modules_list->len);
-       
-       for(i=0; i<modules_list->len; i++) {
-               mono_array_setref (arr, i, g_ptr_array_index (modules_list, i));
+       if (count == num_added) {
+               arr = temp_arr;
+       } else {
+               /* shorter version of the array */
+               arr = mono_array_new (mono_domain_get (), mono_get_object_class (), num_added);
+
+               for (i = 0; i < num_added; i++)
+                       mono_array_setref (arr, i, mono_array_get (temp_arr, MonoObject*, i));
        }
-       
-       g_ptr_array_free (modules_list, TRUE);
-       
-       return(arr);
+
+       return arr;
 }
 
 void ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal (MonoObject *this, MonoString *filename)
@@ -437,7 +468,7 @@ static gchar *
 quote_path (const gchar *path)
 {
        gchar *res = g_shell_quote (path);
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        {
        gchar *q = res;
        while (*q) {
@@ -454,38 +485,52 @@ quote_path (const gchar *path)
 static gboolean
 complete_path (const gunichar2 *appname, gchar **completed)
 {
-       gchar *utf8app;
+       gchar *utf8app, *utf8appmemory;
        gchar *found;
 
-       utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
+       utf8appmemory = utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
+#ifdef TARGET_WIN32 // Should this happen on all platforms? 
+       {
+               // remove the quotes around utf8app.
+               size_t len;
+               len = strlen (utf8app);
+               if (len) {
+                       if (utf8app[len-1] == '\"')
+                               utf8app[len-1] = '\0';
+                       if (utf8app[0] == '\"')
+                               utf8app++;
+               }
+       }
+#endif
+
        if (g_path_is_absolute (utf8app)) {
                *completed = quote_path (utf8app);
-               g_free (utf8app);
+               g_free (utf8appmemory);
                return TRUE;
        }
 
        if (g_file_test (utf8app, G_FILE_TEST_IS_EXECUTABLE) && !g_file_test (utf8app, G_FILE_TEST_IS_DIR)) {
                *completed = quote_path (utf8app);
-               g_free (utf8app);
+               g_free (utf8appmemory);
                return TRUE;
        }
        
        found = g_find_program_in_path (utf8app);
        if (found == NULL) {
                *completed = NULL;
-               g_free (utf8app);
+               g_free (utf8appmemory);
                return FALSE;
        }
 
        *completed = quote_path (found);
        g_free (found);
-       g_free (utf8app);
+       g_free (utf8appmemory);
        return TRUE;
 }
 
 #ifndef HAVE_GETPROCESSID
 /* Run-time GetProcessId detection for Windows */
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
 #define HAVE_GETPROCESSID
 
 typedef DWORD (WINAPI *GETPROCESSID_PROC) (HANDLE);
@@ -550,7 +595,7 @@ static DWORD WINAPI GetProcessId_detect (HANDLE process)
        GetProcessId = &GetProcessId_stub;
        return GetProcessId (process);
 }
-#endif /* PLATFORM_WIN32 */
+#endif /* HOST_WIN32 */
 #endif /* !HAVE_GETPROCESSID */
 
 MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoProcessStartInfo *proc_start_info, MonoProcInfo *process_info)
@@ -559,8 +604,10 @@ 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.nShow = SW_SHOWNORMAL;
+       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);
+       
        
        if (proc_start_info->filename != NULL) {
                shellex.lpFile = mono_string_chars (proc_start_info->filename);
@@ -580,23 +627,13 @@ MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoPr
                shellex.lpDirectory = mono_string_chars (proc_start_info->working_directory);
        }
 
-       ret = ShellExecuteEx (&shellex);
-       if (ret == FALSE) {
-               process_info->pid = -GetLastError ();
+       if (proc_start_info->error_dialog) {    
+               shellex.hwnd = proc_start_info->error_dialog_parent_handle;
        } 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.
-                */
-#ifdef HAVE_GETPROCESSID
-               process_info->pid = GetProcessId (shellex.hProcess);
-#else
-               process_info->pid = 0;
-#endif
-               process_info->tid = 0;
+               shellex.fMask |= SEE_MASK_FLAG_NO_UI;
        }
 
+       ret = ShellExecuteEx (&shellex);
        return (ret);
 }
 
@@ -629,7 +666,7 @@ MonoBoolean ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoPro
                process_info->pid = -ERROR_FILE_NOT_FOUND;
                return FALSE;
        }
-#ifdef PLATFORM_WIN32
+#ifdef TARGET_WIN32
        /* Seems like our CreateProcess does not work as the windows one.
         * This hack is needed to deal with paths containing spaces */
        shell_path = NULL;
@@ -694,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);
@@ -745,6 +782,22 @@ MonoBoolean ves_icall_System_Diagnostics_Process_WaitForExit_internal (MonoObjec
        }
 }
 
+MonoBoolean ves_icall_System_Diagnostics_Process_WaitForInputIdle_internal (MonoObject *this, HANDLE process, gint32 ms)
+{
+       guint32 ret;
+       
+       MONO_ARCH_SAVE_REGS;
+
+       if(ms<0) {
+               /* Wait forever */
+               ret=WaitForInputIdle (process, INFINITE);
+       } else {
+               ret=WaitForInputIdle (process, ms);
+       }
+
+       return (ret) ? FALSE : TRUE;
+}
+
 gint64 ves_icall_System_Diagnostics_Process_ExitTime_internal (HANDLE process)
 {
        gboolean ret;
@@ -793,9 +846,7 @@ gint32 ves_icall_System_Diagnostics_Process_ExitCode_internal (HANDLE process)
 
        GetExitCodeProcess (process, &code);
        
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": process exit code is %d", code);
-#endif
+       LOGDEBUG (g_message ("%s: process exit code is %d", __func__, code));
        
        return(code);
 }
@@ -821,10 +872,7 @@ MonoString *ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE pr
                return(NULL);
        }
        
-#ifdef DEBUG
-       g_message (G_GNUC_PRETTY_FUNCTION ": process name is [%s]",
-                  g_utf16_to_utf8 (name, -1, NULL, NULL, NULL));
-#endif
+       LOGDEBUG (g_message ("%s: process name is [%s]", __func__, g_utf16_to_utf8 (name, -1, NULL, NULL, NULL)));
        
        string=mono_string_new_utf16 (mono_domain_get (), name, len);
        
@@ -837,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)
@@ -937,3 +998,41 @@ ves_icall_System_Diagnostics_Process_SetPriorityClass (HANDLE process, gint32 pr
        *error = ret == 0 ? GetLastError () : 0;
        return ret;
 }
+
+HANDLE
+ves_icall_System_Diagnostics_Process_ProcessHandle_duplicate (HANDLE process)
+{
+       HANDLE ret;
+
+       MONO_ARCH_SAVE_REGS;
+
+       LOGDEBUG (g_message ("%s: Duplicating process handle %p", __func__, process));
+       
+       DuplicateHandle (GetCurrentProcess (), process, GetCurrentProcess (),
+                        &ret, THREAD_ALL_ACCESS, TRUE, 0);
+       
+       return ret;
+}
+
+void
+ves_icall_System_Diagnostics_Process_ProcessHandle_close (HANDLE process)
+{
+       MONO_ARCH_SAVE_REGS;
+
+       LOGDEBUG (g_message ("%s: Closing process handle %p", __func__, process));
+
+       CloseHandle (process);
+}
+
+gint64
+ves_icall_System_Diagnostics_Process_GetProcessData (int pid, gint32 data_type, gint32 *error)
+{
+       MonoProcessError perror;
+       guint64 res;
+
+       res = mono_process_get_data_with_error (GINT_TO_POINTER (pid), data_type, &perror);
+       if (error)
+               *error = perror;
+       return res;
+}
+