2 * process.c: System.Diagnostics.Process support
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
15 #include <mono/metadata/object.h>
16 #include <mono/metadata/process.h>
17 #include <mono/metadata/assembly.h>
18 #include <mono/metadata/appdomain.h>
19 #include <mono/metadata/image.h>
20 #include <mono/metadata/cil-coff.h>
21 #include <mono/metadata/exception.h>
22 #include <mono/utils/strenc.h>
23 #include <mono/io-layer/io-layer.h>
24 /* FIXME: fix this code to not depend so much on the inetrnals */
25 #include <mono/metadata/class-internals.h>
29 HANDLE
ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid
)
35 /* GetCurrentProcess returns a pseudo-handle, so use
38 handle
=OpenProcess (PROCESS_ALL_ACCESS
, TRUE
, pid
);
41 /* FIXME: Throw an exception */
48 guint32
ves_icall_System_Diagnostics_Process_GetPid_internal (void)
52 return(GetCurrentProcessId ());
55 void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject
*this,
61 g_message (G_GNUC_PRETTY_FUNCTION
": Closing process %p, handle %p",
65 CloseHandle (process
);
68 #define STASH_SYS_ASS(this) \
69 if(system_assembly == NULL) { \
70 system_assembly=this->vtable->klass->image; \
73 static MonoImage
*system_assembly
=NULL
;
75 static guint32
unicode_chars (const gunichar2
*str
)
87 static guint32
unicode_bytes (const gunichar2
*str
)
93 /* Include the terminators */
101 * compare a null-terminated utf16 string and a normal string.
102 * Can be used only for ascii or latin1 chars.
105 unicode_string_equals (const gunichar2
*str1
, const guchar
*str2
)
107 while (*str1
&& *str2
) {
113 return *str1
== *str2
;
116 static void process_set_field_object (MonoObject
*obj
, const guchar
*fieldname
,
119 MonoClassField
*field
;
122 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to object at %p",
126 field
=mono_class_get_field_from_name (mono_object_class (obj
),
128 *(MonoObject
**)(((char *)obj
) + field
->offset
)=data
;
131 static void process_set_field_string (MonoObject
*obj
, const guchar
*fieldname
,
132 const gunichar2
*val
, guint32 len
)
134 MonoClassField
*field
;
138 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to [%s]",
139 fieldname
, g_utf16_to_utf8 (val
, len
, NULL
, NULL
, NULL
));
142 string
=mono_string_new_utf16 (mono_object_domain (obj
), val
, len
);
144 field
=mono_class_get_field_from_name (mono_object_class (obj
),
146 *(MonoString
**)(((char *)obj
) + field
->offset
)=string
;
149 static void process_set_field_string_utf8 (MonoObject
*obj
,
150 const guchar
*fieldname
,
153 MonoClassField
*field
;
157 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to [%s]",
161 string
=mono_string_new (mono_object_domain (obj
), val
);
163 field
=mono_class_get_field_from_name (mono_object_class (obj
),
165 *(MonoString
**)(((char *)obj
) + field
->offset
)=string
;
168 static void process_set_field_int (MonoObject
*obj
, const guchar
*fieldname
,
171 MonoClassField
*field
;
174 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to %d",
178 field
=mono_class_get_field_from_name (mono_object_class (obj
),
180 *(guint32
*)(((char *)obj
) + field
->offset
)=val
;
183 static void process_set_field_bool (MonoObject
*obj
, const guchar
*fieldname
,
186 MonoClassField
*field
;
189 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to %s",
190 fieldname
, val
?"TRUE":"FALSE");
193 field
=mono_class_get_field_from_name (mono_object_class (obj
),
195 *(guint8
*)(((char *)obj
) + field
->offset
)=val
;
205 /* Returns a pointer to the value data, because theres no way to know
206 * how big that data is (value_len is set to zero for most blocks :-()
208 static gpointer
process_get_versioninfo_block (gpointer data
,
211 block
->data_len
=*((guint16
*)data
);
212 data
= (char *)data
+ sizeof(guint16
);
213 block
->value_len
=*((guint16
*)data
);
214 data
= (char *)data
+ sizeof(guint16
);
216 /* No idea what the type is supposed to indicate */
217 block
->type
=*((guint16
*)data
);
218 data
= (char *)data
+ sizeof(guint16
);
219 block
->key
=((gunichar2
*)data
);
221 /* skip over the key (including the terminator) */
222 data
=((gunichar2
*)data
)+(unicode_chars (block
->key
)+1);
224 /* align on a 32-bit boundary */
225 data
=(gpointer
)((char *)data
+ 3);
226 data
=(gpointer
)((char *)data
- (GPOINTER_TO_INT(data
) & 3));
231 /* Returns a pointer to the byte following the Var block */
232 static gpointer
process_read_var_block (MonoObject
*filever
, gpointer data_ptr
,
235 /* Not currently interested in the VarFileInfo block. This
236 * might change if language support is needed for file version
237 * strings (VarFileInfo contains lists of supported
242 /* data_ptr is pointing at a Var block of length data_len */
243 data_ptr
=process_get_versioninfo_block (data_ptr
, &block
);
244 data_ptr
=((guchar
*)data_ptr
)+block
.value_len
;
249 /* Returns a pointer to the byte following the String block, or NULL
250 * if the data read hits padding. We can't recover from this because
251 * the data length does not include padding bytes, so it's not
252 * possible to just return the start position + length.
254 static gpointer
process_read_string_block (MonoObject
*filever
,
260 guint16 string_len
=28; /* Length of the StringTable block */
262 /* data_ptr is pointing at an array of one or more String
263 * blocks with total length (not including alignment padding)
266 while(string_len
<data_len
) {
269 /* align on a 32-bit boundary */
270 data_ptr
=(gpointer
)((char *)data_ptr
+ 3);
271 data_ptr
=(gpointer
)((char *)data_ptr
-
272 (GPOINTER_TO_INT(data_ptr
) & 3));
274 data_ptr
=process_get_versioninfo_block (data_ptr
, &block
);
275 if(block
.data_len
==0) {
276 /* We must have hit padding, so give up
280 g_message (G_GNUC_PRETTY_FUNCTION
281 ": Hit 0-length block, giving up");
286 string_len
=string_len
+block
.data_len
;
287 value
=(gunichar2
*)data_ptr
;
288 /* Skip over the value */
289 data_ptr
=((gunichar2
*)data_ptr
)+block
.value_len
;
292 if (unicode_string_equals (block
.key
, "Comments")) {
293 process_set_field_string (filever
, "comments", value
, unicode_chars (value
));
294 } else if (unicode_string_equals (block
.key
, "CompanyName")) {
295 process_set_field_string (filever
, "companyname", value
, unicode_chars (value
));
296 } else if (unicode_string_equals (block
.key
, "FileDescription")) {
297 process_set_field_string (filever
, "filedescription", value
, unicode_chars (value
));
298 } else if (unicode_string_equals (block
.key
, "FileVersion")) {
299 process_set_field_string (filever
, "fileversion", value
, unicode_chars (value
));
300 } else if (unicode_string_equals (block
.key
, "InternalName")) {
301 process_set_field_string (filever
, "internalname", value
, unicode_chars (value
));
302 } else if (unicode_string_equals (block
.key
, "LegalCopyright")) {
303 process_set_field_string (filever
, "legalcopyright", value
, unicode_chars (value
));
304 } else if (unicode_string_equals (block
.key
, "LegalTrademarks")) {
305 process_set_field_string (filever
, "legaltrademarks", value
, unicode_chars (value
));
306 } else if (unicode_string_equals (block
.key
, "OriginalFilename")) {
307 process_set_field_string (filever
, "originalfilename", value
, unicode_chars (value
));
308 } else if (unicode_string_equals (block
.key
, "PrivateBuild")) {
309 process_set_field_string (filever
, "privatebuild", value
, unicode_chars (value
));
310 } else if (unicode_string_equals (block
.key
, "ProductName")) {
311 process_set_field_string (filever
, "productname", value
, unicode_chars (value
));
312 } else if (unicode_string_equals (block
.key
, "ProductVersion")) {
313 process_set_field_string (filever
, "productversion", value
, unicode_chars (value
));
314 } else if (unicode_string_equals (block
.key
, "SpecialBuild")) {
315 process_set_field_string (filever
, "specialbuild", value
, unicode_chars (value
));
317 /* Not an error, just not interesting
327 /* returns a pointer to the byte following the Stringtable block, or
328 * NULL if the data read hits padding. We can't recover from this
329 * because the data length does not include padding bytes, so it's not
330 * possible to just return the start position + length
332 static gpointer
process_read_stringtable_block (MonoObject
*filever
,
338 guint16 string_len
=36; /* length of the StringFileInfo block */
340 /* data_ptr is pointing at an array of StringTable blocks,
341 * with total length (not including alignment padding) of
345 while(string_len
<data_len
) {
346 /* align on a 32-bit boundary */
347 data_ptr
=(gpointer
)((char *)data_ptr
+ 3);
348 data_ptr
=(gpointer
)((char *)data_ptr
-
349 (GPOINTER_TO_INT(data_ptr
) & 3));
351 data_ptr
=process_get_versioninfo_block (data_ptr
, &block
);
352 if(block
.data_len
==0) {
353 /* We must have hit padding, so give up
357 g_message (G_GNUC_PRETTY_FUNCTION
358 ": Hit 0-length block, giving up");
362 string_len
=string_len
+block
.data_len
;
364 language
= g_utf16_to_utf8 (block
.key
, unicode_bytes (block
.key
), NULL
, NULL
, NULL
);
365 g_strdown (language
);
367 /* Kludge: treat en_US as neutral too */
368 if (!strcmp (language
, "007f04b0") ||
369 !strcmp (language
, "000004b0") ||
370 !strcmp (language
, "040904b0")) {
371 /* Got the one we're interested in */
372 process_set_field_string_utf8 (filever
, "language",
375 data_ptr
=process_read_string_block (filever
, data_ptr
,
379 /* Some other language. We might want to do
380 * something with this in the future.
382 data_ptr
=process_read_string_block (filever
, data_ptr
,
389 /* Child block hit padding */
391 g_message (G_GNUC_PRETTY_FUNCTION
": Child block hit 0-length block, giving up");
400 static void process_read_fixedfileinfo_block (MonoObject
*filever
,
401 VS_FIXEDFILEINFO
*ffi
)
404 g_message (G_GNUC_PRETTY_FUNCTION
": ffi: sig 0x%x, strucver 0x%x, fileverm 0x%x, fileverl 0x%x, prodverm 0x%x, prodverl 0x%x, ffmask 0x%x, ff 0x%x, os 0x%x, type 0x%x, subtype 0x%x, datem 0x%x, datel 0x%x", ffi
->dwSignature
, ffi
->dwStrucVersion
, ffi
->dwFileVersionMS
, ffi
->dwFileVersionLS
, ffi
->dwProductVersionMS
, ffi
->dwProductVersionLS
, ffi
->dwFileFlagsMask
, ffi
->dwFileFlags
, ffi
->dwFileOS
, ffi
->dwFileType
, ffi
->dwFileSubtype
, ffi
->dwFileDateMS
, ffi
->dwFileDateLS
);
407 process_set_field_int (filever
, "filemajorpart",
408 HIWORD (ffi
->dwFileVersionMS
));
409 process_set_field_int (filever
, "fileminorpart",
410 LOWORD (ffi
->dwFileVersionMS
));
411 process_set_field_int (filever
, "filebuildpart",
412 HIWORD (ffi
->dwFileVersionLS
));
413 process_set_field_int (filever
, "fileprivatepart",
414 LOWORD (ffi
->dwFileVersionLS
));
416 process_set_field_int (filever
, "productmajorpart",
417 HIWORD (ffi
->dwProductVersionMS
));
418 process_set_field_int (filever
, "productminorpart",
419 LOWORD (ffi
->dwProductVersionMS
));
420 process_set_field_int (filever
, "productbuildpart",
421 HIWORD (ffi
->dwProductVersionLS
));
422 process_set_field_int (filever
, "productprivatepart",
423 LOWORD (ffi
->dwProductVersionLS
));
425 process_set_field_bool (filever
, "isdebug",
426 ffi
->dwFileFlags
&VS_FF_DEBUG
);
427 process_set_field_bool (filever
, "isprerelease",
428 ffi
->dwFileFlags
&VS_FF_PRERELEASE
);
429 process_set_field_bool (filever
, "ispatched",
430 ffi
->dwFileFlags
&VS_FF_PATCHED
);
431 process_set_field_bool (filever
, "isprivatebuild",
432 ffi
->dwFileFlags
&VS_FF_PRIVATEBUILD
);
433 process_set_field_bool (filever
, "isspecialbuild",
434 ffi
->dwFileFlags
&VS_FF_SPECIALBUILD
);
437 static void process_get_fileversion (MonoObject
*filever
, MonoImage
*image
)
439 MonoPEResourceDataEntry
*version_info
;
441 VS_FIXEDFILEINFO
*ffi
;
444 gint32 data_len
; /* signed to guard against underflow */
446 version_info
=mono_image_lookup_resource (image
,
447 MONO_PE_RESOURCE_ID_VERSION
,
450 g_message (G_GNUC_PRETTY_FUNCTION
": image_lookup returned %p",
454 if(version_info
==NULL
) {
458 data
=mono_image_rva_map (image
,
459 version_info
->rde_data_offset
);
464 /* See io-layer/versioninfo.h for the gory details on how this
465 * data is laid out. (data should be pointing to
466 * VS_VERSIONINFO data).
469 data_ptr
=process_get_versioninfo_block (data
, &block
);
471 data_len
=block
.data_len
;
473 if(block
.value_len
!=sizeof(VS_FIXEDFILEINFO
)) {
475 g_message (G_GNUC_PRETTY_FUNCTION
476 ": FIXEDFILEINFO size mismatch");
481 if (!unicode_string_equals (block
.key
, "VS_VERSION_INFO")) {
483 g_message (G_GNUC_PRETTY_FUNCTION
484 ": VS_VERSION_INFO mismatch");
489 ffi
=((VS_FIXEDFILEINFO
*)data_ptr
);
490 data_ptr
= (char *)data_ptr
+ sizeof(VS_FIXEDFILEINFO
);
491 if((ffi
->dwSignature
!=VS_FFI_SIGNATURE
) ||
492 (ffi
->dwStrucVersion
!=VS_FFI_STRUCVERSION
)) {
494 g_message (G_GNUC_PRETTY_FUNCTION
495 ": FIXEDFILEINFO bad signature");
499 process_read_fixedfileinfo_block (filever
, ffi
);
501 /* Subtract the 92 bytes we've already seen */
504 /* There now follow zero or one StringFileInfo blocks and zero
505 * or one VarFileInfo blocks
507 while(data_len
> 0) {
508 /* align on a 32-bit boundary */
509 data_ptr
=(gpointer
)((char *)data_ptr
+ 3);
510 data_ptr
=(gpointer
)((char *)data_ptr
-
511 (GPOINTER_TO_INT(data_ptr
) & 3));
513 data_ptr
=process_get_versioninfo_block (data_ptr
, &block
);
514 if(block
.data_len
==0) {
515 /* We must have hit padding, so give up
519 g_message (G_GNUC_PRETTY_FUNCTION
520 ": Hit 0-length block, giving up");
525 data_len
=data_len
-block
.data_len
;
527 if (unicode_string_equals (block
.key
, "VarFileInfo")) {
528 data_ptr
=process_read_var_block (filever
, data_ptr
,
530 } else if (unicode_string_equals (block
.key
, "StringFileInfo")) {
531 data_ptr
=process_read_stringtable_block (filever
, data_ptr
, block
.data_len
);
535 g_message (G_GNUC_PRETTY_FUNCTION
536 ": Not a valid VERSIONINFO child block");
542 /* Child block hit padding */
544 g_message (G_GNUC_PRETTY_FUNCTION
": Child block hit 0-length block, giving up");
551 static void process_add_module (GPtrArray
*modules
, MonoAssembly
*ass
)
553 MonoClass
*proc_class
, *filever_class
;
554 MonoObject
*item
, *filever
;
555 MonoDomain
*domain
=mono_domain_get ();
557 const char* filename
;
559 /* Build a System.Diagnostics.ProcessModule with the data.
560 * Leave BaseAddress and EntryPointAddress set to NULL,
561 * FileName is ass->image->name, FileVersionInfo is an object
562 * constructed from the PE image data referenced by
563 * ass->image, ModuleMemorySize set to 0, ModuleName the last
564 * component of FileName.
566 proc_class
=mono_class_from_name (system_assembly
, "System.Diagnostics",
568 item
=mono_object_new (domain
, proc_class
);
570 filever_class
=mono_class_from_name (system_assembly
,
571 "System.Diagnostics",
573 filever
=mono_object_new (domain
, filever_class
);
576 g_message (G_GNUC_PRETTY_FUNCTION
": recording assembly: FileName [%s] FileVersionInfo [%d.%d.%d.%d], ModuleName [%s]", ass
->image
->name
, ass
->aname
.major
, ass
->aname
.minor
, ass
->aname
.build
, ass
->aname
.revision
, ass
->image
->name
);
579 process_get_fileversion (filever
, mono_assembly_get_image (ass
));
581 filename
= mono_image_get_filename (mono_assembly_get_image (ass
));
582 process_set_field_string_utf8 (filever
, "filename", filename
);
583 process_set_field_string_utf8 (item
, "filename", filename
);
584 process_set_field_object (item
, "version_info", filever
);
586 modulename
=g_path_get_basename (filename
);
587 process_set_field_string_utf8 (item
, "modulename", modulename
);
590 g_ptr_array_add (modules
, item
);
593 static void process_scan_modules (gpointer data
, gpointer user_data
)
595 MonoAssembly
*ass
=data
;
596 GPtrArray
*modules
=user_data
;
598 /* The main assembly is already in the list */
599 if(mono_assembly_get_main () != ass
) {
600 process_add_module (modules
, ass
);
605 /* Returns an array of System.Diagnostics.ProcessModule */
606 MonoArray
*ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject
*this)
608 /* I was going to use toolhelp for this, but then realised I
609 * was being an idiot :)
611 * (Toolhelp would give shared libraries open by the runtime,
612 * as well as open assemblies. On windows my tests didnt find
613 * the assemblies loaded by mono either.)
615 GPtrArray
*modules_list
=g_ptr_array_new ();
621 STASH_SYS_ASS (this);
623 /* Make sure the first entry is the main module */
624 process_add_module (modules_list
, mono_assembly_get_main ());
626 mono_assembly_foreach (process_scan_modules
, modules_list
);
628 /* Build a MonoArray out of modules_list */
629 arr
=mono_array_new (mono_domain_get (), mono_get_object_class (),
632 for(i
=0; i
<modules_list
->len
; i
++) {
633 mono_array_set (arr
, MonoObject
*, i
,
634 g_ptr_array_index (modules_list
, i
));
637 g_ptr_array_free (modules_list
, FALSE
);
642 void ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal (MonoObject
*this, MonoString
*filename
)
645 guchar
*filename_utf8
;
649 STASH_SYS_ASS (this);
651 filename_utf8
= mono_string_to_utf8 (filename
);
652 image
= mono_pe_file_open (filename_utf8
, NULL
);
653 g_free (filename_utf8
);
656 /* FIXME: an exception might be appropriate here */
658 g_message (G_GNUC_PRETTY_FUNCTION
": Failed to load image");
664 process_get_fileversion (this, image
);
665 process_set_field_string_utf8 (this, "filename", mono_image_get_filename (image
));
667 mono_image_close (image
);
670 /* Only used when UseShellExecute is false */
672 quote_path (const gchar
*path
)
674 gchar
*res
= g_shell_quote (path
);
675 #ifdef PLATFORM_WIN32
688 /* Only used when UseShellExecute is false */
690 complete_path (const gunichar2
*appname
, gchar
**completed
)
695 utf8app
= g_utf16_to_utf8 (appname
, -1, NULL
, NULL
, NULL
);
696 if (g_path_is_absolute (utf8app
)) {
697 *completed
= quote_path (utf8app
);
702 if (g_file_test (utf8app
, G_FILE_TEST_IS_EXECUTABLE
) && !g_file_test (utf8app
, G_FILE_TEST_IS_DIR
)) {
703 *completed
= quote_path (utf8app
);
708 found
= g_find_program_in_path (utf8app
);
715 *completed
= quote_path (found
);
721 MonoBoolean
ves_icall_System_Diagnostics_Process_Start_internal (MonoString
*appname
, MonoString
*cmd
, MonoString
*dirname
, HANDLE stdin_handle
, HANDLE stdout_handle
, HANDLE stderr_handle
, MonoProcInfo
*process_info
)
725 STARTUPINFO startinfo
={0};
726 PROCESS_INFORMATION procinfo
;
727 gunichar2
*shell_path
= NULL
;
728 gchar
*env_vars
= NULL
;
729 gboolean free_shell_path
= TRUE
;
734 startinfo
.cb
=sizeof(STARTUPINFO
);
735 startinfo
.dwFlags
=STARTF_USESTDHANDLES
;
736 startinfo
.hStdInput
=stdin_handle
;
737 startinfo
.hStdOutput
=stdout_handle
;
738 startinfo
.hStdError
=stderr_handle
;
740 if (process_info
->use_shell
) {
742 const gchar
*shell_args
;
743 #ifdef PLATFORM_WIN32
744 spath
= g_getenv ("COMSPEC");
745 shell_args
= "/c %s";
747 spath
= g_getenv ("SHELL");
748 shell_args
= "-c %s";
754 shell_path
= mono_unicode_from_external (spath
, &dummy
);
755 tmp
= mono_string_to_utf8 (cmd
);
756 quoted
= g_shell_quote (tmp
);
757 #ifdef PLATFORM_WIN32
767 newcmd
= g_strdup_printf (shell_args
, quoted
);
770 cmd
= mono_string_new (mono_domain_get (), newcmd
);
775 shell_path
= mono_string_chars (appname
);
776 complete_path (shell_path
, &spath
);
778 process_info
->pid
= -ERROR_FILE_NOT_FOUND
;
781 #ifdef PLATFORM_WIN32
782 /* Seems like our CreateProcess does not work as the windows one.
783 * This hack is needed to deal with paths containing spaces */
785 free_shell_path
= FALSE
;
786 tmp
= mono_string_to_utf8 (cmd
);
787 newcmd
= g_strdup_printf ("%s %s", spath
, tmp
);
788 cmd
= mono_string_new_wrapper (newcmd
);
792 shell_path
= g_utf8_to_utf16 (spath
, -1, NULL
, NULL
, NULL
);
797 if (process_info
->env_keys
!= NULL
) {
800 MonoString
*key
, *value
;
801 gunichar2
*str
, *ptr
;
804 for (len
= 0, i
= 0; i
< mono_array_length (process_info
->env_keys
); i
++) {
805 ms
= mono_array_get (process_info
->env_values
, MonoString
*, i
);
809 len
+= mono_string_length (ms
) * sizeof (gunichar2
);
810 ms
= mono_array_get (process_info
->env_keys
, MonoString
*, i
);
811 len
+= mono_string_length (ms
) * sizeof (gunichar2
);
812 len
+= 2 * sizeof (gunichar2
);
815 equals16
= g_utf8_to_utf16 ("=", 1, NULL
, NULL
, NULL
);
816 ptr
= str
= g_new0 (gunichar2
, len
+ 1);
817 for (i
= 0; i
< mono_array_length (process_info
->env_keys
); i
++) {
818 value
= mono_array_get (process_info
->env_values
, MonoString
*, i
);
822 key
= mono_array_get (process_info
->env_keys
, MonoString
*, i
);
823 memcpy (ptr
, mono_string_chars (key
), mono_string_length (key
) * sizeof (gunichar2
));
824 ptr
+= mono_string_length (key
);
826 memcpy (ptr
, equals16
, sizeof (gunichar2
));
829 memcpy (ptr
, mono_string_chars (value
), mono_string_length (value
) * sizeof (gunichar2
));
830 ptr
+= mono_string_length (value
);
835 env_vars
= (gchar
*) str
;
838 /* The default dir name is "". Turn that into NULL to mean
839 * "current directory"
841 if(mono_string_length (dirname
)==0) {
844 dir
=mono_string_chars (dirname
);
847 ret
=CreateProcess (shell_path
, mono_string_chars (cmd
), NULL
, NULL
, TRUE
, CREATE_UNICODE_ENVIRONMENT
, env_vars
, dir
, &startinfo
, &procinfo
);
854 process_info
->process_handle
=procinfo
.hProcess
;
855 /*process_info->thread_handle=procinfo.hThread;*/
856 process_info
->thread_handle
=NULL
;
857 CloseHandle(procinfo
.hThread
);
858 process_info
->pid
=procinfo
.dwProcessId
;
859 process_info
->tid
=procinfo
.dwThreadId
;
861 process_info
->pid
= -GetLastError ();
867 MonoBoolean
ves_icall_System_Diagnostics_Process_WaitForExit_internal (MonoObject
*this, HANDLE process
, gint32 ms
)
875 ret
=WaitForSingleObjectEx (process
, INFINITE
, TRUE
);
877 ret
=WaitForSingleObjectEx (process
, ms
, TRUE
);
879 if(ret
==WAIT_OBJECT_0
) {
886 gint64
ves_icall_System_Diagnostics_Process_ExitTime_internal (HANDLE process
)
890 FILETIME create_time
, exit_time
, kernel_time
, user_time
;
894 ret
=GetProcessTimes (process
, &create_time
, &exit_time
, &kernel_time
,
897 ticks
=((guint64
)exit_time
.dwHighDateTime
<< 32) +
898 exit_time
.dwLowDateTime
;
906 gint64
ves_icall_System_Diagnostics_Process_StartTime_internal (HANDLE process
)
910 FILETIME create_time
, exit_time
, kernel_time
, user_time
;
914 ret
=GetProcessTimes (process
, &create_time
, &exit_time
, &kernel_time
,
917 ticks
=((guint64
)create_time
.dwHighDateTime
<< 32) +
918 create_time
.dwLowDateTime
;
926 gint32
ves_icall_System_Diagnostics_Process_ExitCode_internal (HANDLE process
)
932 GetExitCodeProcess (process
, &code
);
935 g_message (G_GNUC_PRETTY_FUNCTION
": process exit code is %d", code
);
941 MonoString
*ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE process
)
946 gunichar2 name
[MAX_PATH
];
952 ok
=EnumProcessModules (process
, &mod
, sizeof(mod
), &needed
);
957 len
=GetModuleBaseName (process
, mod
, name
, sizeof(name
));
963 g_message (G_GNUC_PRETTY_FUNCTION
": process name is [%s]",
964 g_utf16_to_utf8 (name
, -1, NULL
, NULL
, NULL
));
967 string
=mono_string_new_utf16 (mono_domain_get (), name
, len
);
972 /* Returns an array of pids */
973 MonoArray
*ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
977 guint32 needed
, count
, i
;
982 ret
=EnumProcesses (pids
, sizeof(pids
), &needed
);
984 /* FIXME: throw an exception */
988 count
=needed
/sizeof(guint32
);
989 procs
=mono_array_new (mono_domain_get (), mono_get_int32_class (),
991 for(i
=0; i
<count
; i
++) {
992 mono_array_set (procs
, guint32
, i
, pids
[i
]);
998 MonoBoolean
ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process
, guint32
*min
, guint32
*max
)
1001 size_t ws_min
, ws_max
;
1003 MONO_ARCH_SAVE_REGS
;
1005 ret
=GetProcessWorkingSetSize (process
, &ws_min
, &ws_max
);
1006 *min
=(guint32
)ws_min
;
1007 *max
=(guint32
)ws_max
;
1012 MonoBoolean
ves_icall_System_Diagnostics_Process_SetWorkingSet_internal (HANDLE process
, guint32 min
, guint32 max
, MonoBoolean use_min
)
1018 MONO_ARCH_SAVE_REGS
;
1020 ret
=GetProcessWorkingSetSize (process
, &ws_min
, &ws_max
);
1031 ret
=SetProcessWorkingSetSize (process
, ws_min
, ws_max
);
1037 ves_icall_System_Diagnostics_Process_Kill_internal (HANDLE process
, gint32 sig
)
1039 MONO_ARCH_SAVE_REGS
;
1041 /* sig == 1 -> Kill, sig == 2 -> CloseMainWindow */
1043 return TerminateProcess (process
, -sig
);