2 * process.c: System.Diagnostics.Process support
5 * Dick Porter (dick@ximian.com)
7 * Copyright 2002 Ximian, Inc.
8 * Copyright 2002-2006 Novell, Inc.
16 #include <mono/metadata/object-internals.h>
17 #include <mono/metadata/process.h>
18 #include <mono/metadata/assembly.h>
19 #include <mono/metadata/appdomain.h>
20 #include <mono/metadata/image.h>
21 #include <mono/metadata/cil-coff.h>
22 #include <mono/metadata/exception.h>
23 #include <mono/utils/strenc.h>
24 #include <mono/utils/mono-proclib.h>
25 #include <mono/io-layer/io-layer.h>
26 /* FIXME: fix this code to not depend so much on the inetrnals */
27 #include <mono/metadata/class-internals.h>
30 /* define LOGDEBUG(...) g_message(__VA_ARGS__) */
33 HANDLE ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid)
39 /* GetCurrentProcess returns a pseudo-handle, so use
42 handle=OpenProcess (PROCESS_ALL_ACCESS, TRUE, pid);
45 /* FIXME: Throw an exception */
52 guint32 ves_icall_System_Diagnostics_Process_GetPid_internal (void)
56 return(GetCurrentProcessId ());
59 void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject *this,
65 g_message ("%s: Closing process %p, handle %p", __func__, this, process);
69 CloseHandle (process);
71 CloseProcess (process);
75 #define STASH_SYS_ASS(this) \
76 if(system_assembly == NULL) { \
77 system_assembly=this->vtable->klass->image; \
80 static MonoImage *system_assembly=NULL;
82 static guint32 unicode_chars (const gunichar2 *str)
94 static void process_set_field_object (MonoObject *obj, const gchar *fieldname,
97 MonoClassField *field;
99 LOGDEBUG (g_message ("%s: Setting field %s to object at %p", __func__, fieldname, data));
101 field=mono_class_get_field_from_name (mono_object_class (obj),
103 mono_gc_wbarrier_generic_store (((char *)obj) + field->offset, data);
106 static void process_set_field_string (MonoObject *obj, const gchar *fieldname,
107 const gunichar2 *val, guint32 len)
109 MonoClassField *field;
112 LOGDEBUG (g_message ("%s: Setting field %s to [%s]", __func__, fieldname, g_utf16_to_utf8 (val, len, NULL, NULL, NULL)));
114 string=mono_string_new_utf16 (mono_object_domain (obj), val, len);
116 field=mono_class_get_field_from_name (mono_object_class (obj),
118 mono_gc_wbarrier_generic_store (((char *)obj) + field->offset, (MonoObject*)string);
121 static void process_set_field_int (MonoObject *obj, const gchar *fieldname,
124 MonoClassField *field;
126 LOGDEBUG (g_message ("%s: Setting field %s to %d", __func__,fieldname, val));
128 field=mono_class_get_field_from_name (mono_object_class (obj),
130 *(guint32 *)(((char *)obj) + field->offset)=val;
133 static void process_set_field_intptr (MonoObject *obj, const gchar *fieldname,
136 MonoClassField *field;
138 LOGDEBUG (g_message ("%s: Setting field %s to %p", __func__, fieldname, val));
140 field=mono_class_get_field_from_name (mono_object_class (obj),
142 *(gpointer *)(((char *)obj) + field->offset)=val;
145 static void process_set_field_bool (MonoObject *obj, const gchar *fieldname,
148 MonoClassField *field;
150 LOGDEBUG (g_message ("%s: Setting field %s to %s", __func__, fieldname, val?"TRUE":"FALSE"));
152 field=mono_class_get_field_from_name (mono_object_class (obj),
154 *(guint8 *)(((char *)obj) + field->offset)=val;
157 #define SFI_COMMENTS "\\StringFileInfo\\%02X%02X%02X%02X\\Comments"
158 #define SFI_COMPANYNAME "\\StringFileInfo\\%02X%02X%02X%02X\\CompanyName"
159 #define SFI_FILEDESCRIPTION "\\StringFileInfo\\%02X%02X%02X%02X\\FileDescription"
160 #define SFI_FILEVERSION "\\StringFileInfo\\%02X%02X%02X%02X\\FileVersion"
161 #define SFI_INTERNALNAME "\\StringFileInfo\\%02X%02X%02X%02X\\InternalName"
162 #define SFI_LEGALCOPYRIGHT "\\StringFileInfo\\%02X%02X%02X%02X\\LegalCopyright"
163 #define SFI_LEGALTRADEMARKS "\\StringFileInfo\\%02X%02X%02X%02X\\LegalTrademarks"
164 #define SFI_ORIGINALFILENAME "\\StringFileInfo\\%02X%02X%02X%02X\\OriginalFilename"
165 #define SFI_PRIVATEBUILD "\\StringFileInfo\\%02X%02X%02X%02X\\PrivateBuild"
166 #define SFI_PRODUCTNAME "\\StringFileInfo\\%02X%02X%02X%02X\\ProductName"
167 #define SFI_PRODUCTVERSION "\\StringFileInfo\\%02X%02X%02X%02X\\ProductVersion"
168 #define SFI_SPECIALBUILD "\\StringFileInfo\\%02X%02X%02X%02X\\SpecialBuild"
169 #define EMPTY_STRING (gunichar2*)"\000\000"
171 static void process_module_string_read (MonoObject *filever, gpointer data,
172 const gchar *fieldname,
173 guchar lang_hi, guchar lang_lo,
176 gchar *lang_key_utf8;
177 gunichar2 *lang_key, *buffer;
180 lang_key_utf8 = g_strdup_printf (key, lang_lo, lang_hi, 0x04, 0xb0);
182 LOGDEBUG (g_message ("%s: asking for [%s]", __func__, lang_key_utf8));
184 lang_key = g_utf8_to_utf16 (lang_key_utf8, -1, NULL, NULL, NULL);
186 if (VerQueryValue (data, lang_key, (gpointer *)&buffer, &chars) && chars > 0) {
187 LOGDEBUG (g_message ("%s: found %d chars of [%s]", __func__, chars, g_utf16_to_utf8 (buffer, chars, NULL, NULL, NULL)));
188 /* chars includes trailing null */
189 process_set_field_string (filever, fieldname, buffer, chars - 1);
191 process_set_field_string (filever, fieldname, EMPTY_STRING, 0);
195 g_free (lang_key_utf8);
198 static void process_module_stringtable (MonoObject *filever, gpointer data,
199 guchar lang_hi, guchar lang_lo)
201 process_module_string_read (filever, data, "comments", lang_hi, lang_lo,
203 process_module_string_read (filever, data, "companyname", lang_hi,
204 lang_lo, SFI_COMPANYNAME);
205 process_module_string_read (filever, data, "filedescription", lang_hi,
206 lang_lo, SFI_FILEDESCRIPTION);
207 process_module_string_read (filever, data, "fileversion", lang_hi,
208 lang_lo, SFI_FILEVERSION);
209 process_module_string_read (filever, data, "internalname", lang_hi,
210 lang_lo, SFI_INTERNALNAME);
211 process_module_string_read (filever, data, "legalcopyright", lang_hi,
212 lang_lo, SFI_LEGALCOPYRIGHT);
213 process_module_string_read (filever, data, "legaltrademarks", lang_hi,
214 lang_lo, SFI_LEGALTRADEMARKS);
215 process_module_string_read (filever, data, "originalfilename", lang_hi,
216 lang_lo, SFI_ORIGINALFILENAME);
217 process_module_string_read (filever, data, "privatebuild", lang_hi,
218 lang_lo, SFI_PRIVATEBUILD);
219 process_module_string_read (filever, data, "productname", lang_hi,
220 lang_lo, SFI_PRODUCTNAME);
221 process_module_string_read (filever, data, "productversion", lang_hi,
222 lang_lo, SFI_PRODUCTVERSION);
223 process_module_string_read (filever, data, "specialbuild", lang_hi,
224 lang_lo, SFI_SPECIALBUILD);
227 static void process_get_fileversion (MonoObject *filever, gunichar2 *filename)
230 VS_FIXEDFILEINFO *ffi;
235 UINT ffi_size, trans_size;
237 gunichar2 lang_buf[128];
238 guint32 lang, lang_count;
240 datalen = GetFileVersionInfoSize (filename, &verinfohandle);
242 data = g_malloc0 (datalen);
243 ok = GetFileVersionInfo (filename, verinfohandle, datalen,
246 query = g_utf8_to_utf16 ("\\", -1, NULL, NULL, NULL);
252 if (VerQueryValue (data, query, (gpointer *)&ffi,
254 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)));
256 process_set_field_int (filever, "filemajorpart", HIWORD (ffi->dwFileVersionMS));
257 process_set_field_int (filever, "fileminorpart", LOWORD (ffi->dwFileVersionMS));
258 process_set_field_int (filever, "filebuildpart", HIWORD (ffi->dwFileVersionLS));
259 process_set_field_int (filever, "fileprivatepart", LOWORD (ffi->dwFileVersionLS));
261 process_set_field_int (filever, "productmajorpart", HIWORD (ffi->dwProductVersionMS));
262 process_set_field_int (filever, "productminorpart", LOWORD (ffi->dwProductVersionMS));
263 process_set_field_int (filever, "productbuildpart", HIWORD (ffi->dwProductVersionLS));
264 process_set_field_int (filever, "productprivatepart", LOWORD (ffi->dwProductVersionLS));
266 process_set_field_bool (filever, "isdebug", (ffi->dwFileFlags & ffi->dwFileFlagsMask) & VS_FF_DEBUG);
267 process_set_field_bool (filever, "isprerelease", (ffi->dwFileFlags & ffi->dwFileFlagsMask) & VS_FF_PRERELEASE);
268 process_set_field_bool (filever, "ispatched", (ffi->dwFileFlags & ffi->dwFileFlagsMask) & VS_FF_PATCHED);
269 process_set_field_bool (filever, "isprivatebuild", (ffi->dwFileFlags & ffi->dwFileFlagsMask) & VS_FF_PRIVATEBUILD);
270 process_set_field_bool (filever, "isspecialbuild", (ffi->dwFileFlags & ffi->dwFileFlagsMask) & VS_FF_SPECIALBUILD);
274 query = g_utf8_to_utf16 ("\\VarFileInfo\\Translation", -1, NULL, NULL, NULL);
280 if (VerQueryValue (data, query,
281 (gpointer *)&trans_data,
283 /* use the first language ID we see
285 if (trans_size >= 4) {
286 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]));
287 lang = (trans_data[0]) |
288 (trans_data[1] << 8) |
289 (trans_data[2] << 16) |
290 (trans_data[3] << 24);
291 /* Only give the lower 16 bits
292 * to VerLanguageName, as
293 * Windows gets confused
296 lang_count = VerLanguageName (lang & 0xFFFF, lang_buf, 128);
298 process_set_field_string (filever, "language", lang_buf, lang_count);
300 process_module_stringtable (filever, data, trans_data[0], trans_data[1]);
303 /* No strings, so set every field to
306 process_set_field_string (filever,
309 process_set_field_string (filever,
312 process_set_field_string (filever,
315 process_set_field_string (filever,
318 process_set_field_string (filever,
321 process_set_field_string (filever,
324 process_set_field_string (filever,
327 process_set_field_string (filever,
330 process_set_field_string (filever,
333 process_set_field_string (filever,
336 process_set_field_string (filever,
339 process_set_field_string (filever,
343 /* And language seems to be set to
344 * en_US according to bug 374600
346 lang_count = VerLanguageName (0x0409, lang_buf, 128);
348 process_set_field_string (filever, "language", lang_buf, lang_count);
358 static MonoObject* process_add_module (HANDLE process, HMODULE mod, gunichar2 *filename, gunichar2 *modulename)
360 MonoClass *proc_class, *filever_class;
361 MonoObject *item, *filever;
362 MonoDomain *domain=mono_domain_get ();
366 /* Build a System.Diagnostics.ProcessModule with the data.
368 proc_class=mono_class_from_name (system_assembly, "System.Diagnostics",
370 item=mono_object_new (domain, proc_class);
372 filever_class=mono_class_from_name (system_assembly,
373 "System.Diagnostics",
375 filever=mono_object_new (domain, filever_class);
377 process_get_fileversion (filever, filename);
379 process_set_field_string (filever, "filename", filename,
380 unicode_chars (filename));
382 ok = GetModuleInformation (process, mod, &modinfo, sizeof(MODULEINFO));
384 process_set_field_intptr (item, "baseaddr",
385 modinfo.lpBaseOfDll);
386 process_set_field_intptr (item, "entryaddr",
388 process_set_field_int (item, "memory_size",
389 modinfo.SizeOfImage);
391 process_set_field_string (item, "filename", filename,
392 unicode_chars (filename));
393 process_set_field_string (item, "modulename", modulename,
394 unicode_chars (modulename));
395 process_set_field_object (item, "version_info", filever);
400 /* Returns an array of System.Diagnostics.ProcessModule */
401 MonoArray *ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject *this, HANDLE process)
403 MonoArray *temp_arr = NULL;
406 gunichar2 filename[MAX_PATH];
407 gunichar2 modname[MAX_PATH];
410 guint32 i, num_added = 0;
412 STASH_SYS_ASS (this);
414 if (EnumProcessModules (process, mods, sizeof(mods), &needed)) {
415 count = needed / sizeof(HMODULE);
416 temp_arr = mono_array_new (mono_domain_get (), mono_get_object_class (), count);
417 for (i = 0; i < count; i++) {
418 if (GetModuleBaseName (process, mods[i], modname, MAX_PATH) &&
419 GetModuleFileNameEx (process, mods[i], filename, MAX_PATH)) {
420 MonoObject *module = process_add_module (process, mods[i],
422 mono_array_setref (temp_arr, num_added++, module);
427 if (count == num_added) {
430 /* shorter version of the array */
431 arr = mono_array_new (mono_domain_get (), mono_get_object_class (), num_added);
433 for (i = 0; i < num_added; i++)
434 mono_array_setref (arr, i, mono_array_get (temp_arr, MonoObject*, i));
440 void ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal (MonoObject *this, MonoString *filename)
444 STASH_SYS_ASS (this);
446 process_get_fileversion (this, mono_string_chars (filename));
447 process_set_field_string (this, "filename",
448 mono_string_chars (filename),
449 mono_string_length (filename));
452 /* Only used when UseShellExecute is false */
454 quote_path (const gchar *path)
456 gchar *res = g_shell_quote (path);
470 /* Only used when UseShellExecute is false */
472 complete_path (const gunichar2 *appname, gchar **completed)
474 gchar *utf8app, *utf8appmemory;
477 utf8appmemory = utf8app = g_utf16_to_utf8 (appname, -1, NULL, NULL, NULL);
478 #ifdef TARGET_WIN32 // Should this happen on all platforms?
480 // remove the quotes around utf8app.
482 len = strlen (utf8app);
484 if (utf8app[len-1] == '\"')
485 utf8app[len-1] = '\0';
486 if (utf8app[0] == '\"')
492 if (g_path_is_absolute (utf8app)) {
493 *completed = quote_path (utf8app);
494 g_free (utf8appmemory);
498 if (g_file_test (utf8app, G_FILE_TEST_IS_EXECUTABLE) && !g_file_test (utf8app, G_FILE_TEST_IS_DIR)) {
499 *completed = quote_path (utf8app);
500 g_free (utf8appmemory);
504 found = g_find_program_in_path (utf8app);
507 g_free (utf8appmemory);
511 *completed = quote_path (found);
513 g_free (utf8appmemory);
517 #if defined (MINGW_CROSS_COMPILE) && defined (HAVE_GETPROCESSID)
518 #undef HAVE_GETPROCESSID
521 #ifndef HAVE_GETPROCESSID
522 /* Run-time GetProcessId detection for Windows */
524 #define HAVE_GETPROCESSID
526 typedef DWORD (WINAPI *GETPROCESSID_PROC) (HANDLE);
527 typedef DWORD (WINAPI *NTQUERYINFORMATIONPROCESS_PROC) (HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
528 typedef DWORD (WINAPI *RTLNTSTATUSTODOSERROR_PROC) (NTSTATUS);
530 static DWORD WINAPI GetProcessId_detect (HANDLE process);
532 static GETPROCESSID_PROC GetProcessId = &GetProcessId_detect;
533 static NTQUERYINFORMATIONPROCESS_PROC NtQueryInformationProcess_proc = NULL;
534 static RTLNTSTATUSTODOSERROR_PROC RtlNtStatusToDosError_proc = NULL;
536 static DWORD WINAPI GetProcessId_ntdll (HANDLE process)
538 PROCESS_BASIC_INFORMATION pi;
541 status = NtQueryInformationProcess_proc (process, ProcessBasicInformation, &pi, sizeof (pi), NULL);
542 if (NT_SUCCESS (status)) {
543 return pi.UniqueProcessId;
545 SetLastError (RtlNtStatusToDosError_proc (status));
550 static DWORD WINAPI GetProcessId_stub (HANDLE process)
552 SetLastError (ERROR_CALL_NOT_IMPLEMENTED);
556 static DWORD WINAPI GetProcessId_detect (HANDLE process)
558 HMODULE module_handle;
559 GETPROCESSID_PROC GetProcessId_kernel;
561 /* Windows XP SP1 and above have GetProcessId API */
562 module_handle = GetModuleHandle (L"kernel32.dll");
563 if (module_handle != NULL) {
564 GetProcessId_kernel = (GETPROCESSID_PROC) GetProcAddress (module_handle, "GetProcessId");
565 if (GetProcessId_kernel != NULL) {
566 GetProcessId = GetProcessId_kernel;
567 return GetProcessId (process);
571 /* Windows 2000 and above have deprecated NtQueryInformationProcess API */
572 module_handle = GetModuleHandle (L"ntdll.dll");
573 if (module_handle != NULL) {
574 NtQueryInformationProcess_proc = (NTQUERYINFORMATIONPROCESS_PROC) GetProcAddress (module_handle, "NtQueryInformationProcess");
575 if (NtQueryInformationProcess_proc != NULL) {
576 RtlNtStatusToDosError_proc = (RTLNTSTATUSTODOSERROR_PROC) GetProcAddress (module_handle, "RtlNtStatusToDosError");
577 if (RtlNtStatusToDosError_proc != NULL) {
578 GetProcessId = &GetProcessId_ntdll;
579 return GetProcessId (process);
584 /* Fall back to ERROR_CALL_NOT_IMPLEMENTED */
585 GetProcessId = &GetProcessId_stub;
586 return GetProcessId (process);
588 #endif /* HOST_WIN32 */
589 #endif /* !HAVE_GETPROCESSID */
591 MonoBoolean ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoProcessStartInfo *proc_start_info, MonoProcInfo *process_info)
593 SHELLEXECUTEINFO shellex = {0};
596 shellex.cbSize = sizeof(SHELLEXECUTEINFO);
597 shellex.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_NOCLOSEPROCESS | SEE_MASK_UNICODE;
598 shellex.nShow = proc_start_info->window_style;
599 shellex.nShow = (shellex.nShow == 0) ? 1 : (shellex.nShow == 1 ? 0 : shellex.nShow);
602 if (proc_start_info->filename != NULL) {
603 shellex.lpFile = mono_string_chars (proc_start_info->filename);
606 if (proc_start_info->arguments != NULL) {
607 shellex.lpParameters = mono_string_chars (proc_start_info->arguments);
610 if (proc_start_info->verb != NULL &&
611 mono_string_length (proc_start_info->verb) != 0) {
612 shellex.lpVerb = mono_string_chars (proc_start_info->verb);
615 if (proc_start_info->working_directory != NULL &&
616 mono_string_length (proc_start_info->working_directory) != 0) {
617 shellex.lpDirectory = mono_string_chars (proc_start_info->working_directory);
620 if (proc_start_info->error_dialog) {
621 shellex.hwnd = proc_start_info->error_dialog_parent_handle;
623 shellex.fMask |= SEE_MASK_FLAG_NO_UI;
626 ret = ShellExecuteEx (&shellex);
628 process_info->pid = -GetLastError ();
630 process_info->process_handle = shellex.hProcess;
631 process_info->thread_handle = NULL;
632 /* It appears that there's no way to get the pid from a
633 * process handle before windows xp. Really.
635 #if defined(HAVE_GETPROCESSID) && !defined(MONO_CROSS_COMPILE)
636 process_info->pid = GetProcessId (shellex.hProcess);
638 process_info->pid = 0;
640 process_info->tid = 0;
646 MonoBoolean ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoProcessStartInfo *proc_start_info, HANDLE stdin_handle, HANDLE stdout_handle, HANDLE stderr_handle, MonoProcInfo *process_info)
650 STARTUPINFO startinfo={0};
651 PROCESS_INFORMATION procinfo;
652 gunichar2 *shell_path = NULL;
653 gchar *env_vars = NULL;
654 gboolean free_shell_path = TRUE;
656 MonoString *cmd = proc_start_info->arguments;
657 guint32 creation_flags, logon_flags;
659 startinfo.cb=sizeof(STARTUPINFO);
660 startinfo.dwFlags=STARTF_USESTDHANDLES;
661 startinfo.hStdInput=stdin_handle;
662 startinfo.hStdOutput=stdout_handle;
663 startinfo.hStdError=stderr_handle;
665 creation_flags = CREATE_UNICODE_ENVIRONMENT;
666 if (proc_start_info->create_no_window)
667 creation_flags |= CREATE_NO_WINDOW;
669 shell_path = mono_string_chars (proc_start_info->filename);
670 complete_path (shell_path, &spath);
672 process_info->pid = -ERROR_FILE_NOT_FOUND;
676 /* Seems like our CreateProcess does not work as the windows one.
677 * This hack is needed to deal with paths containing spaces */
679 free_shell_path = FALSE;
682 tmp = mono_string_to_utf8 (cmd);
683 newcmd = g_strdup_printf ("%s %s", spath, tmp);
684 cmd = mono_string_new_wrapper (newcmd);
689 cmd = mono_string_new_wrapper (spath);
692 shell_path = g_utf8_to_utf16 (spath, -1, NULL, NULL, NULL);
696 if (process_info->env_keys != NULL) {
699 MonoString *key, *value;
700 gunichar2 *str, *ptr;
703 for (len = 0, i = 0; i < mono_array_length (process_info->env_keys); i++) {
704 ms = mono_array_get (process_info->env_values, MonoString *, i);
708 len += mono_string_length (ms) * sizeof (gunichar2);
709 ms = mono_array_get (process_info->env_keys, MonoString *, i);
710 len += mono_string_length (ms) * sizeof (gunichar2);
711 len += 2 * sizeof (gunichar2);
714 equals16 = g_utf8_to_utf16 ("=", 1, NULL, NULL, NULL);
715 ptr = str = g_new0 (gunichar2, len + 1);
716 for (i = 0; i < mono_array_length (process_info->env_keys); i++) {
717 value = mono_array_get (process_info->env_values, MonoString *, i);
721 key = mono_array_get (process_info->env_keys, MonoString *, i);
722 memcpy (ptr, mono_string_chars (key), mono_string_length (key) * sizeof (gunichar2));
723 ptr += mono_string_length (key);
725 memcpy (ptr, equals16, sizeof (gunichar2));
728 memcpy (ptr, mono_string_chars (value), mono_string_length (value) * sizeof (gunichar2));
729 ptr += mono_string_length (value);
734 env_vars = (gchar *) str;
737 /* The default dir name is "". Turn that into NULL to mean
738 * "current directory"
740 if(mono_string_length (proc_start_info->working_directory)==0) {
743 dir=mono_string_chars (proc_start_info->working_directory);
746 if (process_info->username) {
747 logon_flags = process_info->load_user_profile ? LOGON_WITH_PROFILE : 0;
748 ret=CreateProcessWithLogonW (mono_string_chars (process_info->username), process_info->domain ? mono_string_chars (process_info->domain) : NULL, process_info->password, logon_flags, shell_path, cmd? mono_string_chars (cmd): NULL, creation_flags, env_vars, dir, &startinfo, &procinfo);
750 ret=CreateProcess (shell_path, cmd? mono_string_chars (cmd): NULL, NULL, NULL, TRUE, creation_flags, env_vars, dir, &startinfo, &procinfo);
758 process_info->process_handle=procinfo.hProcess;
759 /*process_info->thread_handle=procinfo.hThread;*/
760 process_info->thread_handle=NULL;
761 if (procinfo.hThread != NULL && procinfo.hThread != INVALID_HANDLE_VALUE)
762 CloseHandle(procinfo.hThread);
763 process_info->pid=procinfo.dwProcessId;
764 process_info->tid=procinfo.dwThreadId;
766 process_info->pid = -GetLastError ();
772 MonoBoolean ves_icall_System_Diagnostics_Process_WaitForExit_internal (MonoObject *this, HANDLE process, gint32 ms)
780 ret=WaitForSingleObjectEx (process, INFINITE, TRUE);
782 ret=WaitForSingleObjectEx (process, ms, TRUE);
784 if(ret==WAIT_OBJECT_0) {
791 MonoBoolean ves_icall_System_Diagnostics_Process_WaitForInputIdle_internal (MonoObject *this, HANDLE process, gint32 ms)
799 ret=WaitForInputIdle (process, INFINITE);
801 ret=WaitForInputIdle (process, ms);
804 return (ret) ? FALSE : TRUE;
807 gint64 ves_icall_System_Diagnostics_Process_ExitTime_internal (HANDLE process)
811 FILETIME create_time, exit_time, kernel_time, user_time;
815 ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
818 ticks=((guint64)exit_time.dwHighDateTime << 32) +
819 exit_time.dwLowDateTime;
827 gint64 ves_icall_System_Diagnostics_Process_StartTime_internal (HANDLE process)
831 FILETIME create_time, exit_time, kernel_time, user_time;
835 ret=GetProcessTimes (process, &create_time, &exit_time, &kernel_time,
838 ticks=((guint64)create_time.dwHighDateTime << 32) +
839 create_time.dwLowDateTime;
847 gint32 ves_icall_System_Diagnostics_Process_ExitCode_internal (HANDLE process)
853 GetExitCodeProcess (process, &code);
855 LOGDEBUG (g_message ("%s: process exit code is %d", __func__, code));
860 MonoString *ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE process)
865 gunichar2 name[MAX_PATH];
871 ok=EnumProcessModules (process, &mod, sizeof(mod), &needed);
876 len=GetModuleBaseName (process, mod, name, MAX_PATH);
881 LOGDEBUG (g_message ("%s: process name is [%s]", __func__, g_utf16_to_utf8 (name, -1, NULL, NULL, NULL)));
883 string=mono_string_new_utf16 (mono_domain_get (), name, len);
888 /* Returns an array of pids */
889 MonoArray *ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
901 pids = g_new0 (guint32, count);
902 ret = EnumProcesses (pids, count * sizeof (guint32), &needed);
908 exc = mono_get_exception_not_supported ("This system does not support EnumProcesses");
909 mono_raise_exception (exc);
910 g_assert_not_reached ();
912 if (needed < (count * sizeof (guint32)))
916 count = (count * 3) / 2;
919 count = needed / sizeof (guint32);
920 procs = mono_array_new (mono_domain_get (), mono_get_int32_class (), count);
921 memcpy (mono_array_addr (procs, guint32, 0), pids, needed);
928 MonoBoolean ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process, guint32 *min, guint32 *max)
931 SIZE_T ws_min, ws_max;
935 ret=GetProcessWorkingSetSize (process, &ws_min, &ws_max);
936 *min=(guint32)ws_min;
937 *max=(guint32)ws_max;
942 MonoBoolean ves_icall_System_Diagnostics_Process_SetWorkingSet_internal (HANDLE process, guint32 min, guint32 max, MonoBoolean use_min)
950 ret=GetProcessWorkingSetSize (process, &ws_min, &ws_max);
961 ret=SetProcessWorkingSetSize (process, ws_min, ws_max);
967 ves_icall_System_Diagnostics_Process_Kill_internal (HANDLE process, gint32 sig)
971 /* sig == 1 -> Kill, sig == 2 -> CloseMainWindow */
973 return TerminateProcess (process, -sig);
977 ves_icall_System_Diagnostics_Process_Times (HANDLE process, gint32 type)
979 FILETIME create_time, exit_time, kernel_time, user_time;
981 if (GetProcessTimes (process, &create_time, &exit_time, &kernel_time, &user_time)) {
983 return *(gint64*)&user_time;
985 return *(gint64*)&kernel_time;
986 /* system + user time: FILETIME can be (memory) cast to a 64 bit int */
987 return *(gint64*)&kernel_time + *(gint64*)&user_time;
993 ves_icall_System_Diagnostics_Process_GetPriorityClass (HANDLE process, gint32 *error)
995 gint32 ret = GetPriorityClass (process);
996 *error = ret == 0 ? GetLastError () : 0;
1001 ves_icall_System_Diagnostics_Process_SetPriorityClass (HANDLE process, gint32 priority_class, gint32 *error)
1003 gboolean ret = SetPriorityClass (process, priority_class);
1004 *error = ret == 0 ? GetLastError () : 0;
1009 ves_icall_System_Diagnostics_Process_ProcessHandle_duplicate (HANDLE process)
1013 MONO_ARCH_SAVE_REGS;
1015 LOGDEBUG (g_message ("%s: Duplicating process handle %p", __func__, process));
1017 DuplicateHandle (GetCurrentProcess (), process, GetCurrentProcess (),
1018 &ret, THREAD_ALL_ACCESS, TRUE, 0);
1024 ves_icall_System_Diagnostics_Process_ProcessHandle_close (HANDLE process)
1026 MONO_ARCH_SAVE_REGS;
1028 LOGDEBUG (g_message ("%s: Closing process handle %p", __func__, process));
1030 CloseHandle (process);
1034 ves_icall_System_Diagnostics_Process_GetProcessData (int pid, gint32 data_type, gint32 *error)
1036 MonoProcessError perror;
1039 res = mono_process_get_data_with_error (GINT_TO_POINTER (pid), data_type, &perror);