2

When i try to write process' information to console i get System.ArgumentException and System.ComponentModel.Win32Exception. What causes this? How can i stop having those?

        Process processListe = Process.GetProcesses();


            for (int i = 0; i < processListe.Count(); i++)
            {
                try
                {
                string companyName = processListe[i].MainModule.FileVersionInfo.CompanyName;
                string fileVersion = processListe[i].MainModule.FileVersionInfo.FileVersion;

                Console.WriteLine(companyName  + " " + fileVersion);


                }
                catch (Exception) { }


            }

Errors happen in "string companyName = processListe[i].MainModule.FileVersionInfo.CompanyName;" line.

Error messages:

   System.ArgumentException: Illegal characters in path.
   at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at System.Diagnostics.ProcessModule.get_FileVersionInfo()


   A first chance exception of type 'System.ComponentModel.Win32Exception' occurred in System.dll
   System.ComponentModel.Win32Exception (0x80004005): Unable to enumerate the process modules.
   at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
   at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
   at System.Diagnostics.Process.get_MainModule()

Lastly i've made an information output of those process which makes me get errors:

    Exception: Illegal characters in path.
    Proess Name: winlogon Company Name: Aestan Software Version: 1.6.1.33
    Detail: System.ArgumentException: Illegal characters in path.
   at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at System.Diagnostics.ProcessModule.get_FileVersionInfo()

    Exception: Illegal characters in path.
    Proess Name: csrss Company Name: Microsoft Corporation Version: 2009.0100.1600.01 ((KJ_RTM).100402-1540 )
    Detail: System.ArgumentException: Illegal characters in path.
at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
   at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
   at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String[] pathList, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.Path.GetFullPath(String path)
   at System.Diagnostics.FileVersionInfo.GetFullPathWithAssert(String fileName)
   at System.Diagnostics.FileVersionInfo.GetVersionInfo(String fileName)
   at System.Diagnostics.ProcessModule.get_FileVersionInfo()

    Exception: Unable to enumerate the process modules.
    Proess Name: System Company Name: BitTorrent, Inc. Version: 7.5.0.25682
    Detail: System.ComponentModel.Win32Exception (0x80004005): Unable to enumerate the process modules. 
at System.Diagnostics.NtProcessManager.GetModuleInfos(Int32 processId, Boolean firstModuleOnly)
   at System.Diagnostics.NtProcessManager.GetFirstModuleInfo(Int32 processId)
   at System.Diagnostics.Process.get_MainModule()

    Exception: Access is denied
    Proess Name: Cheat Engine Company Name:  Version: 5.6.1.10
    Detail:  System.ComponentModel.Win32Exception (0x80004005): Access is denied
   at System.Diagnostics.ProcessManager.OpenProcess(Int32 processId, Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
   at System.Diagnostics.Process.get_HasExited()
platypus
  • 706
  • 2
  • 18
  • 40
  • 1
    Are you running your application as Administrator or any other user having Administrator rights. Also if you can post which line and full exception ? – Surjit Samra Dec 03 '11 at 18:22
  • I've administrator rights. Added the error message btw. – platypus Dec 03 '11 at 18:36
  • @gcx - can you include the name of the processes (`.ProcessName`) that are giving you those errors? – shf301 Dec 03 '11 at 18:40
  • @shf301 i've added to my original message. – platypus Dec 03 '11 at 18:56
  • @gcx I have no clue why you're getting "Illegal characters in path" exceptions. Maybe try viewing them with Process Explorer and see what that shows you or breaking on those processes in the debugger and seeing what you can see. – shf301 Dec 03 '11 at 20:25

3 Answers3

8

The short answer is that you can't get rid of the exceptions. There are a couple of exceptions I see when I run this code that I don't see explicitly called out in the documentation:

  1. Win32Exception - Access Denied: The process is running as a user and your current user doesn't have rights to access the process. Note even when running as administrator you won't have access to all processes (For example audiodg.exe because of DRM restrictions)
  2. Win32Exception - A 32 bit processes cannot access modules of a 64 bit process
  3. Win32Exception - Unable to enumerate the process modules - I see this occurring on the pseudo processes System and Idle - they aren't real processes (they are place holders for kernel services) and don't have any modules to list.
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
shf301
  • 31,086
  • 2
  • 52
  • 86
  • I have the same issue. I trained second solution and I compile my code as 64 bit application. It did not solve it yet. First one would be my solution, I don't know. Is there a way to have those access of my current user. Even I run my code as administrator, I still get the error. What can I do for first one. – Ahmet DAL Jan 19 '15 at 20:24
4

According to Microsoft, you get ArgumentException if the process exits between the time you called Process.GetProcesses() and the time you access processLite[i].MainModule

Checking processLite[i].HasExited may help, but it is not guaranteed, because there's still enough time for the process to exit before you make the next call.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Just a thought, but should you not make sure that the process is still running when you out the information? I'm thinking that the list might just be references to the process, and when you try and access the properties, it's trying to re-call the process that now doesn't exist.

Martin
  • 2,180
  • 4
  • 21
  • 41