On the terminal server, 10 people run the application, let it be CALC.exe, and naturally, in the processes of 10 SINGLE processes.

Here I was told how to find the path to the file of the desired process.

And how to do the same thing, only if the user is called Vasya, then the process started by VOSEY was?

  • Next time, please, formulate the whole question right away. And then, after all, not every solution of the problem by small victims is doped to a more complex solution. - kirelagin
  • @Eugene Don't forget, please, take (tick) the best answer to a question. - Nicolas Chabanovsky

2 answers 2

The easiest option with WMI:

string processName = "calc.exe"; string currentUser = WindowsIdentity.GetCurrent().Name.Split('\\')[1]; string query = "Select * from Win32_Process Where Name = \"" + processName + "\""; ManagementObjectSearcher searcher = new ManagementObjectSearcher(query); ManagementObjectCollection processes = searcher.Get(); foreach (ManagementObject proc in processes) { string owner; string[] argList = new string[] { string.Empty }; int returnVal = Convert.ToInt32(proc.InvokeMethod("GetOwner", argList)); if (returnVal == 0) owner = argList[0]; else continue; if (owner != currentUser) continue; // Вот тут-то и остался только нужный процесс! string path = proc["ExecutablePath"].ToString(); } 
  • By the way! How could I not even think about WMI. Since there is no direct interaction between the processes, this method is deep on the drum bit. - kirelagin

On WinApi, the problem was solved even in the first question) Search for msdn immediately issued:

We get the process token by handle: OpenProcessToken (...)

User SID - in the information about the token: GetTokenInformation (...)


@kirelagin:

Original with msdn:

 BOOL WINAPI OpenProcessToken( __in HANDLE ProcessHandle, __in DWORD DesiredAccess, __out PHANDLE TokenHandle ); 

In c #:

 [DllImport("Advapi32.dll", EntryPoint = "OpenProcessToken")] private static extern bool OpenProcessToken(int ProcessHandle, int DesiredAccess, int* TokenHandle); 
  • Unfortunately, WinAPI in C # is not easy to screw in :(. - kirelagin