-4

I'm writing a keylogger program. In my program, I want to write to log file current window title which is active. And if user changes to other windows, log file will be appended new title window. Here I have the code for getting windows title :

[System.Runtime.InteropServices.DllImport("user32.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]
    public static extern int GetWindowText(IntPtr hwnd, string lpString, int cch);
    public static string ActiveApplTitle()
    {
        //This method is used to get active application's title using GetWindowText() method present in user32.dll
        IntPtr hwnd = GetForegroundWindow();
        if (hwnd.Equals(IntPtr.Zero)) return "";
        string lpText = new string((char)0, 100);
        int intLength = GetWindowText(hwnd, lpText, lpText.Length);
        if ((intLength <= 0) || (intLength > lpText.Length)) return "unknown";
        return lpText.Trim();
    }

So, I don't know how to update window title when there's a change. Please give me an idea. Thanks a lot!

0xn0n4m3
  • 23
  • 9

1 Answers1

-2

Can you stick it in a loop on another thread. Have two variables: previousWindow and currentWindow. Keep comparing them, when they change - you update your log file.

jules0075
  • 74
  • 1
  • 2
  • 10