[runtime] Introduce MonoError into mono_metadata_parse_method_signature_full.
[mono.git] / mono / metadata / process.c
index 85b475db7c3c6ed2393819b87f2089e6a019c030..d7cc110aec6bbca2ac91b363582f40410cf1fba0 100644 (file)
@@ -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>
-/* FIXME: fix this code to not depend so much on the inetrnals */
+/* FIXME: fix this code to not depend so much on the internals */
 #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)
 {
@@ -46,11 +49,12 @@ HANDLE ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid)
        return(handle);
 }
 
-guint32 ves_icall_System_Diagnostics_Process_GetPid_internal (void)
+guint32
+ves_icall_System_Diagnostics_Process_GetPid_internal (void)
 {
        MONO_ARCH_SAVE_REGS;
 
-       return(GetCurrentProcessId ());
+       return mono_process_current_pid ();
 }
 
 void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject *this,
@@ -59,11 +63,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 +97,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 +110,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 +124,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 +136,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 +148,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);
@@ -189,17 +180,12 @@ static void process_module_string_read (MonoObject *filever, gpointer data,
 
        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, &chars) && chars > 0) {
-#ifdef DEBUG
-               g_message ("%s: found %d chars of [%s]", __func__, chars,
-                          g_utf16_to_utf8 (buffer, chars, NULL, NULL, NULL));
-#endif
+               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 {
@@ -266,9 +252,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));
@@ -300,9 +284,7 @@ static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
                                /* use the first language ID we see
                                 */
                                if (trans_size >= 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[0], trans_data[1], trans_data[2], trans_data[3]);
-#endif
+                                       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) |
@@ -374,8 +356,7 @@ static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
        }
 }
 
-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;
@@ -414,50 +395,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)
@@ -477,7 +455,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) {
@@ -498,7 +476,7 @@ complete_path (const gunichar2 *appname, gchar **completed)
        gchar *found;
 
        utf8appmemory = utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
-#ifdef PLATFORM_WIN32 // Should this happen on all platforms? 
+#ifdef TARGET_WIN32 // Should this happen on all platforms? 
        {
                // remove the quotes around utf8app.
                size_t len;
@@ -537,76 +515,6 @@ complete_path (const gunichar2 *appname, gchar **completed)
        return TRUE;
 }
 
-#ifndef HAVE_GETPROCESSID
-/* Run-time GetProcessId detection for Windows */
-#ifdef PLATFORM_WIN32
-#define HAVE_GETPROCESSID
-
-typedef DWORD (WINAPI *GETPROCESSID_PROC) (HANDLE);
-typedef DWORD (WINAPI *NTQUERYINFORMATIONPROCESS_PROC) (HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
-typedef DWORD (WINAPI *RTLNTSTATUSTODOSERROR_PROC) (NTSTATUS);
-
-static DWORD WINAPI GetProcessId_detect (HANDLE process);
-
-static GETPROCESSID_PROC GetProcessId = &GetProcessId_detect;
-static NTQUERYINFORMATIONPROCESS_PROC NtQueryInformationProcess_proc = NULL;
-static RTLNTSTATUSTODOSERROR_PROC RtlNtStatusToDosError_proc = NULL;
-
-static DWORD WINAPI GetProcessId_ntdll (HANDLE process)
-{
-       PROCESS_BASIC_INFORMATION pi;
-       NTSTATUS status;
-
-       status = NtQueryInformationProcess_proc (process, ProcessBasicInformation, &pi, sizeof (pi), NULL);
-       if (NT_SUCCESS (status)) {
-               return pi.UniqueProcessId;
-       } else {
-               SetLastError (RtlNtStatusToDosError_proc (status));
-               return 0;
-       }
-}
-
-static DWORD WINAPI GetProcessId_stub (HANDLE process)
-{
-       SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
-       return 0;
-}
-
-static DWORD WINAPI GetProcessId_detect (HANDLE process)
-{
-       HMODULE module_handle;
-       GETPROCESSID_PROC GetProcessId_kernel;
-
-       /* Windows XP SP1 and above have GetProcessId API */
-       module_handle = GetModuleHandle (L"kernel32.dll");
-       if (module_handle != NULL) {
-               GetProcessId_kernel = (GETPROCESSID_PROC) GetProcAddress (module_handle, "GetProcessId");
-               if (GetProcessId_kernel != NULL) {
-                       GetProcessId = GetProcessId_kernel;
-                       return GetProcessId (process);
-               }
-       }
-
-       /* Windows 2000 and above have deprecated NtQueryInformationProcess API */
-       module_handle = GetModuleHandle (L"ntdll.dll");
-       if (module_handle != NULL) {
-               NtQueryInformationProcess_proc = (NTQUERYINFORMATIONPROCESS_PROC) GetProcAddress (module_handle, "NtQueryInformationProcess");
-               if (NtQueryInformationProcess_proc != NULL) {
-                       RtlNtStatusToDosError_proc = (RTLNTSTATUSTODOSERROR_PROC) GetProcAddress (module_handle, "RtlNtStatusToDosError");
-                       if (RtlNtStatusToDosError_proc != NULL) {
-                               GetProcessId = &GetProcessId_ntdll;
-                               return GetProcessId (process);
-                       }
-               }
-       }
-
-       /* Fall back to ERROR_CALL_NOT_IMPLEMENTED */
-       GetProcessId = &GetProcessId_stub;
-       return GetProcessId (process);
-}
-#endif /* PLATFORM_WIN32 */
-#endif /* !HAVE_GETPROCESSID */
-
 MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoProcessStartInfo *proc_start_info, MonoProcInfo *process_info)
 {
        SHELLEXECUTEINFO shellex = {0};
@@ -614,8 +522,8 @@ MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoPr
 
        shellex.cbSize = sizeof(SHELLEXECUTEINFO);
        shellex.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE;
-       shellex.nShow = SW_SHOWNORMAL;
-
+       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) {
@@ -651,7 +559,7 @@ MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoPr
                /* It appears that there's no way to get the pid from a
                 * process handle before windows xp.  Really.
                 */
-#ifdef HAVE_GETPROCESSID
+#if defined(HAVE_GETPROCESSID) && !defined(MONO_CROSS_COMPILE)
                process_info->pid = GetProcessId (shellex.hProcess);
 #else
                process_info->pid = 0;
@@ -691,7 +599,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;
@@ -756,7 +664,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);
@@ -807,44 +715,56 @@ 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;
+}
+
+static guint64
+file_time_to_guint64 (FILETIME *time)
+{
+       return ((guint64)time->dwHighDateTime << 32) | ((guint64)time->dwLowDateTime);
+}
+
 gint64 ves_icall_System_Diagnostics_Process_ExitTime_internal (HANDLE process)
 {
        gboolean ret;
-       gint64 ticks;
        FILETIME create_time, exit_time, kernel_time, user_time;
        
        MONO_ARCH_SAVE_REGS;
 
-       ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
-                            &user_time);
-       if(ret==TRUE) {
-               ticks=((guint64)exit_time.dwHighDateTime << 32) +
-                       exit_time.dwLowDateTime;
-               
-               return(ticks);
-       } else {
-               return(0);
-       }
+       ret = GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
+                                                  &user_time);
+       if (ret)
+               return file_time_to_guint64 (&exit_time);
+       else
+               return 0;
 }
 
 gint64 ves_icall_System_Diagnostics_Process_StartTime_internal (HANDLE process)
 {
        gboolean ret;
-       gint64 ticks;
        FILETIME create_time, exit_time, kernel_time, user_time;
        
        MONO_ARCH_SAVE_REGS;
 
-       ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
-                            &user_time);
-       if(ret==TRUE) {
-               ticks=((guint64)create_time.dwHighDateTime << 32) +
-                       create_time.dwLowDateTime;
-               
-               return(ticks);
-       } else {
-               return(0);
-       }
+       ret = GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
+                                                  &user_time);
+       if (ret)
+               return file_time_to_guint64 (&create_time);
+       else
+               return 0;
 }
 
 gint32 ves_icall_System_Diagnostics_Process_ExitCode_internal (HANDLE process)
@@ -855,9 +775,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);
 }
@@ -883,10 +801,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);
        
@@ -896,28 +811,61 @@ MonoString *ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE pr
 /* Returns an array of pids */
 MonoArray *ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
 {
+#if !defined(HOST_WIN32)
        MonoArray *procs;
-       gboolean ret;
-       DWORD needed;
-       guint32 count, i;
-       DWORD pids[1024];
+       gpointer *pidarray;
+       int i, count;
 
        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]);
+       pidarray = mono_process_list (&count);
+       if (!pidarray)
+               mono_raise_exception (mono_get_exception_not_supported ("This system does not support EnumProcesses"));
+       procs = mono_array_new (mono_domain_get (), mono_get_int32_class (), count);
+       if (sizeof (guint32) == sizeof (gpointer)) {
+               memcpy (mono_array_addr (procs, guint32, 0), pidarray, count);
+       } else {
+               for (i = 0; i < count; ++i)
+                       *(mono_array_addr (procs, guint32, i)) = GPOINTER_TO_UINT (pidarray [i]);
        }
-       
-       return(procs);
+       g_free (pidarray);
+
+       return procs;
+#else
+       MonoArray *procs;
+       gboolean ret;
+       DWORD needed;
+       int count;
+       guint32 *pids;
+
+       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;
+#endif
 }
 
 MonoBoolean ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process, guint32 *min, guint32 *max)
@@ -974,12 +922,15 @@ ves_icall_System_Diagnostics_Process_Times (HANDLE process, gint32 type)
        FILETIME create_time, exit_time, kernel_time, user_time;
        
        if (GetProcessTimes (process, &create_time, &exit_time, &kernel_time, &user_time)) {
+               guint64 ktime = file_time_to_guint64 (&kernel_time);
+               guint64 utime = file_time_to_guint64 (&user_time);
+
                if (type == 0)
-                       return *(gint64*)&user_time;
+                       return utime;
                else if (type == 1)
-                       return *(gint64*)&kernel_time;
-               /* system + user time: FILETIME can be (memory) cast to a 64 bit int */
-               return *(gint64*)&kernel_time + *(gint64*)&user_time;
+                       return ktime;
+               else
+                       return ktime + utime;
        }
        return 0;
 }
@@ -1007,9 +958,7 @@ ves_icall_System_Diagnostics_Process_ProcessHandle_duplicate (HANDLE process)
 
        MONO_ARCH_SAVE_REGS;
 
-#ifdef DEBUG
-       g_message ("%s: Duplicating process handle %p", __func__, process);
-#endif
+       LOGDEBUG (g_message ("%s: Duplicating process handle %p", __func__, process));
        
        DuplicateHandle (GetCurrentProcess (), process, GetCurrentProcess (),
                         &ret, THREAD_ALL_ACCESS, TRUE, 0);
@@ -1022,9 +971,20 @@ ves_icall_System_Diagnostics_Process_ProcessHandle_close (HANDLE process)
 {
        MONO_ARCH_SAVE_REGS;
 
-#ifdef DEBUG
-       g_message ("%s: Closing process handle %p", __func__, process);
-#endif
+       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;
+}
+