2 * process.c: System.Diagnostics.Process support
5 * Dick Porter (dick@ximian.com)
7 * (C) 2002 Ximian, Inc.
8 * Copyright (c) 2002-2006 Novell, Inc.
16 #include <mono/metadata/object.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/io-layer/io-layer.h>
25 /* FIXME: fix this code to not depend so much on the inetrnals */
26 #include <mono/metadata/class-internals.h>
30 HANDLE
ves_icall_System_Diagnostics_Process_GetProcess_internal (guint32 pid
)
36 /* GetCurrentProcess returns a pseudo-handle, so use
39 handle
=OpenProcess (PROCESS_ALL_ACCESS
, TRUE
, pid
);
42 /* FIXME: Throw an exception */
49 guint32
ves_icall_System_Diagnostics_Process_GetPid_internal (void)
53 return(GetCurrentProcessId ());
56 void ves_icall_System_Diagnostics_Process_Process_free_internal (MonoObject
*this,
62 g_message (G_GNUC_PRETTY_FUNCTION
": Closing process %p, handle %p",
66 CloseHandle (process
);
69 #define STASH_SYS_ASS(this) \
70 if(system_assembly == NULL) { \
71 system_assembly=this->vtable->klass->image; \
74 static MonoImage
*system_assembly
=NULL
;
76 static guint32
unicode_chars (const gunichar2
*str
)
88 static guint32
unicode_bytes (const gunichar2
*str
)
94 /* Include the terminators */
102 unicode_get (const gunichar2
*str
)
107 len
= unicode_bytes (str
);
108 swapped
= g_malloc0 (len
);
111 swapped
[i
] = GUINT16_FROM_LE (str
[i
]);
119 * compare a little-endian null-terminated utf16 string and a normal string.
120 * Can be used only for ascii or latin1 chars.
123 unicode_string_equals (const gunichar2
*str1
, const gchar
*str2
)
125 while (*str1
&& *str2
) {
126 if (GUINT16_TO_LE (*str1
) != *str2
)
131 return *str1
== *str2
;
134 static void process_set_field_object (MonoObject
*obj
, const gchar
*fieldname
,
137 MonoClassField
*field
;
140 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to object at %p",
144 field
=mono_class_get_field_from_name (mono_object_class (obj
),
146 /* FIXME: moving GC */
147 *(MonoObject
**)(((char *)obj
) + field
->offset
)=data
;
150 static void process_set_field_string (MonoObject
*obj
, const gchar
*fieldname
,
151 const gunichar2
*val
, guint32 len
)
153 MonoClassField
*field
;
157 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to [%s]",
158 fieldname
, g_utf16_to_utf8 (val
, len
, NULL
, NULL
, NULL
));
161 string
=mono_string_new_utf16 (mono_object_domain (obj
), val
, len
);
163 field
=mono_class_get_field_from_name (mono_object_class (obj
),
165 /* FIXME: moving GC */
166 *(MonoString
**)(((char *)obj
) + field
->offset
)=string
;
169 static void process_set_field_string_utf8 (MonoObject
*obj
,
170 const gchar
*fieldname
,
173 MonoClassField
*field
;
177 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to [%s]",
181 string
=mono_string_new (mono_object_domain (obj
), val
);
183 field
=mono_class_get_field_from_name (mono_object_class (obj
),
185 /* FIXME: moving GC */
186 *(MonoString
**)(((char *)obj
) + field
->offset
)=string
;
189 static void process_set_field_int (MonoObject
*obj
, const gchar
*fieldname
,
192 MonoClassField
*field
;
195 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to %d",
199 field
=mono_class_get_field_from_name (mono_object_class (obj
),
201 *(guint32
*)(((char *)obj
) + field
->offset
)=val
;
204 static void process_set_field_bool (MonoObject
*obj
, const gchar
*fieldname
,
207 MonoClassField
*field
;
210 g_message (G_GNUC_PRETTY_FUNCTION
": Setting field %s to %s",
211 fieldname
, val
?"TRUE":"FALSE");
214 field
=mono_class_get_field_from_name (mono_object_class (obj
),
216 *(guint8
*)(((char *)obj
) + field
->offset
)=val
;
226 /* Returns a pointer to the value data, because theres no way to know
227 * how big that data is (value_len is set to zero for most blocks :-()
229 static gpointer
process_get_versioninfo_block (gpointer data
,
232 block
->data_len
=GUINT16_TO_LE (*((guint16
*)data
));
233 data
= (char *)data
+ sizeof(guint16
);
234 block
->value_len
=GUINT16_TO_LE (*((guint16
*)data
));
235 data
= (char *)data
+ sizeof(guint16
);
237 /* No idea what the type is supposed to indicate */
238 block
->type
=GUINT16_TO_LE (*((guint16
*)data
));
239 data
= (char *)data
+ sizeof(guint16
);
240 block
->key
=((gunichar2
*)data
);
242 /* skip over the key (including the terminator) */
243 data
=((gunichar2
*)data
)+(unicode_chars (block
->key
)+1);
245 /* align on a 32-bit boundary */
246 data
=(gpointer
)((char *)data
+ 3);
247 data
=(gpointer
)((char *)data
- (GPOINTER_TO_INT(data
) & 3));
252 /* Returns a pointer to the byte following the Var block */
253 static gpointer
process_read_var_block (MonoObject
*filever
, gpointer data_ptr
,
256 /* Not currently interested in the VarFileInfo block. This
257 * might change if language support is needed for file version
258 * strings (VarFileInfo contains lists of supported
263 /* data_ptr is pointing at a Var block of length data_len */
264 data_ptr
=process_get_versioninfo_block (data_ptr
, &block
);
265 data_ptr
=((guchar
*)data_ptr
)+block
.value_len
;
270 /* Returns a pointer to the byte following the String block, or NULL
271 * if the data read hits padding. We can't recover from this because
272 * the data length does not include padding bytes, so it's not
273 * possible to just return the start position + length.
275 static gpointer
process_read_string_block (MonoObject
*filever
,
281 guint16 string_len
=28; /* Length of the StringTable block */
283 /* data_ptr is pointing at an array of one or more String
284 * blocks with total length (not including alignment padding)
287 while(string_len
<data_len
) {
290 /* align on a 32-bit boundary */
291 data_ptr
=(gpointer
)((char *)data_ptr
+ 3);
292 data_ptr
=(gpointer
)((char *)data_ptr
-
293 (GPOINTER_TO_INT(data_ptr
) & 3));
295 data_ptr
=process_get_versioninfo_block (data_ptr
, &block
);
296 if(block
.data_len
==0) {
297 /* We must have hit padding, so give up
301 g_message (G_GNUC_PRETTY_FUNCTION
302 ": Hit 0-length block, giving up");
307 string_len
=string_len
+block
.data_len
;
308 value
=unicode_get ((gunichar2
*)data_ptr
);
309 /* Skip over the value */
310 data_ptr
=((gunichar2
*)data_ptr
)+block
.value_len
;
313 if (unicode_string_equals (block
.key
, "Comments")) {
314 process_set_field_string (filever
, "comments", value
, unicode_chars (value
));
315 } else if (unicode_string_equals (block
.key
, "CompanyName")) {
316 process_set_field_string (filever
, "companyname", value
, unicode_chars (value
));
317 } else if (unicode_string_equals (block
.key
, "FileDescription")) {
318 process_set_field_string (filever
, "filedescription", value
, unicode_chars (value
));
319 } else if (unicode_string_equals (block
.key
, "FileVersion")) {
320 process_set_field_string (filever
, "fileversion", value
, unicode_chars (value
));
321 } else if (unicode_string_equals (block
.key
, "InternalName")) {
322 process_set_field_string (filever
, "internalname", value
, unicode_chars (value
));
323 } else if (unicode_string_equals (block
.key
, "LegalCopyright")) {
324 process_set_field_string (filever
, "legalcopyright", value
, unicode_chars (value
));
325 } else if (unicode_string_equals (block
.key
, "LegalTrademarks")) {
326 process_set_field_string (filever
, "legaltrademarks", value
, unicode_chars (value
));
327 } else if (unicode_string_equals (block
.key
, "OriginalFilename")) {
328 process_set_field_string (filever
, "originalfilename", value
, unicode_chars (value
));
329 } else if (unicode_string_equals (block
.key
, "PrivateBuild")) {
330 process_set_field_string (filever
, "privatebuild", value
, unicode_chars (value
));
331 } else if (unicode_string_equals (block
.key
, "ProductName")) {
332 process_set_field_string (filever
, "productname", value
, unicode_chars (value
));
333 } else if (unicode_string_equals (block
.key
, "ProductVersion")) {
334 process_set_field_string (filever
, "productversion", value
, unicode_chars (value
));
335 } else if (unicode_string_equals (block
.key
, "SpecialBuild")) {
336 process_set_field_string (filever
, "specialbuild", value
, unicode_chars (value
));
338 /* Not an error, just not interesting
349 /* returns a pointer to the byte following the Stringtable block, or
350 * NULL if the data read hits padding. We can't recover from this
351 * because the data length does not include padding bytes, so it's not
352 * possible to just return the start position + length
354 static gpointer
process_read_stringtable_block (MonoObject
*filever
,
361 guint16 string_len
=36; /* length of the StringFileInfo block */
363 /* data_ptr is pointing at an array of StringTable blocks,
364 * with total length (not including alignment padding) of
368 while(string_len
<data_len
) {
369 /* align on a 32-bit boundary */
370 data_ptr
=(gpointer
)((char *)data_ptr
+ 3);
371 data_ptr
=(gpointer
)((char *)data_ptr
-
372 (GPOINTER_TO_INT(data_ptr
) & 3));
374 data_ptr
=process_get_versioninfo_block (data_ptr
, &block
);
375 if(block
.data_len
==0) {
376 /* We must have hit padding, so give up
380 g_message (G_GNUC_PRETTY_FUNCTION
381 ": Hit 0-length block, giving up");
385 string_len
=string_len
+block
.data_len
;
387 value
= unicode_get (block
.key
);
388 language
= g_utf16_to_utf8 (value
, unicode_bytes (block
.key
), NULL
, NULL
, NULL
);
389 g_strdown (language
);
391 /* Kludge: treat en_US as neutral too */
392 if (!strcmp (language
, "007f04b0") ||
393 !strcmp (language
, "000004b0") ||
394 !strcmp (language
, "040904b0")) {
395 /* Got the one we're interested in */
396 process_set_field_string_utf8 (filever
, "language",
399 data_ptr
=process_read_string_block (filever
, data_ptr
,
403 /* Some other language. We might want to do
404 * something with this in the future.
406 data_ptr
=process_read_string_block (filever
, data_ptr
,
414 /* Child block hit padding */
416 g_message (G_GNUC_PRETTY_FUNCTION
": Child block hit 0-length block, giving up");
425 static void process_read_fixedfileinfo_block (MonoObject
*filever
,
426 VS_FIXEDFILEINFO
*ffi
)
429 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
);
432 process_set_field_int (filever
, "filemajorpart",
433 HIWORD (ffi
->dwFileVersionMS
));
434 process_set_field_int (filever
, "fileminorpart",
435 LOWORD (ffi
->dwFileVersionMS
));
436 process_set_field_int (filever
, "filebuildpart",
437 HIWORD (ffi
->dwFileVersionLS
));
438 process_set_field_int (filever
, "fileprivatepart",
439 LOWORD (ffi
->dwFileVersionLS
));
441 process_set_field_int (filever
, "productmajorpart",
442 HIWORD (ffi
->dwProductVersionMS
));
443 process_set_field_int (filever
, "productminorpart",
444 LOWORD (ffi
->dwProductVersionMS
));
445 process_set_field_int (filever
, "productbuildpart",
446 HIWORD (ffi
->dwProductVersionLS
));
447 process_set_field_int (filever
, "productprivatepart",
448 LOWORD (ffi
->dwProductVersionLS
));
450 process_set_field_bool (filever
, "isdebug",
451 ffi
->dwFileFlags
&VS_FF_DEBUG
);
452 process_set_field_bool (filever
, "isprerelease",
453 ffi
->dwFileFlags
&VS_FF_PRERELEASE
);
454 process_set_field_bool (filever
, "ispatched",
455 ffi
->dwFileFlags
&VS_FF_PATCHED
);
456 process_set_field_bool (filever
, "isprivatebuild",
457 ffi
->dwFileFlags
&VS_FF_PRIVATEBUILD
);
458 process_set_field_bool (filever
, "isspecialbuild",
459 ffi
->dwFileFlags
&VS_FF_SPECIALBUILD
);
462 static void process_get_fileversion (MonoObject
*filever
, MonoImage
*image
)
464 MonoPEResourceDataEntry
*version_info
;
466 VS_FIXEDFILEINFO
*ffi
;
469 gint32 data_len
; /* signed to guard against underflow */
471 version_info
=mono_image_lookup_resource (image
,
472 MONO_PE_RESOURCE_ID_VERSION
,
475 g_message (G_GNUC_PRETTY_FUNCTION
": image_lookup returned %p",
479 if(version_info
==NULL
) {
483 data
=mono_image_rva_map (image
,
484 version_info
->rde_data_offset
);
485 g_free (version_info
);
490 /* See io-layer/versioninfo.h for the gory details on how this
491 * data is laid out. (data should be pointing to
492 * VS_VERSIONINFO data).
495 data_ptr
=process_get_versioninfo_block (data
, &block
);
497 data_len
=block
.data_len
;
499 if(block
.value_len
!=sizeof(VS_FIXEDFILEINFO
)) {
501 g_message (G_GNUC_PRETTY_FUNCTION
502 ": FIXEDFILEINFO size mismatch");
507 if (!unicode_string_equals (block
.key
, "VS_VERSION_INFO")) {
509 g_message (G_GNUC_PRETTY_FUNCTION
510 ": VS_VERSION_INFO mismatch");
515 ffi
=((VS_FIXEDFILEINFO
*)data_ptr
);
516 data_ptr
= (char *)data_ptr
+ sizeof(VS_FIXEDFILEINFO
);
517 if((ffi
->dwSignature
!=VS_FFI_SIGNATURE
) ||
518 (ffi
->dwStrucVersion
!=VS_FFI_STRUCVERSION
)) {
520 g_message (G_GNUC_PRETTY_FUNCTION
521 ": FIXEDFILEINFO bad signature");
525 process_read_fixedfileinfo_block (filever
, ffi
);
527 /* Subtract the 92 bytes we've already seen */
530 /* There now follow zero or one StringFileInfo blocks and zero
531 * or one VarFileInfo blocks
533 while(data_len
> 0) {
534 /* align on a 32-bit boundary */
535 data_ptr
=(gpointer
)((char *)data_ptr
+ 3);
536 data_ptr
=(gpointer
)((char *)data_ptr
-
537 (GPOINTER_TO_INT(data_ptr
) & 3));
539 data_ptr
=process_get_versioninfo_block (data_ptr
, &block
);
540 if(block
.data_len
==0) {
541 /* We must have hit padding, so give up
545 g_message (G_GNUC_PRETTY_FUNCTION
546 ": Hit 0-length block, giving up");
551 data_len
=data_len
-block
.data_len
;
553 if (unicode_string_equals (block
.key
, "VarFileInfo")) {
554 data_ptr
=process_read_var_block (filever
, data_ptr
,
556 } else if (unicode_string_equals (block
.key
, "StringFileInfo")) {
557 data_ptr
=process_read_stringtable_block (filever
, data_ptr
, block
.data_len
);
561 g_message (G_GNUC_PRETTY_FUNCTION
562 ": Not a valid VERSIONINFO child block");
568 /* Child block hit padding */
570 g_message (G_GNUC_PRETTY_FUNCTION
": Child block hit 0-length block, giving up");
577 static void process_add_module (GPtrArray
*modules
, MonoAssembly
*ass
)
579 MonoClass
*proc_class
, *filever_class
;
580 MonoObject
*item
, *filever
;
581 MonoDomain
*domain
=mono_domain_get ();
583 const char* filename
;
585 /* Build a System.Diagnostics.ProcessModule with the data.
586 * Leave BaseAddress and EntryPointAddress set to NULL,
587 * FileName is ass->image->name, FileVersionInfo is an object
588 * constructed from the PE image data referenced by
589 * ass->image, ModuleMemorySize set to 0, ModuleName the last
590 * component of FileName.
592 proc_class
=mono_class_from_name (system_assembly
, "System.Diagnostics",
594 item
=mono_object_new (domain
, proc_class
);
596 filever_class
=mono_class_from_name (system_assembly
,
597 "System.Diagnostics",
599 filever
=mono_object_new (domain
, filever_class
);
602 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
);
605 process_get_fileversion (filever
, mono_assembly_get_image (ass
));
607 filename
= mono_image_get_filename (mono_assembly_get_image (ass
));
608 process_set_field_string_utf8 (filever
, "filename", filename
);
609 process_set_field_string_utf8 (item
, "filename", filename
);
610 process_set_field_object (item
, "version_info", filever
);
612 modulename
=g_path_get_basename (filename
);
613 process_set_field_string_utf8 (item
, "modulename", modulename
);
616 /* FIXME: moving GC */
617 g_ptr_array_add (modules
, item
);
620 static void process_scan_modules (gpointer data
, gpointer user_data
)
622 MonoAssembly
*ass
=data
;
623 GPtrArray
*modules
=user_data
;
625 /* The main assembly is already in the list */
626 if(mono_assembly_get_main () != ass
) {
627 process_add_module (modules
, ass
);
632 /* Returns an array of System.Diagnostics.ProcessModule */
633 MonoArray
*ves_icall_System_Diagnostics_Process_GetModules_internal (MonoObject
*this)
635 /* I was going to use toolhelp for this, but then realised I
636 * was being an idiot :)
638 * (Toolhelp would give shared libraries open by the runtime,
639 * as well as open assemblies. On windows my tests didnt find
640 * the assemblies loaded by mono either.)
642 GPtrArray
*modules_list
=g_ptr_array_new ();
648 STASH_SYS_ASS (this);
650 /* Make sure the first entry is the main module */
651 process_add_module (modules_list
, mono_assembly_get_main ());
653 mono_assembly_foreach (process_scan_modules
, modules_list
);
655 /* Build a MonoArray out of modules_list */
656 arr
=mono_array_new (mono_domain_get (), mono_get_object_class (),
659 for(i
=0; i
<modules_list
->len
; i
++) {
660 mono_array_setref (arr
, i
, g_ptr_array_index (modules_list
, i
));
663 g_ptr_array_free (modules_list
, TRUE
);
668 void ves_icall_System_Diagnostics_FileVersionInfo_GetVersionInfo_internal (MonoObject
*this, MonoString
*filename
)
671 gchar
*filename_utf8
;
675 STASH_SYS_ASS (this);
677 filename_utf8
= mono_string_to_utf8 (filename
);
678 image
= mono_pe_file_open (filename_utf8
, NULL
);
679 g_free (filename_utf8
);
682 /* FIXME: an exception might be appropriate here */
684 g_message (G_GNUC_PRETTY_FUNCTION
": Failed to load image");
690 process_get_fileversion (this, image
);
691 process_set_field_string_utf8 (this, "filename", mono_image_get_filename (image
));
693 mono_image_close (image
);
696 /* Only used when UseShellExecute is false */
698 quote_path (const gchar
*path
)
700 gchar
*res
= g_shell_quote (path
);
701 #ifdef PLATFORM_WIN32
714 /* Only used when UseShellExecute is false */
716 complete_path (const gunichar2
*appname
, gchar
**completed
)
721 utf8app
= g_utf16_to_utf8 (appname
, -1, NULL
, NULL
, NULL
);
722 if (g_path_is_absolute (utf8app
)) {
723 *completed
= quote_path (utf8app
);
728 if (g_file_test (utf8app
, G_FILE_TEST_IS_EXECUTABLE
) && !g_file_test (utf8app
, G_FILE_TEST_IS_DIR
)) {
729 *completed
= quote_path (utf8app
);
734 found
= g_find_program_in_path (utf8app
);
741 *completed
= quote_path (found
);
747 MonoBoolean
ves_icall_System_Diagnostics_Process_ShellExecuteEx_internal (MonoProcessStartInfo
*proc_start_info
, MonoProcInfo
*process_info
)
749 SHELLEXECUTEINFO shellex
= {0};
752 shellex
.cbSize
= sizeof(SHELLEXECUTEINFO
);
753 shellex
.fMask
= SEE_MASK_FLAG_DDEWAIT
| SEE_MASK_NOCLOSEPROCESS
| SEE_MASK_UNICODE
;
754 shellex
.nShow
= SW_SHOWNORMAL
;
756 if (proc_start_info
->filename
!= NULL
) {
757 shellex
.lpFile
= mono_string_chars (proc_start_info
->filename
);
760 if (proc_start_info
->arguments
!= NULL
) {
761 shellex
.lpParameters
= mono_string_chars (proc_start_info
->arguments
);
764 if (proc_start_info
->verb
!= NULL
&&
765 mono_string_length (proc_start_info
->verb
) != 0) {
766 shellex
.lpVerb
= mono_string_chars (proc_start_info
->verb
);
769 if (proc_start_info
->working_directory
!= NULL
&&
770 mono_string_length (proc_start_info
->working_directory
) != 0) {
771 shellex
.lpDirectory
= mono_string_chars (proc_start_info
->working_directory
);
774 ret
= ShellExecuteEx (&shellex
);
776 process_info
->pid
= -GetLastError ();
778 process_info
->process_handle
= shellex
.hProcess
;
779 process_info
->thread_handle
= NULL
;
780 /* It appears that there's no way to get the pid from a
781 * process handle before windows xp. Really.
783 #ifdef HAVE_GETPROCESSID
784 process_info
->pid
= GetProcessId (shellex
.hProcess
);
786 process_info
->pid
= 0;
788 process_info
->tid
= 0;
794 MonoBoolean
ves_icall_System_Diagnostics_Process_CreateProcess_internal (MonoProcessStartInfo
*proc_start_info
, HANDLE stdin_handle
, HANDLE stdout_handle
, HANDLE stderr_handle
, MonoProcInfo
*process_info
)
798 STARTUPINFO startinfo
={0};
799 PROCESS_INFORMATION procinfo
;
800 gunichar2
*shell_path
= NULL
;
801 gchar
*env_vars
= NULL
;
802 gboolean free_shell_path
= TRUE
;
804 MonoString
*cmd
= proc_start_info
->arguments
;
806 startinfo
.cb
=sizeof(STARTUPINFO
);
807 startinfo
.dwFlags
=STARTF_USESTDHANDLES
;
808 startinfo
.hStdInput
=stdin_handle
;
809 startinfo
.hStdOutput
=stdout_handle
;
810 startinfo
.hStdError
=stderr_handle
;
812 shell_path
= mono_string_chars (proc_start_info
->filename
);
813 complete_path (shell_path
, &spath
);
815 process_info
->pid
= -ERROR_FILE_NOT_FOUND
;
818 #ifdef PLATFORM_WIN32
819 /* Seems like our CreateProcess does not work as the windows one.
820 * This hack is needed to deal with paths containing spaces */
822 free_shell_path
= FALSE
;
825 tmp
= mono_string_to_utf8 (cmd
);
826 newcmd
= g_strdup_printf ("%s %s", spath
, tmp
);
827 cmd
= mono_string_new_wrapper (newcmd
);
832 cmd
= mono_string_new_wrapper (spath
);
835 shell_path
= g_utf8_to_utf16 (spath
, -1, NULL
, NULL
, NULL
);
839 if (process_info
->env_keys
!= NULL
) {
842 MonoString
*key
, *value
;
843 gunichar2
*str
, *ptr
;
846 for (len
= 0, i
= 0; i
< mono_array_length (process_info
->env_keys
); i
++) {
847 ms
= mono_array_get (process_info
->env_values
, MonoString
*, i
);
851 len
+= mono_string_length (ms
) * sizeof (gunichar2
);
852 ms
= mono_array_get (process_info
->env_keys
, MonoString
*, i
);
853 len
+= mono_string_length (ms
) * sizeof (gunichar2
);
854 len
+= 2 * sizeof (gunichar2
);
857 equals16
= g_utf8_to_utf16 ("=", 1, NULL
, NULL
, NULL
);
858 ptr
= str
= g_new0 (gunichar2
, len
+ 1);
859 for (i
= 0; i
< mono_array_length (process_info
->env_keys
); i
++) {
860 value
= mono_array_get (process_info
->env_values
, MonoString
*, i
);
864 key
= mono_array_get (process_info
->env_keys
, MonoString
*, i
);
865 memcpy (ptr
, mono_string_chars (key
), mono_string_length (key
) * sizeof (gunichar2
));
866 ptr
+= mono_string_length (key
);
868 memcpy (ptr
, equals16
, sizeof (gunichar2
));
871 memcpy (ptr
, mono_string_chars (value
), mono_string_length (value
) * sizeof (gunichar2
));
872 ptr
+= mono_string_length (value
);
877 env_vars
= (gchar
*) str
;
880 /* The default dir name is "". Turn that into NULL to mean
881 * "current directory"
883 if(mono_string_length (proc_start_info
->working_directory
)==0) {
886 dir
=mono_string_chars (proc_start_info
->working_directory
);
889 ret
=CreateProcess (shell_path
, cmd
? mono_string_chars (cmd
): NULL
, NULL
, NULL
, TRUE
, CREATE_UNICODE_ENVIRONMENT
, env_vars
, dir
, &startinfo
, &procinfo
);
896 process_info
->process_handle
=procinfo
.hProcess
;
897 /*process_info->thread_handle=procinfo.hThread;*/
898 process_info
->thread_handle
=NULL
;
899 if (procinfo
.hThread
!= NULL
&& procinfo
.hThread
!= INVALID_HANDLE_VALUE
)
900 CloseHandle(procinfo
.hThread
);
901 process_info
->pid
=procinfo
.dwProcessId
;
902 process_info
->tid
=procinfo
.dwThreadId
;
904 process_info
->pid
= -GetLastError ();
910 MonoBoolean
ves_icall_System_Diagnostics_Process_WaitForExit_internal (MonoObject
*this, HANDLE process
, gint32 ms
)
918 ret
=WaitForSingleObjectEx (process
, INFINITE
, TRUE
);
920 ret
=WaitForSingleObjectEx (process
, ms
, TRUE
);
922 if(ret
==WAIT_OBJECT_0
) {
929 gint64
ves_icall_System_Diagnostics_Process_ExitTime_internal (HANDLE process
)
933 FILETIME create_time
, exit_time
, kernel_time
, user_time
;
937 ret
=GetProcessTimes (process
, &create_time
, &exit_time
, &kernel_time
,
940 ticks
=((guint64
)exit_time
.dwHighDateTime
<< 32) +
941 exit_time
.dwLowDateTime
;
949 gint64
ves_icall_System_Diagnostics_Process_StartTime_internal (HANDLE process
)
953 FILETIME create_time
, exit_time
, kernel_time
, user_time
;
957 ret
=GetProcessTimes (process
, &create_time
, &exit_time
, &kernel_time
,
960 ticks
=((guint64
)create_time
.dwHighDateTime
<< 32) +
961 create_time
.dwLowDateTime
;
969 gint32
ves_icall_System_Diagnostics_Process_ExitCode_internal (HANDLE process
)
975 GetExitCodeProcess (process
, &code
);
978 g_message (G_GNUC_PRETTY_FUNCTION
": process exit code is %d", code
);
984 MonoString
*ves_icall_System_Diagnostics_Process_ProcessName_internal (HANDLE process
)
989 gunichar2 name
[MAX_PATH
];
995 ok
=EnumProcessModules (process
, &mod
, sizeof(mod
), &needed
);
1000 len
=GetModuleBaseName (process
, mod
, name
, MAX_PATH
);
1006 g_message (G_GNUC_PRETTY_FUNCTION
": process name is [%s]",
1007 g_utf16_to_utf8 (name
, -1, NULL
, NULL
, NULL
));
1010 string
=mono_string_new_utf16 (mono_domain_get (), name
, len
);
1015 /* Returns an array of pids */
1016 MonoArray
*ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
1020 guint32 needed
, count
, i
;
1023 MONO_ARCH_SAVE_REGS
;
1025 ret
=EnumProcesses (pids
, sizeof(pids
), &needed
);
1027 /* FIXME: throw an exception */
1031 count
=needed
/sizeof(guint32
);
1032 procs
=mono_array_new (mono_domain_get (), mono_get_int32_class (),
1034 for(i
=0; i
<count
; i
++) {
1035 mono_array_set (procs
, guint32
, i
, pids
[i
]);
1041 MonoBoolean
ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process
, guint32
*min
, guint32
*max
)
1044 size_t ws_min
, ws_max
;
1046 MONO_ARCH_SAVE_REGS
;
1048 ret
=GetProcessWorkingSetSize (process
, &ws_min
, &ws_max
);
1049 *min
=(guint32
)ws_min
;
1050 *max
=(guint32
)ws_max
;
1055 MonoBoolean
ves_icall_System_Diagnostics_Process_SetWorkingSet_internal (HANDLE process
, guint32 min
, guint32 max
, MonoBoolean use_min
)
1061 MONO_ARCH_SAVE_REGS
;
1063 ret
=GetProcessWorkingSetSize (process
, &ws_min
, &ws_max
);
1074 ret
=SetProcessWorkingSetSize (process
, ws_min
, ws_max
);
1080 ves_icall_System_Diagnostics_Process_Kill_internal (HANDLE process
, gint32 sig
)
1082 MONO_ARCH_SAVE_REGS
;
1084 /* sig == 1 -> Kill, sig == 2 -> CloseMainWindow */
1086 return TerminateProcess (process
, -sig
);