[英]Can anyone tell me were I am making mistake in the snippet
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 the sample which i was trying to take the window procedure of a form, this is how i do in C++ i get the windwproc easlily if i try the same in C# .net 3.5 i am unable to get the window proc,, after calling SetWindowLong API application hangs and it pops up some dont send report... i have read this is the way to get the window proc.. please let me know were i am making mistake... 这是我尝试采用窗体的窗口过程的示例,这是我在C ++中所做的事情,如果我在C#.net 3.5中尝试相同的操作,则无法轻松获得windwproc,在调用SetWindowLong API应用程序挂起,并弹出一些不发送报告的信息...我已阅读这是获取窗口proc的方法..请让我知道我在犯错误...
SampleProc.Method .MethodHandle.Value.ToInt32()
SampleProc.Method .MethodHandle.Value.ToInt32()
Just use SampleProc
. 只需使用
SampleProc
。 If that fails, try marshalling it to a FunctionPointer
. 如果失败,请尝试将其编组到
FunctionPointer
。
The delegate instance does not need to be static. 委托实例不需要是静态的。 No idea why you think it should.
不知道为什么你认为应该。
I think you need to declare your delegate instance at the form level, like this: 我认为您需要在表单级别声明您的委托实例,如下所示:
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;
}
Your original delegate instance was being declared in the form's constructor, where it immediately went out of scope (and thus wasn't around anymore to be called back to). 您最初的委托实例在窗体的构造函数中声明,在该实例中它立即超出范围(因此不再被调用)。
There might be other problems with your sample, too. 您的样本可能还会有其他问题。
You are working with the compact framework, right? 您正在使用紧凑型框架,对吗? Are you doing everything from the same process?
您是否从同一过程中进行所有操作?
I have experienced this kind of trouble myself, if the process where the window is created is not the same process as the message stems from. 如果创建窗口的过程与消息所源自的过程不同,则我本人也遇到了这种麻烦。 However I actively used SendMessage to send a message.
但是我积极使用SendMessage发送消息。 If I did this from a different process, I got the "send error report" page.
如果我是通过其他流程执行此操作,则会看到“发送错误报告”页面。 I now have found out, that if the SendMessage comes from the same process, things work fine.
我现在发现,如果SendMessage来自同一进程,则一切正常。
In the above you could probably replace process with thread, although i am no sure. 在上面,您可能可以用线程替换进程,尽管我不确定。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.