Skip to content

Commit

Permalink
Remove arbitrary 1024 limit in GetProcesses
Browse files Browse the repository at this point in the history
	Reallocate the array of PIDs if EnumProcesses used all of it and try
	again.
  • Loading branch information
gonzalop committed Oct 22, 2010
1 parent 5d586fe commit 827a231
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
7 changes: 3 additions & 4 deletions mcs/class/System/System.Diagnostics/Process.cs
Expand Up @@ -856,11 +856,10 @@ public static Process GetProcessById(int processId)
public static Process[] GetProcesses()
{
int [] pids = GetProcesses_internal ();
ArrayList proclist = new ArrayList ();

if (pids == null)
return new Process [0];


ArrayList proclist = new ArrayList (pids.Length);
for (int i = 0; i < pids.Length; i++) {
try {
proclist.Add (GetProcessById (pids [i]));
Expand Down Expand Up @@ -893,7 +892,7 @@ public static Process[] GetProcessesByName(string processName)
if (pids == null)
return new Process [0];

ArrayList proclist = new ArrayList ();
ArrayList proclist = new ArrayList (pids.Length);
for (int i = 0; i < pids.Length; i++) {
try {
Process p = GetProcessById (pids [i]);
Expand Down
43 changes: 28 additions & 15 deletions mono/metadata/process.c
Expand Up @@ -891,25 +891,38 @@ MonoArray *ves_icall_System_Diagnostics_Process_GetProcesses_internal (void)
MonoArray *procs;
gboolean ret;
DWORD needed;
guint32 count, i;
DWORD pids[1024];
guint32 count;
guint32 *pids;

MONO_ARCH_SAVE_REGS;

ret=EnumProcesses (pids, sizeof(pids), &needed);
if(ret==FALSE) {
/* FIXME: throw an exception */
return(NULL);
}

count=needed/sizeof(DWORD);
procs=mono_array_new (mono_domain_get (), mono_get_int32_class (),
count);
for(i=0; i<count; i++) {
mono_array_set (procs, guint32, i, pids[i]);
}
count = 512;
do {
pids = g_new0 (guint32, count);
ret = EnumProcesses (pids, count * sizeof (guint32), &needed);
if (ret == FALSE) {
MonoException *exc;

g_free (pids);
pids = NULL;
exc = mono_get_exception_not_supported ("This system does not support EnumProcesses");
mono_raise_exception (exc);
g_assert_not_reached ();
}
if (needed < (count * sizeof (guint32)))
break;
g_free (pids);
pids = NULL;
count = (count * 3) / 2;
} while (TRUE);

count = needed / sizeof (guint32);
procs = mono_array_new (mono_domain_get (), mono_get_int32_class (), count);
memcpy (mono_array_addr (procs, guint32, 0), pids, needed);
g_free (pids);
pids = NULL;

return(procs);
return procs;
}

MonoBoolean ves_icall_System_Diagnostics_Process_GetWorkingSet_internal (HANDLE process, guint32 *min, guint32 *max)
Expand Down

0 comments on commit 827a231

Please sign in to comment.