C# 和 SendMessage(键)不起作用
我尝试向应用程序发送密钥。为了方便测试,我只使用记事本。这就是代码的样子:
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几个问题:
颈部射击:Vista 和 Win7 UIPI(用户界面权限隔离)可防止受限制的进程注入消息进入一个提升的过程。
Several problems:
The neck shot: Vista and Win7 UIPI (User Interface Privilege Isolation) prevents a restricted process from injecting messages into an elevated process.