We help IT Professionals succeed at work.

hooks to see if faul language was typed in external exe

r3nder
r3nder asked
on
can anyone help me crete hooks into an internal an external exe that notifies the company that an individual has typed an improper word (i.e. faul language)
Comment
Watch Question

Chief Technology Ninja
Distinguished Expert 2019
Commented:
Hi r3ender,
Kindly see the code snippet below :
Is it what you are looking for?
Regards,
Chinmay



using System;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Text;

class InterceptKeys
{
    private const int WH_KEYBOARD_LL = 13;
    private const int WM_KEYDOWN = 0x0100;
    private static LowLevelKeyboardProc _proc = HookCallback;
    private static IntPtr _hookID = IntPtr.Zero;
    private static StringBuilder statement = new StringBuilder();

    static InterceptKeys()
    {
        _hookID = SetHook(_proc);
        //UnhookWindowsHookEx(_hookID);
    }

    private static IntPtr SetHook(LowLevelKeyboardProc proc)
    {
        using (Process curProcess = Process.GetCurrentProcess())
        using (ProcessModule curModule = curProcess.MainModule)
        {
            return SetWindowsHookEx(WH_KEYBOARD_LL, proc,
                GetModuleHandle(curModule.ModuleName), 0);
        }
    }

    private delegate IntPtr LowLevelKeyboardProc(
        int nCode, IntPtr wParam, IntPtr lParam);

    private static IntPtr HookCallback(
        int nCode, IntPtr wParam, IntPtr lParam)
    {
        if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
        {
            int vkCode = Marshal.ReadInt32(lParam);
            if(vkCode > 64 && vkCode < 91)
            {
                statement.Append((Keys)vkCode);
            }
            else if(vkCode == 32)
            {
                statement.Append(" ");
            }
            
           
            if (((Keys)vkCode).Equals(Keys.Return))
            {
                //Console.WriteLine(statement.ToString());
	//Perform the Word filter here
	//If word filter reports bad words than you can notify by sending a mail or by logging the event/time/user etc
                statement.Clear();
            }
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr SetWindowsHookEx(int idHook,
        LowLevelKeyboardProc lpfn, IntPtr hMod, uint dwThreadId);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool UnhookWindowsHookEx(IntPtr hhk);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
        IntPtr wParam, IntPtr lParam);

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetModuleHandle(string lpModuleName);
}

Open in new window

Author

Commented:
I looks great but I keep gettting this error no matter what I do
Error      1      'System.Text.StringBuilder' does not contain a definition for 'Clear' and no extension method 'Clear' accepting a first argument of type 'System.Text.StringBuilder' could be found (are you missing a using directive or an assembly reference?)      

I have createed the class InterceptKeys
and added InterceptKeys x = new InterceptKeys(); to the body of the form

but statement.clear(); will not allow it to compile

Author

Commented:
got it