Using WH_JOURNALRECORD and undo seems to return WM_CANCELJOURNAL

I am using C # and I have a program that successfully writes log messages using SetWindowsHookExc WH_JOURNALRECORD.

My problem arises when I need to stop. The docs show that if the user pressed CTRL-ESC or CTRL-ALT-DELETE, a message will be sent WM_CANCELJOURNALthat I can see when to stop. My application is disconnected, but I never get it WM_CANCELJOURNAL.

I have two hook settings. One hook for logging and one for checking cancel messages:

IntPtr hinstance = Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]);

JournalRecordProcedure = JournalRecordProc;
journalHook = SetWindowsHookEx(WH_JOURNALRECORD, JournalRecordProcedure, hinstance, 0);

GetMessageProcedure = GetMessageProc;
messageHook = SetWindowsHookEx(WH_GETMESSAGE, GetMessageProcedure, hinstance, 0); 


------

public static int JournalRecordProc(int nCode, IntPtr wParam, IntPtr lParam)
{
    if (nCode < 0) return CallNextHookEx(journalHook, nCode, wParam, lParam);

    EventMsgStruct msg = (EventMsgStruct) Marshal.PtrToStructure(lParam, typeof (EventMsgStruct));

    script.Add(msg); //just a quick way to record for now

    return CallNextHookEx(journalHook, nCode, wParam, lParam);
}

public static int GetMessageProc(int code, IntPtr wParam, IntPtr lParam)
{

    //it comes here but how do I test if it WM_CANCELJOURNAL ??
    //code always seems to be equal to zero.. I must be missing something


    return CallNextHookEx(journalHook, code, wParam, lParam);
}
+1
source share
1 answer

, :

, CTRL + BREAK . CTRL + C , , . , : CTRL + ESC CTRL + ALT + DEL. ( ), WM_CANCELJOURNAL .

, WM_CANCELJOURNAL , SetWindowsHookEx. WM_* (WndProc WinForms), - .

, , WH_GETMESSAGE hook:

. GetMessage, .

[., ]

WM_CANCELJOURNAL NULL, . WM_CANCELJOURNAL: , GetMessage PeekMessage DispatchMessage. , hook GetMsgProc ( SetWindowsHookEx WH_GETMESSAGE), .

WinForms , , . , : . , , , , , , , WH_GETMESSAGE, , WM_CANCELJOURNAL.


Update:

GetMessageProc code , hook . 0, HC_ACTION. code 0, hook CallNextHookEx . , JournalRecordProc.

MSG, lParam. Win32. .NET, P/Invoke . MSG System.Windows.Forms.Message ( , WndProc), , GetMessageProc : :

public delegate int GetMessageProc(int code, IntPtr wParam, ref Message lParam);

Windows MSG member Message. , WM_CANCELJOURNAL:

public static int GetMessageProc(int code, IntPtr wParam, ref Message lParam)
{
    if (code >= 0)
    {
        if (lParam.Msg == WM_CANCELJOURNAL)
        {
            // do something
        }
    }

    return CallNextHookEx(messageHook, code, wParam, ref lParam);
}

, , CallNextHookEx , CallNextHookEx, GetMessageProc:

[DllImport("user32.dll")]
public static extern int CallNextHookEx(IntPtr hHook, int nCode,
                                        IntPtr wParam, ref Message lParam);
+1

Source: https://habr.com/ru/post/1609800/


All Articles