PostMessage在Windows 7 x64中不起作用

时间:2011-07-09 22:31:31

标签: c# windows-7 64-bit sendmessage postmessage

我需要在游戏窗口中模拟按键。我尝试发送密钥“A”,但它不起作用:

[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 ;-(
}

1 个答案:

答案 0 :(得分:4)

你得到什么错误?游戏是以管理员身份运行的吗?您可能被UIPI阻止。