Can someone tell me that I am wrong in the passage

public partial class Form1 : Form
{
  [DllImport("coredll.dll")]
  static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

  const int GWL_WNDPROC = -4;

  public delegate int WindProc(IntPtr hWnd, uint msg, long Wparam, long lparam);

    public Form1()
    {
        InitializeComponent();

        WindProc SampleProc = new WindProc (SubclassWndProc);

        SetWindowLong(this .Handle , GWL_WNDPROC,
            SampleProc.Method .MethodHandle.Value.ToInt32());

    }

public int SubclassWndProc(IntPtr  hwnd, uint  msg, long  Wparam, long  lparam)
{
    return 1;
}

      

Here is an example I tried to take a form window procedure, this is how I do in C ++ I get windwproc easlily, if I try to do the same in C # .net 3.5, I cannot get the proc window, after the application call SetWindowLong API hangs and it pops up, some don't send the report ... I read that this is the way to get the proc window .. Please let me know, I am making a mistake ...

+2


source to share


4 answers


SampleProc.Method.MethodHandle.Value.ToInt32 ()



Just use SampleProc

. If that fails, try sorting it with FunctionPointer

.

+3


source


The delegate instance does not have to be static. Not sure why you think it should be.



+1


source


I think you need to declare your delegate instance at the form level, for example:

public partial class Form1 : Form
{
    [DllImport("coredll.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    const int GWL_WNDPROC = -4;
    public delegate int WindProc(IntPtr hWnd, uint msg, 
        long Wparam, long lparam);
    private WindProc _SampleProc;
    public Form1()
    {
        InitializeComponent();
        _SampleProc = new WindProc(SubclassWndProc);
        SetWindowLong(this.Handle, GWL_WNDPROC,
            _SampleProc.Method.MethodHandle.Value.ToInt32());

    }
    public int SubclassWndProc(IntPtr  hwnd, uint  msg, 
        long  Wparam, long  lparam)
    {
        return 1;
    }

      

The original delegate instance was declared in the form's constructor, where it immediately went out of scope (and therefore never came back again).

There may be other problems with your sample.

0


source


You are working with a compact frame, right? Are you doing everything from one process?

I've experienced this kind of problem myself if the process in which the window is created is not the same process as the message. However, I used SendMessage extensively to send the message. If I did it from a different process, I got the Send Bug page. Now I figured out that if SendMessage comes from the same process everything works fine.

In the example above, you can probably replace the process with a thread, although I'm not sure.

0


source







All Articles