1

I am trying to subscribe to the WndProc(ref Message m) method via reflection.

Currently, I have the next code:

Assembly winFormsAssembly = AppDomain.CurrentDomain.GetAssemblies()
                                     .FirstOrDefault(asm => asm
                                                           .FullName
                                                           .Contains("System.Windows.Forms"));

Type formType = winFormsAssembly?.GetType("System.Windows.Forms.Form");
Type messageType = winFormsAssembly?.GetType("System.Windows.Forms.Message");

MethodInfo wndProcInfo = formType.GetMethod("WndProc",
                                            BindingFlags.NonPublic | BindingFlags.Instance,
                                            null,
                                            new[] {messageType.MakeByRefType()}, null);

It is necessary to receive an event when the method was invoked and get its parameters. Please give an advise which step should be the next.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Vladyslav
  • 31
  • 1
  • 1
    Why you think that it is possible like this? To add something to WndProc you need to override and via reflection you can only call ... Still you may try to use native `SetWindowLongPtr` with HWND of window (`Control.Handle`) to change it – Selvin Jul 15 '21 at 11:21
  • @Selvin I don't need to modify the WndProc method. In my case, it is enough to detect when it was invoked and obtain its parameters for information purposes without any modifications. – Vladyslav Jul 15 '21 at 11:32
  • *I don't need to modify the WndProc method* yes, you do ... there can be only one WndProc registered for given HWND ... to obtain information that you mentioned you need register new WndProc which would do log and then call old WndProc – Selvin Jul 15 '21 at 11:44
  • Does this answer your question? [AddEventHandler using reflection](https://stackoverflow.com/questions/1121441/addeventhandler-using-reflection) – thehennyy Jul 15 '21 at 11:45
  • @thehennyy there is no event related with `WndProc`, so no, **this is not a duplicate of *AddEventHandler using reflection*** – Selvin Jul 15 '21 at 11:46
  • @Selvin Ok, I understood, thank you. Are there any other ways to obtain WM messages of the form using reflection? – Vladyslav Jul 15 '21 at 11:56
  • [There is no need for reflection ...](https://gist.github.com/SelvinPL/2a12da21e7b97232b25ec6d7a1452331) all you need is `Control` derived object – Selvin Jul 15 '21 at 12:15
  • check link again I've added NativeWindow solution and WindowsMessage enum – Selvin Jul 15 '21 at 13:05
  • @Selvin I will try. Thank you. – Vladyslav Jul 16 '21 at 10:15

0 Answers0