C# 使用 PostMessage

发布于 2024-11-27 14:06:38 字数 838 浏览 0 评论 0原文

我正在尝试向应用程序发送密钥。我使用断点测试了 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 技术交流群。

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

发布评论

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

评论(5

红衣飘飘貌似仙 2024-12-04 14:06:38

请注意,PostMessage 的正确导入是:

[DllImport("user32.dll")]
static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

像您所做的那样发送密钥可能不起作用,但您可以使用间谍++来捕获由 Windows 发布到应用程序的实际消息并使用这些消息。

https://learn.microsoft。 com/en-us/visualstudio/debugger/introducing-spy-increment?view=vs-2022

Just note that the correct import of PostMessage is:

[DllImport("user32.dll")]
static extern bool PostMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

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

陌上芳菲 2024-12-04 14:06:38

如果我理解正确的话,您想发送游戏密钥。
在这种情况下,我认为游戏没有从 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

你对谁都笑 2024-12-04 14:06:38

你不能像这样伪造消息并期望它可靠地工作 - 你所做的只是发送一条消息,没什么花哨的。

这意味着,如果目标程序以不同的方式测试输入(例如,通过直接检查按键状态、对不同的消息做出反应或只是使用完全不同的机制),则目标程序最多可能会完全忽略您的输入,并且最坏的情况是完全困惑。

使用 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:

固执像三岁 2024-12-04 14:06:38

通常,程序员会对 KeyUp 事件做出反应,而不是按下;您尝试发送 WM_KEYUP 吗?

Often programmers will react to KeyUp events rather than down; did you try sending WM_KEYUP?

痕至 2024-12-04 14:06:38

您应该使用 SendInput 来模拟另一个窗口的输入。

有关更多详细信息,请参阅博文。

You should be using SendInput to simulate input into another window.

See this blog post for more details.

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