Win32Exception while getting a process handle?!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tim

    #1

    Win32Exception while getting a process handle?!

    Hey guys,
    I tried to get a process handle with the following code snippet:

    00 Process[] processes = Process.GetProc esses();
    01 if (null != processes && processes.Lengt h 0)
    02 {
    03 foreach (Process currentProcess in processes)
    04 {
    05 if (null != currentProcess)
    06 {
    07 if (currentProcess .Handle !=
    Process.GetCurr entProcess().Ha ndle)
    08 {
    09 if
    (currentProcess .MainModule.Fil eName.ToLower() .Contains("proc essname"))
    10 {
    11 currentProcess. Kill();
    12 }
    13 }
    14 }
    15 }
    16 }

    As soon as I reach line XX the framework will throw a Win32Exception
    with the exception message "Access denied". The application should
    have all needed user privileges because it runs as a service
    application with the SYSTEM account permissions. As of this, I think
    that my application will need additional .NET rights. Has somebody any
    idea how to get the required permission? Thanks a lot.

    Best regards,
    Tim
  • Tim

    #2
    Re: Win32Exception while getting a process handle?!

    It should be line "07" and not line "XX" ;)

    Comment

    • =?Utf-8?B?UGV0ZXIgUml0Y2hpZSBbQyMgTVZQXQ==?=

      #3
      RE: Win32Exception while getting a process handle?!

      What is Process.Process Name for the process you are trying to retrieve the
      handle for?

      If it's a system process, you'll be unlikely to have been granted all access
      to open the process. When Process gets the handle it tries to open the
      process with PROCESS_ALL_ACC ESS, if you've been denied any process permission
      for that particular process you will get an access denied error (via
      Win32Exception) .

      --
      Browse http://connect.microsoft.com/VisualStudio/feedback/ and vote.

      Microsoft MVP, Visual Developer - Visual C#


      "Tim" wrote:
      Hey guys,
      I tried to get a process handle with the following code snippet:
      >
      00 Process[] processes = Process.GetProc esses();
      01 if (null != processes && processes.Lengt h 0)
      02 {
      03 foreach (Process currentProcess in processes)
      04 {
      05 if (null != currentProcess)
      06 {
      07 if (currentProcess .Handle !=
      Process.GetCurr entProcess().Ha ndle)
      08 {
      09 if
      (currentProcess .MainModule.Fil eName.ToLower() .Contains("proc essname"))
      10 {
      11 currentProcess. Kill();
      12 }
      13 }
      14 }
      15 }
      16 }
      >
      As soon as I reach line XX the framework will throw a Win32Exception
      with the exception message "Access denied". The application should
      have all needed user privileges because it runs as a service
      application with the SYSTEM account permissions. As of this, I think
      that my application will need additional .NET rights. Has somebody any
      idea how to get the required permission? Thanks a lot.
      >
      Best regards,
      Tim
      >

      Comment

      Working...