C# 和 SendMessage(键)不起作用

发布于 2024-09-07 00:19:26 字数 752 浏览 2 评论 0原文

我尝试向应用程序发送密钥。为了方便测试,我只使用记事本。这就是代码的样子:

[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 的原因。

I tried to send a key to an application. For an easy test I just used notepad. That's what the code looks like:

[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);
    }

But nothing happens.

My main goal is to send the key to an elevated application, but I would be happy to send it to notepad first.
I want to work with SendMessage, because I want to control how long I press a button, also I don't want to have the other application in the foreground. That's the reason I am not working with SendKeys.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无声无音无过去 2024-09-14 00:19:26

几个问题:

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

颈部射击:Vista 和 Win7 UIPI(用户界面权限隔离)可防止受限制的进程注入消息进入一个提升的过程。

Several problems:

  • Your declaration is wrong, the last 2 parameters are IntPtr, not int
  • You should use PostMessage, not SendMessage
  • You are sending to the wrong window. The edit window of Notepad is a child window
  • You are forgetting to send WM_KEYUP
  • The actual letter you get will depend on the state of the Shift key

The neck shot: Vista and Win7 UIPI (User Interface Privilege Isolation) prevents a restricted process from injecting messages into an elevated process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文