C# 使用 PostMessage
我正在尝试向应用程序发送密钥。我使用断点测试了 Handlewindow 值来了解我在做什么,但我找不到解决方案。更详细地说,这是一个小游戏,当我在游戏中激活聊天栏时,我想要发送的密钥将写在那里,但我想让它在我玩游戏时使用命令。游戏没有守卫或一些保护措施。
这是我的代码:
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
const uint WM_KEYDOWN = 0x0100;
private void button1_Click(object sender, EventArgs e)
{
string pName = textBox1.Text;
//Get Processes
Process[] processes = Process.GetProcessesByName(pName);
//Main part
foreach (Process p in processes)
if (p.ProcessName == (string)pName)
{
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (int)Keys.W, 0);
}
}
就像我说的,它可以成功发送 1000000 次,但没有任何反应。 是否有其他方法可以将密钥发送到最小化甚至隐藏的 Windows 应用程序?它应该只发送到我的应用程序。
I'm trying to send a key to an application. I tested the Handlewindow value used breakpoints to understand what I'm doing wirong but i cant find a solution. To be more detailed, its a little game and when I activate the chatbar ingame, the key I want to send will be written there, but I want to make it functionaly when I am playing to use the commands. The game doesnt have a guard or some protections.
Here is my code:
[DllImport("user32.dll")]
static extern bool PostMessage(IntPtr hWnd, uint Msg, int wParam, int lParam);
const uint WM_KEYDOWN = 0x0100;
private void button1_Click(object sender, EventArgs e)
{
string pName = textBox1.Text;
//Get Processes
Process[] processes = Process.GetProcessesByName(pName);
//Main part
foreach (Process p in processes)
if (p.ProcessName == (string)pName)
{
PostMessage(p.MainWindowHandle, WM_KEYDOWN, (int)Keys.W, 0);
}
}
Like I said, it can be sent 1000000 times successfully but nothing happens.
Is there another way how I can send keys to an Windows application that works minimized or even hidden? It should be only send to my app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
请注意,
PostMessage
的正确导入是:像您所做的那样发送密钥可能不起作用,但您可以使用间谍++来捕获由 Windows 发布到应用程序的实际消息并使用这些消息。
https://learn.microsoft。 com/en-us/visualstudio/debugger/introducing-spy-increment?view=vs-2022
Just note that the correct import of
PostMessage
is:Sending the keys like you are doing might not work, but you can use spy++ to capture the actual messages that are being posted to the application by windows and use those.
https://learn.microsoft.com/en-us/visualstudio/debugger/introducing-spy-increment?view=vs-2022
如果我理解正确的话,您想发送游戏密钥。
在这种情况下,我认为游戏没有从 Windows 消息队列中获取按键,它可能是使用 DirectX 或类似方式直接读取键盘状态。
请参阅类似的问题:
将击键发送到游戏
If i understand correctly, you want to send keys to a game.
In this case i think that the game is not fetching the keys from the Windows Message queue, it is probably reading the keyboard status directly using DirectX or similar ways.
See this similar question:
Send Key Strokes to Games
你不能像这样伪造消息并期望它可靠地工作 - 你所做的只是发送一条消息,没什么花哨的。
这意味着,如果目标程序以不同的方式测试输入(例如,通过直接检查按键状态、对不同的消息做出反应或只是使用完全不同的机制),则目标程序最多可能会完全忽略您的输入,并且最坏的情况是完全困惑。
使用 SendInput 函数(我不确定这是否作为.Net 框架与否)。
一些相当相关的有趣博客文章:
You can't fake messages like this and expect it to work reliably - all you are doing is sending a message, nothing fancy.
What this means is that if the target program tests for input in a different way (for example by directly checking the key state, reacting to different messages or just using a completely different mechanism) the target program at best may just completely ignore your input and at worst get totally confused.
Use the SendInput function instead (I'm not sure if this exposed as part of the .Net framework or not).
Some interesting blog articles which are fairly relevant:
通常,程序员会对
KeyUp
事件做出反应,而不是按下;您尝试发送 WM_KEYUP 吗?Often programmers will react to
KeyUp
events rather than down; did you try sending WM_KEYUP?您应该使用 SendInput 来模拟另一个窗口的输入。
有关更多详细信息,请参阅此博文。
You should be using SendInput to simulate input into another window.
See this blog post for more details.