Current Process Handle


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Current Process Handle

  1. #1
    Jason Raneses Guest

    Current Process Handle

    Is anyone familiar with the best way to get a handle to the current process?
    I have tried using Process.GetCurrentProcess() which returns me a process
    object, however when using its Handle property an exception is thrown which
    states "The Handle property is only available when you start a
    process". How can I get the handle then? The current process is obviously
    already started. I can acess other things like the process id, process
    name, etc.

    Thanks in advance,

    Jason



  2. #2
    Mattias Sjögren Guest

    Re: Current Process Handle

    Jason,

    >Is anyone familiar with the best way to get a handle to the current process?


    How about calling OpenProcess with PInvoke?

    [DllImport("kernel32.dll")]
    static extern int OpenProcess(int dwDesiredAccess, int bInheritHandle,
    int dwProcessId);

    And don't forget to close the handle using CloseHandle when you're
    done.


    Matt

    ====================================
    Mattias Sjögren - mattias @ mvps.org
    http://www.msjogren.net/dotnet/

    CodeHound - The Software Developer's Search Engine
    http://www.codehound.com

  3. #3
    Jason Raneses Guest

    Re: Current Process Handle

    Thanks for the response. I was really looking for a way to get a handle to
    the application that is currently running. For example, this is a C# .NET
    WinForm application...I need a handle to it so I can pass that handle to
    OpenProcessToken, as well as to a few other Win32 API functions I am
    calling.

    Jason

    "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
    news:3a64b1c4.115922758@news.devx.com...
    > Jason,
    >
    > >Is anyone familiar with the best way to get a handle to the current

    process?
    >
    > How about calling OpenProcess with PInvoke?
    >
    > [DllImport("kernel32.dll")]
    > static extern int OpenProcess(int dwDesiredAccess, int bInheritHandle,
    > int dwProcessId);
    >
    > And don't forget to close the handle using CloseHandle when you're
    > done.
    >
    >
    > Matt
    >
    > ====================================
    > Mattias Sjögren - mattias @ mvps.org
    > http://www.msjogren.net/dotnet/
    >
    > CodeHound - The Software Developer's Search Engine
    > http://www.codehound.com




  4. #4
    Mattias Sjögren Guest

    Re: Current Process Handle

    Hi Jason,

    >I was really looking for a way to get a handle to the application that is currently running.


    And that's exactly what you'll get from OpenProcess if you pass it the
    process ID of your app, given by Process.GetCurrentProcess().Id.

    Another way is to call the GetCurrentProcess API.

    [DllImport("kernel32.dll")]
    static extern int GetCurrentProcess();


    Matt

    ====================================
    Mattias Sjögren - mattias @ mvps.org

  5. #5
    Jason Raneses Guest

    Re: Current Process Handle

    Ok, OpenProcess worked and returned a valid handle, but GetCurrentProcess
    returns -1.

    ---

    [DllImport("kernel32.dll")]
    public static extern int GetCurrentProcess();

    [DllImport("kernel32.dll")]
    public static extern int OpenProcess(int desiredAccess, bool inheritHandle,
    int processID);

    int SYNCHRONIZE = 0x00100000;
    int PROCESS_TERMINATE = 0x0001;
    int PROCESS_CREATE_THREAD = 0x0002;
    int PROCESS_SET_SESSIONID = 0x0004;
    int PROCESS_VM_OPERATION = 0x0008;
    int PROCESS_VM_READ = 0x0010;
    int PROCESS_VM_WRITE = 0x0020;
    int PROCESS_DUP_HANDLE = 0x0040;
    int PROCESS_CREATE_PROCESS = 0x0080;
    int PROCESS_SET_QUOTA = 0x0100;
    int PROCESS_SET_INFORMATION = 0x0200;
    int PROCESS_QUERY_INFORMATION = 0x0400;
    int PROCESS_SUSPEND_RESUME = 0x0800;
    int PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF;

    Process process = Process.GetCurrentProcess();

    int processID1 = 0;

    processID1 = OpenProcess(PROCESS_ALL_ACCESS, true, process.Id);

    Console.WriteLine("processID1 from Win32 is " + processID1.ToString());

    int processID2 = 0;

    processID2 = GetCurrentProcess();

    Console.WriteLine("processID2 from Win32 is " + processID2.ToString());

    ---

    Returns:

    processID1 from Win32 is 612
    processID2 from Win32 is -1

    I can't figure out why the second, and of course easier call, is
    returning -1. Any ideas?

    Jason

    "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
    news:3a64baee.118268320@news.devx.com...
    > Hi Jason,
    >
    > >I was really looking for a way to get a handle to the application that is

    currently running.
    >
    > And that's exactly what you'll get from OpenProcess if you pass it the
    > process ID of your app, given by Process.GetCurrentProcess().Id.
    >
    > Another way is to call the GetCurrentProcess API.
    >
    > [DllImport("kernel32.dll")]
    > static extern int GetCurrentProcess();
    >
    >
    > Matt
    >
    > ====================================
    > Mattias Sjögren - mattias @ mvps.org




  6. #6
    Join Date
    Mar 2009
    Posts
    1

    GetCurrentProcess() returning -1

    GetCurrentProcess() returns a pseudo handle for the current process.

    A pseudo handle is a special constant, -1, that is interpreted as the current process handle. The calling process can use a pseudo handle to specify its own process whenever a process handle is required.

    Use OpenProcess(). It should work fine.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center