简体   繁体   English

C# 使用 PostMessage

[英]C# Using PostMessage

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.我测试了使用断点的 Handlewindow 值以了解我在做什么,但我找不到解决方案。 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.就像我说的,它可以成功发送 1000000 次,但没有任何反应。 Is there another way how I can send keys to an Windows application that works minimized or even hidden?还有另一种方法可以将密钥发送到最小化甚至隐藏的 Windows 应用程序吗? It should be only send to my app.它应该只发送到我的应用程序。

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. 在这种情况下,我认为游戏没有从Windows消息队列中获取键,它可能是直接使用DirectX或类似方式读取键盘状态。

See this similar question: 看到类似的问题:

Send Key Strokes to Games 向游戏发送按键

Often programmers will react to KeyUp events rather than down; 程序员通常会对KeyUp事件做出反应,而不是做出反应。 did you try sending WM_KEYUP? 您是否尝试发送WM_KEYUP?

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). 请改用SendInput函数(我不确定这是否作为.Net框架的一部分公开)。

Some interesting blog articles which are fairly relevant: 一些非常相关的有趣博客文章:

You should be using SendInput to simulate input into another window. 您应该使用SendInput模拟到另一个窗口的输入。

See this blog post for more details. 请参见博客帖子的更多细节。

Just note that the correct import of PostMessage is:请注意, PostMessage的正确导入是:

[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.像您一样发送密钥可能不起作用,但您可以使用 spy++ 捕获 windows 发布到应用程序的实际消息并使用它们。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM