1

So I've been contemplating if it is possible to use a delegate in to a PInvoke signature when the signature calls for a handle to a window (Such as in AddClipboardFormatListener(IntPtr hwnd)).

I ask because essentially all it does is makes a call to said windows HwnProc method....So what if I made a delegate like this:

public delegate IntPtr MainWindowProc(IntPtr hwnd, int msg, 
                IntPtr wParam, IntPtr lParam, ref bool handled)

and changed AddClipboardFormatListener to look like this instead:

AddClipboardFormatListener(MainWindowProc theDelegate)

I got the idea from SetWindowsHookEx where I made a delegate for the callback for the second parameter. I'm fixing to wire up a test for this right now, so I might be answering my own question... but then I got to thinking they don't call the method, they get the instance and call he method.. So maybe I'd have to do something like

public interface ICanReadWindows
{
    IntPtr MainWindowProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled);
}

and pass that in to AddClipboardFormatListener??? I'm not super familiar with how that all plays together so I was hoping to pick peoples brains (such as Hans)

Thank you

Rohit Gupta
  • 2,411
  • 11
  • 21
  • 36
Robert Snyder
  • 2,269
  • 2
  • 31
  • 54
  • 1
    No, you need a window and a message loop. The window already takes care of the delegate. Pass the window's handle to the function. Boilerplate code [is here](http://stackoverflow.com/a/2061741/17034), just change the WndProc() override. – Hans Passant Aug 14 '15 at 22:46

0 Answers0