向窗口发送 WM_KEYUP 消息会引发 OverflowException
我正在尝试实现一个程序,该程序将相同的消息发送到一个窗口,如果连续按下某个键,该窗口就会发送该消息。 这是应用程序的代码的一部分(整个 Form1.cs 代码位于此处):
[DllImport("User32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, UIntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
private void button1_Click(object sender, EventArgs e)
{
// find notepad process
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad");
// find child handle of notepad process
// that is where we can send WM_KEYDOWN, WM_CHAR and WM_KEY up messages to write in the window
IntPtr childHandle = FindWindowEx(p[0].MainWindowHandle, IntPtr.Zero,"Edit", IntPtr.Zero);
// WM_KEYDOWN message with parameters for 1st key
SendMessage(childHandle, (uint)0x0100, (UIntPtr)0x00000041, (IntPtr)(0x001E0001));
// WM_CHAR message with parameters for 1st key
SendMessage(childHandle, (uint)0x0102, (UIntPtr)0x00000061, (IntPtr)(0x001E0001));
// WM_KEYDOWN message with parameters for n-th key
SendMessage(childHandle, (uint)0x0100, (UIntPtr)0x00000041, (IntPtr)(0x401E0001));
// WM_CHAR message with parameters for n-th key
SendMessage(childHandle, (uint)0x0102, (UIntPtr)0x00000061, (IntPtr)(0x401E0001));
// WM_KEYUP message
SendMessage(childHandle, (uint)0x0101, (UIntPtr)0x00000041, (IntPtr)(0xC01E0001));
}
到达 WM_KEYUP SendMessage 时在代码中,程序崩溃并给出错误:
我该如何修复此错误? 在 WM_KEYUP 之前的 4 个 SendMessage 调用工作正常,它们向记事本发送 2 个字母“a”。
感谢您的回复
I am trying to implement a program that sends the same messages to a window that would be sent if a certain key is continuously pressed.
This is part of the code (entire Form1.cs code is here ) for the application:
[DllImport("User32.dll")]
static extern int SendMessage(IntPtr hWnd, uint wMsg, UIntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, IntPtr windowTitle);
private void button1_Click(object sender, EventArgs e)
{
// find notepad process
System.Diagnostics.Process[] p = System.Diagnostics.Process.GetProcessesByName("notepad");
// find child handle of notepad process
// that is where we can send WM_KEYDOWN, WM_CHAR and WM_KEY up messages to write in the window
IntPtr childHandle = FindWindowEx(p[0].MainWindowHandle, IntPtr.Zero,"Edit", IntPtr.Zero);
// WM_KEYDOWN message with parameters for 1st key
SendMessage(childHandle, (uint)0x0100, (UIntPtr)0x00000041, (IntPtr)(0x001E0001));
// WM_CHAR message with parameters for 1st key
SendMessage(childHandle, (uint)0x0102, (UIntPtr)0x00000061, (IntPtr)(0x001E0001));
// WM_KEYDOWN message with parameters for n-th key
SendMessage(childHandle, (uint)0x0100, (UIntPtr)0x00000041, (IntPtr)(0x401E0001));
// WM_CHAR message with parameters for n-th key
SendMessage(childHandle, (uint)0x0102, (UIntPtr)0x00000061, (IntPtr)(0x401E0001));
// WM_KEYUP message
SendMessage(childHandle, (uint)0x0101, (UIntPtr)0x00000041, (IntPtr)(0xC01E0001));
}
Upon reaching the WM_KEYUP SendMessage in the code the program crashes and gives the error:
How can i fix this error?
The 4 SendMessage calls before the WM_KEYUP one work ok and they send 2 letter 'a' to notepad.
Thanks for your replies
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设你正在为 x86 编译。在 x86 目标上,
0xC01E0001
不是有效的IntPtr
值(最大值为0x7FFFFFFF
)。这里有 2 个选项:
lParam
。在这种情况下,您必须为IntPtr
使用另一个值,或者使用UIntPtr
类型(但此类型不符合 CLS)。lParam
。在这种情况下,只需将IntPtr.Zero
作为lParam
传递即可。I assume you are compiling for x86. On x86 targets,
0xC01E0001
is not a validIntPtr
value (the maximum is0x7FFFFFFF
).2 options here :
lParam
in the target application. In this case you have to use another value for theIntPtr
, or use the typeUIntPtr
(but this type is not CLS-compliant).lParam
in the target application. In this case just passIntPtr.Zero
aslParam
.