0

I'm trying to capture the WM_PASTE message if I paste from the clipboard to an application outside my own, but it never fires. Is there any way to do this or am I stuck with using a keyboard hook?

Relevant Code:

//--dllImport
    [DllImport("user32.dll")]
    static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);

    [DllImport("user32.dll")]
    static extern IntPtr GetClipboardViewer();

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);

    [DllImport("user32.dll", SetLastError = true)]
    static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);

    IntPtr hWndNextWindow;

    //--Variables


    //--ClipboardHandler
    protected override void WndProc(ref Message m)
    {
        switch (m.Msg)
        {
            case (0x0001): // WM_CREATE
                hWndNextWindow = SetClipboardViewer(this.Handle);
                break;
            case (0x0002): // WM_DESTROY
                ChangeClipboardChain(this.Handle, hWndNextWindow);
                break;
            case (0x030D): // WM_CHANGECBCHAIN
                if (m.WParam == hWndNextWindow)
                    hWndNextWindow = m.LParam;
                else if (hWndNextWindow != IntPtr.Zero)
                    SendMessage(hWndNextWindow, m.Msg, m.WParam, m.LParam);
                break;
            case (0x0308): // WM_DRAWCLIPBOARD
                {
                    //DoStuff
                }
                SendMessage(hWndNextWindow, m.Msg, m.WParam, m.LParam);
                break;


            //WM_PASTE
            case (0x0302):
                //DEBUG
                MessageBox.Show("PASTE");
                break;


        }

        base.WndProc(ref m);
    }
WolfyD
  • 865
  • 3
  • 14
  • 29
  • What is your ultimate goal? Do you want to prevent users copying from your application? – CodeCaster Feb 10 '15 at 11:28
  • I'm working on a clipboard extender, I was hoping to add functionality to save multiple copied strings and return them one after the other but I'd have to know when to set the clipboard to the new string when the last one was pasted out – WolfyD Feb 10 '15 at 11:30
  • I think, You must hook into windows messages globally to do this. – icbytes Feb 10 '15 at 11:31
  • A keyboard hook couldn't work, I think. It couldn't detect clicks on the Paste button in an application's toolbar, for instance, could it? –  Feb 10 '15 at 11:31
  • @icbytes Could you give me some help, or some tips on where to look for help on this? – WolfyD Feb 10 '15 at 11:33
  • You can hook mouse events and keyboard-events. Eack click will point to something "under" the mouse-cursor, which also can be obtained. So basically this should work. – icbytes Feb 10 '15 at 11:33
  • @hvd Yeah that is what i was worried about as well, that's why I was hoping that it could be just solved by `WM_PASTE` – WolfyD Feb 10 '15 at 11:34
  • @WolfyD: You can take a look in codeproject, there are surely also examples in .net . http://www.codeproject.com/Articles/49319/Easy-way-to-set-up-global-API-hooks and http://www.codeproject.com/Articles/7294/Processing-Global-Mouse-and-Keyboard-Hooks-in-C – icbytes Feb 10 '15 at 11:35
  • 1
    Well I'm like completely new to this stuff so i guess this is gonna be a lot harder then I've hoped – WolfyD Feb 10 '15 at 11:41

0 Answers0