Get a process owner username...
Mike Andrews <[email protected]> Wed, 20 May 2009 09:49:07 -0500
| Newsgroups | gmane.comp.windows.devel.dotnet.advanced |
|---|---|
| Message-ID | <[email protected]> |
I really need some help on this one.
I need to get the owner (domain/username) of a process. I found some code
on CodeProject <http://www.codeproject.com/KB/cs/processownersid.aspx> that
looked/looks promising for what I need but it still falls short.
I can't seem to get past the OpenProcessToken Win32 api call that I need.
Here's some sample code that I'm using just to test it out:
public enum DesireAccess : uint {
STANDARD_RIGHTS_REQUIRED = 0x000F0000,
STANDARD_RIGHTS_READ = 0x00020000,
TOKEN_ASSIGN_PRIMARY = 0x0001,
TOKEN_DUPLICATE = 0x0002,
TOKEN_IMPERSONATE = 0x0004,
TOKEN_QUERY = 0x0008,
TOKEN_QUERY_SOURCE = 0x0010,
TOKEN_ADJUST_PRIVILEGES = 0x0020,
TOKEN_ADJUST_GROUPS = 0x0040,
TOKEN_ADJUST_DEFAULT = 0x0080,
TOKEN_ADJUST_SESSIONID = 0x0100,
TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY),
TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY |
TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY |
TOKEN_QUERY_SOURCE |
TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT
|
TOKEN_ADJUST_SESSIONID)
}
public static string GetProcessUser(Process p) {
IntPtr k = IntPtr.Zero;
var t = OpenProcessToken(p.Handle, DesireAccess.TOKEN_ALL_ACCESS, out
k);
var lastError = Marshal.GetLastWin32Error();
var exception = new Win32Exception(lastError);
return null;
}
When I step through this bit of code the OpenProcessToken method returns 0.
According to MSDN<http://msdn.microsoft.com/en-us/library/aa379295(VS.85).aspx>a
return value of 0 indicates an error. So I use the
Marshall.GetLastWin32Error() method and then create a new Win32Exception
object to retrieve the message. The message is Access Denied.
I'm at a loss to understand why that error and what I'm doing wrong here.
Any suggestions?
Thanks,
Mike
===================================
View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives