向窗口发送 WM_KEYUP 消息会引发 OverflowException

发布于 2024-11-19 20:03:03 字数 1850 浏览 2 评论 0原文

我正在尝试实现一个程序,该程序将相同的消息发送到一个窗口,如果连续按下某个键,该窗口就会发送该消息。 这是应用程序的代码的一部分(整个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

没有你我更好 2024-11-26 20:03:03

我假设你正在为 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 valid IntPtr value (the maximum is 0x7FFFFFFF).

2 options here :

  • You do use the value lParam in the target application. In this case you have to use another value for the IntPtr, or use the type UIntPtr (but this type is not CLS-compliant).
  • You do not use the value lParam in the target application. In this case just pass IntPtr.Zero as lParam.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文