【问题标题】:C# and SendMessage (keys) is not workingC# 和 SendMessage(键)不起作用
【发布时间】:2011-03-06 01:26:24
【问题描述】:

我尝试向应用程序发送密钥。为了一个简单的测试,我只使用了记事本。代码是这样的:

[DllImport("USER32.DLL", EntryPoint = "SendMessageW", SetLastError = true,
         CharSet = CharSet.Unicode, ExactSpelling = true,
         CallingConvention = CallingConvention.StdCall)]
    public static extern bool SendMessage(IntPtr hwnd, int Msg, int wParam, int lParam);


        const int WM_KEYDOWN = 0x100;
    const int WM_a = 0x41;

        public void Press()
    {
        Process[] p = Process.GetProcessesByName("notepad");
        IntPtr pHandle = p[0].MainWindowHandle;

        SendMessage(pHandle, WM_KEYDOWN, WM_a, 0);
    }

但是什么也没发生。

我的主要目标是将密钥发送到提升的应用程序,但我很乐意先将其发送到记事本。 我想使用 SendMessage,因为我想控制按下按钮的时间,而且我不想让其他应用程序在前台。这就是我不使用 SendKeys 的原因。

【问题讨论】:

    标签: c# pinvoke sendmessage


    【解决方案1】:

    几个问题:

    • 你的声明有误,最后2个参数是IntPtr,不是int
    • 您应该使用 PostMessage,而不是 SendMessage
    • 您发送到错误的窗口。记事本的编辑窗口是子窗口
    • 您忘记发送 WM_KEYUP
    • 实际得到的字母取决于 Shift 键的状态

    颈部截图:Vista 和 Win7 UIPI(用户界面特权隔离)可防止受限进程将消息注入提升的进程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-01
      • 1970-01-01
      相关资源
      最近更新 更多