none
Retrieving username of a running process RRS feed

  • Question

  • Hello,
    I've a windows service who's looking the owner of the Internet Explorer running process. I retrieve the PID with following code :

    Dim theProcesses() As Process = System.Diagnostics.Process.GetProcessesByName("iexplore")
    Dim PID As
    String
    Dim tp As
    Process
    For Each currentProcess As Process In
    theProcesses
       PID = currentProcess.Id
       tp = System.Diagnostics.Process.GetProcessById(PID)
    Next

    The question is now : how can I retrieve the username who is executing Internet Explorer ?

    Thank you for your help.

    wum

    Wednesday, November 22, 2006 3:25 PM

Answers

  • Hello All.

    wum:

    Here's some C# code that will do the job.  Sorry, I'm not too familiar with VB, so you may have to see if somebody can translate it into VB for you, if you can't.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    namespace ConsoleApplication1
    {

       class Program
       {

          //just use the current TS server context.
          internal static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;

          //the User Name is the info we want returned by the query.
          internal static int WTS_UserName = 5;

          static void Main(string[] args)
          {
             Process[] procs = Process.GetProcesses();
             foreach (Process proc in procs)
             {
                Console.WriteLine(proc.ProcessName);
                Console.WriteLine(proc.SessionId);

                //access to the Idle process is restricted, so don't try to access it.
                if (proc.ProcessName != "Idle")
                {
                   IntPtr AnswerBytes;
                   IntPtr AnswerCount;
                   if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE,
                                                   proc.SessionId,
                                                   WTS_UserName,
                                                   out AnswerBytes,
                                                   out
    AnswerCount))
                   {
                      string userName = Marshal.PtrToStringUni(AnswerBytes);
                      Console.WriteLine(userName);
                      Console.WriteLine();
                   }
                   else
                   {
                      Console.WriteLine("Could not access Terminal Services Server.");
                   }
                }
             }
             Console.ReadLine();
          }

          [DllImport("Wtsapi32.dll")]
          public static extern bool WTSQuerySessionInformationW(
          IntPtr hServer,
          int SessionId,
          int
    WTSInfoClass,
          out IntPtr ppBuffer,
          out IntPtr
    pBytesReturned);
       }
    }

    HTH.

    Friday, November 24, 2006 2:34 PM

All replies

  • using the Process.StartInfo.UserName should work for you. it gets or sets the username of the process. so....

    For Each currentProcess As Process In theProcesses
       PID = currentProcess.Id
       tp = System.Diagnostics.Process.GetProcessById(PID)

       MessageBox.Show(tp.StartInfo.UserName)
    Next

     

    does this work for you?

    Wednesday, November 22, 2006 7:51 PM
    Moderator
  • Thank you for the tip but unfortunally, it doesn't work. tp.StartInfo.UserName always seems to contains an emptry string.
    Thursday, November 23, 2006 7:58 AM
  • Try with WMI:

    Dim args(1) As Object
    Dim ms As New ManagementObjectSearcher("SELECT * FROM Win32_Process WHERE ProcessId = " & ID)

    Debug.WriteLine(
    "Domain: User")
    For Each mo As ManagementObject In ms.Get
      If CUInt(mo.InvokeMethod("GetOwner", args)) = 0 Then
        Debug.WriteLine(args(1).ToString & ": " & args(0).ToString)
      End If
    Next

    Thursday, November 23, 2006 11:20 AM
  • Thanks for your help but I have another problem:

    The command ms.Get causes an "Initialization failure". If I try the GetInstances method of a ManagementClass, the same failure appears. The System.Management class has been imported.

    Is there anything else to do to bring the WMI to work ?

    Friday, November 24, 2006 7:01 AM
  • this might be an internal WMI error...

    try here: http://www.microsoft.com/technet/scriptcenter/topics/help/wmidiag.mspx

    Friday, November 24, 2006 9:13 AM
  • Hello All.

    wum:

    Here's some C# code that will do the job.  Sorry, I'm not too familiar with VB, so you may have to see if somebody can translate it into VB for you, if you can't.

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    namespace ConsoleApplication1
    {

       class Program
       {

          //just use the current TS server context.
          internal static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;

          //the User Name is the info we want returned by the query.
          internal static int WTS_UserName = 5;

          static void Main(string[] args)
          {
             Process[] procs = Process.GetProcesses();
             foreach (Process proc in procs)
             {
                Console.WriteLine(proc.ProcessName);
                Console.WriteLine(proc.SessionId);

                //access to the Idle process is restricted, so don't try to access it.
                if (proc.ProcessName != "Idle")
                {
                   IntPtr AnswerBytes;
                   IntPtr AnswerCount;
                   if (WTSQuerySessionInformationW(WTS_CURRENT_SERVER_HANDLE,
                                                   proc.SessionId,
                                                   WTS_UserName,
                                                   out AnswerBytes,
                                                   out
    AnswerCount))
                   {
                      string userName = Marshal.PtrToStringUni(AnswerBytes);
                      Console.WriteLine(userName);
                      Console.WriteLine();
                   }
                   else
                   {
                      Console.WriteLine("Could not access Terminal Services Server.");
                   }
                }
             }
             Console.ReadLine();
          }

          [DllImport("Wtsapi32.dll")]
          public static extern bool WTSQuerySessionInformationW(
          IntPtr hServer,
          int SessionId,
          int
    WTSInfoClass,
          out IntPtr ppBuffer,
          out IntPtr
    pBytesReturned);
       }
    }

    HTH.

    Friday, November 24, 2006 2:34 PM
  • I have found a way to retrieve the username of a user using WMI. Thanks to LEPACA, you're right. The problem was an internal WMI error and I could correct it with the WMIDiag tool.

    Everything works fine now.
    Thanks to you all.

    WUM

    Monday, November 27, 2006 8:33 AM