Process Module  
Author Message
Kaiser28





PostPosted: Visual C# General, Process Module Top

Hi,

I have an application which lists all the running processes of the machine and i need to get the full path from which the process is running.

I am trying to use the following methods : Process.MainModule.Filename

Process.MainModule.ModuleName

The above code doesn't work it throws an exception "Unable to enumerate the process modules"

any thoughts

Regards

Alan



Visual C#3  
 
 
SvenC





PostPosted: Visual C# General, Process Module Top

Hi Kaiser28,

I guess this is a security issue. Make sure that you:

- run the app with full trust
- test the app as local admin // you might need debugging privileges to access other processes
- run as admin if running in Vista with UAC enabled

There might still be situations where you are not allowed to "look" at individual system processes from your user login, so you also need to catch security exceptions when you try to access process information.

--
SvenC


 
 
ahmedilyas





PostPosted: Visual C# General, Process Module Top

I tried this also - its not a problem with full trust, or with Vista - im still also getting the same error too!

running .NET 2.0 on WS2003, patched up etc... on admin account.

Thought I would give the heads up!

 

to me, as pretty much hinted/suggested, could be the last process in the list it won't allow you to access it...could may well be that as this is what I am seeing. I have 64 processes which its picked up but will only let me see up to 62-63 processes before throwing the exception

 

 



 
 
Kaiser28





PostPosted: Visual C# General, Process Module Top

thx for the replies.

I am running .Net v2.0 on WinXp prof serv pack 2 and i am also on the admin account i also tried to catch exceptions but still a messagebox popped up with the same message.

If anyone finds a solution please reply in my thread. thx very much :)

Regards

Alan


 
 
ahmedilyas





PostPosted: Visual C# General, Process Module Top

I could be wrong but here is my understanding:

I would lean towards agreeing with SvenC about the problem - that you are not able to access some processes due to the security at some level causing the exception.

I think the thing you can do is catch the COMException and move on although this would be bad practice as the exception could be anything. I would think that the last 1-2 processes (maybe a couple more) will throw this error - again, this is just my understanding and I believe in it more and more as I step through the de****/execute code. So a temp but not a perfect solution would be something like this:

 



Process[] theProcesses = Process.GetProcesses();
int errors = 0;
 
foreach(Process currentProcess in theProcesses)
{
   try
   {
      MessageBox.Show(currentProcess.MainModule.FileName); //example to show the filename
   }
   catch (COMException ex)
   {
      'handle it then move on, or just leave and move on
      errors++;
   }
}
 
MessageBox.Show("# of errors: " + errors.ToString());

 



 
 
guilhermecvm





PostPosted: Visual C# General, Process Module Top

what about show it icon in the listbox

 
 
cablehead





PostPosted: Visual C# General, Process Module Top

private static extern UInt32 GetWindowThreadProcessId(

Int32 hWnd,

out Int32 lpdwProcessId

private Int32 GetWindowProcessID(Int32 hwnd)

{

Int32 pid = 1;

GetWindowThreadProcessId(hwnd, out pid);

return pid;

}

//Pass the hwnd

string appProcessName = Process.GetProcessById(GetWindowProcessID(hwnd)).ProcessName;

string appExePath = Process.GetProcessById(GetWindowProcessID(hwnd)).MainModule.FileName;



 
 
JonPetitta





PostPosted: Visual C# General, Process Module Top

Just as a note on this, it throws an exception while trying to get the path for System and Idle, you just need to filter these out.