PC Review


Reply
Thread Tools Rate Thread

who created a process?

 
 
Strahimir Antoljak
Guest
Posts: n/a
 
      14th Aug 2003

Is there a way to find out the name
of the user who created a process.
Some kind of process property or method
that would report the user name how launched
it?
(but not Environment.UserName)

Thanks,

--
Strah


 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      14th Aug 2003
Strahimir Antoljak wrote:
|| Is there a way to find out the name
|| of the user who created a process.
|| Some kind of process property or method
|| that would report the user name how launched
|| it?
|| (but not Environment.UserName)
||
|| Thanks,
||
|| --
|| Strah

There is no support for this in the FCL.
Your only option is to PInvoke (or MC++)
1. call the Win32 'OpenProcessToken' Win32 API using the 'Process.Handle' property as the first argument
2. use the tokenHandle returned to call WindowsIdentity(tokenHandle)
3. WindowsIdentity.Name should contain the process owner.
4. Close the tokenHandle using the 'CloseHandle' Win32 API.

Note that you will need special privileges to call OpenProcessToken, consult the SDK docs for details.
Willy.


 
Reply With Quote
 
David Browne
Guest
Posts: n/a
 
      14th Aug 2003

"Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Strahimir Antoljak wrote:
> || Is there a way to find out the name
> || of the user who created a process.
> || Some kind of process property or method
> || that would report the user name how launched
> || it?
> || (but not Environment.UserName)
> ||
> || Thanks,
> ||
> || --
> || Strah
>
> There is no support for this in the FCL.
> Your only option is to PInvoke (or MC++)
> 1. call the Win32 'OpenProcessToken' Win32 API using the 'Process.Handle'

property as the first argument
> 2. use the tokenHandle returned to call WindowsIdentity(tokenHandle)
> 3. WindowsIdentity.Name should contain the process owner.
> 4. Close the tokenHandle using the 'CloseHandle' Win32 API.
>
> Note that you will need special privileges to call OpenProcessToken,

consult the SDK docs for details.
> Willy.
>

Yikes. Sounds scarry.

Anyway there is a performance counter that will tell you this. It is slow,
since the instances are identified by name, so you have to iterate all the
Process counter instances, but it's probably fast enough for some purposes.
Eg to determine if a application has been started as a service or not.


Function GetCreatingProcessID(ByVal processID As Integer) As Integer
Dim creatingProcess As Integer
Dim cat As New System.Diagnostics.PerformanceCounterCategory("Process")
Dim instance As String
For Each instance In cat.GetInstanceNames()
Dim pid As New System.Diagnostics.PerformanceCounter("Process", "ID
Process", instance, True)
If pid.RawValue = processID Then
Dim creator As New System.Diagnostics.PerformanceCounter("Process",
"Creating Process ID", instance, True)
creatingProcess = creator.RawValue
pid.Dispose()
creator.Dispose()
Return creatingProcess
End If
pid.Dispose()
Next
Throw New Exception("Process " & processID.ToString & " not found")
End Function

David


 
Reply With Quote
 
Strahimir Antoljak
Guest
Posts: n/a
 
      14th Aug 2003
David,

I needed a user name (logon name) who created
a process, and this gives me some integer???
thanks

--
Strah

"David Browne" <davidbaxterbrowne no potted (E-Mail Removed)> wrote in
message news:(E-Mail Removed)...
>
> "Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > Strahimir Antoljak wrote:
> > || Is there a way to find out the name
> > || of the user who created a process.
> > || Some kind of process property or method
> > || that would report the user name how launched
> > || it?
> > || (but not Environment.UserName)
> > ||
> > || Thanks,
> > ||
> > || --
> > || Strah
> >
> > There is no support for this in the FCL.
> > Your only option is to PInvoke (or MC++)
> > 1. call the Win32 'OpenProcessToken' Win32 API using the

'Process.Handle'
> property as the first argument
> > 2. use the tokenHandle returned to call WindowsIdentity(tokenHandle)
> > 3. WindowsIdentity.Name should contain the process owner.
> > 4. Close the tokenHandle using the 'CloseHandle' Win32 API.
> >
> > Note that you will need special privileges to call OpenProcessToken,

> consult the SDK docs for details.
> > Willy.
> >

> Yikes. Sounds scarry.
>
> Anyway there is a performance counter that will tell you this. It is

slow,
> since the instances are identified by name, so you have to iterate all the
> Process counter instances, but it's probably fast enough for some

purposes.
> Eg to determine if a application has been started as a service or not.
>
>
> Function GetCreatingProcessID(ByVal processID As Integer) As Integer
> Dim creatingProcess As Integer
> Dim cat As New

System.Diagnostics.PerformanceCounterCategory("Process")
> Dim instance As String
> For Each instance In cat.GetInstanceNames()
> Dim pid As New System.Diagnostics.PerformanceCounter("Process", "ID
> Process", instance, True)
> If pid.RawValue = processID Then
> Dim creator As New

System.Diagnostics.PerformanceCounter("Process",
> "Creating Process ID", instance, True)
> creatingProcess = creator.RawValue
> pid.Dispose()
> creator.Dispose()
> Return creatingProcess
> End If
> pid.Dispose()
> Next
> Throw New Exception("Process " & processID.ToString & " not found")
> End Function
>
> David
>
>



 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      14th Aug 2003
David Browne wrote:
|| "Willy Denoyette [MVP]" <(E-Mail Removed)> wrote in message
|| news:(E-Mail Removed)...
||| Strahimir Antoljak wrote:
||||| Is there a way to find out the name
||||| of the user who created a process.
||||| Some kind of process property or method
||||| that would report the user name how launched
||||| it?
||||| (but not Environment.UserName)
|||||
||||| Thanks,
|||||
||||| --
||||| Strah
|||
||| There is no support for this in the FCL.
||| Your only option is to PInvoke (or MC++)
||| 1. call the Win32 'OpenProcessToken' Win32 API using the
||| 'Process.Handle' property as the first argument
||| 2. use the tokenHandle returned to call WindowsIdentity(tokenHandle)
||| 3. WindowsIdentity.Name should contain the process owner.
||| 4. Close the tokenHandle using the 'CloseHandle' Win32 API.
|||
||| Note that you will need special privileges to call OpenProcessToken,
|| consult the SDK docs for details.
||| Willy.
|||
|| Yikes. Sounds scarry.
||

Yes, it is :-), another option is to use the System.Management (WMI) namespace.

|| Anyway there is a performance counter that will tell you this. It


No, it's not, OP asked for the user principal name of the creator of the process.

Here is how to do it in (C#)

using System;
using System.Text;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Security.Principal;
using System.Diagnostics;
using System.Security;
// Problem: proc.Handle property returns Access denied for 'idle' process and,
// Cannot OpenProcessToken() for NT AUTHORITY\NETWORK SERVICE and NT AUTHORITY\LOCAL SYSTEM
// Better use - WTSEnumerateProcesses on XP and higher
//

using HANDLE = System.IntPtr;
class IdentUser {

[DllImport("advapi32", SetLastError=true), SuppressUnmanagedCodeSecurityAttribute]
static extern int OpenProcessToken(
HANDLE ProcessHandle, // handle to process
int DesiredAccess, // desired access to process
ref IntPtr TokenHandle // handle to open access token
);

[DllImport("kernel32", SetLastError=true), SuppressUnmanagedCodeSecurityAttribute]
static extern bool CloseHandle(HANDLE handle);

public const int TOKEN_QUERY = 0X00000008;


public static void Main() {

Process[] _process = Process.GetProcesses();
foreach(Process proc in _process)
{
try {
Console.WriteLine("Process Name :{0} \tProcess ID : {1} ",

proc.ProcessName, proc.Id);

DumpPrincipalName(proc.Handle);
Console.WriteLine("--------------------------------------------------");
}
catch(Exception ex)
{Console.WriteLine("Exception: {0}", ex.Message);}
}
}


static void DumpPrincipalName(HANDLE processHandle)
{
int access = TOKEN_QUERY;
HANDLE tokenHandle = IntPtr.Zero;
if ( 0 != OpenProcessToken( processHandle, access, ref tokenHandle ) )
{
WindowsIdentity wi = new WindowsIdentity(tokenHandle);
Console.WriteLine(wi.Name);
CloseHandle(tokenHandle); // Close process token
}
else
Console.WriteLine("Error OpenProcessToken: {0}",Marshal.GetLastWin32Error());
}

}




 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to make the WINWORD process only for the application I created =?Utf-8?B?SmVycnk=?= Microsoft Word Document Management 0 8th Mar 2007 02:43 AM
Getting Process ID of a created ActiveX object tensai Microsoft Dot NET Framework 0 11th Aug 2005 02:28 AM
thread abort ignored when GUI created and run in process caincognito@yahoo.com Microsoft C# .NET 0 28th Jul 2005 12:12 AM
thread abort ignored when GUI created and run in process caincognito@yahoo.com Microsoft C# .NET 0 28th Jul 2005 12:12 AM
who created a process? Strahimir Antoljak Microsoft C# .NET 6 14th Aug 2003 08:26 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 05:42 AM.