简体   繁体   中英

PostMessage does not work in Windows 7 x64

I need to simulate a keypress in game window. I try to send key "A", but it don't work:

[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);

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

private void button1_Click(object sender, EventArgs e)
{
    IntPtr hWnd = FindWindow(null, "Game Name");  // it's work!

    if (hWnd == IntPtr.Zero)
    {
        MessageBox.Show("Game is not running");
        return;
    }

    SetForegroundWindow(hWnd);  // it's work too and now I have active window of game
    System.Threading.Thread.Sleep(3000);

    const int WM_KEYDOWN = 0x0100;
    PostMessage(hWnd, WM_KEYDOWN, (IntPtr)Keys.A, IntPtr.Zero);     // don't work ;-(
}

What error do you get? Is the game running as administrator? You could be blocked by UIPI .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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