Windows API Fails to Create “Input Messages”/events for Certain Applications (i.e. Mozilla Firefox)

StackOverflow https://stackoverflow.com/questions/8751607

I'm attempting to emulate user clicking and mouse-moving, specifically in Mozilla Firefox, in a Windows 7 environment. The solution I frankensteined together from various tutorials, forum posts, and MSDN documents works for 99% of Windows Applications out there, but it won't work for Firefox 8.0.

From my preliminary research, the most accurate (low-level) way of emulating keyboard and mouse input in Windows is to use the SendInput function from the User32.dll Windows Library. To test this, I wrote a short C# program to loop through and make a call to SendInput that creates a programmatic mouse-click every 5 seconds, no matter where the mouse cursor is on screen.

Once running, the program perfectly emulates a mouseclick for almost every application window I switch focus to, even including the Windows interface itself (Start Button, Taskbar, Windows Explorer, etc.), but no programmatic mouse-clicks occur when I bring the cursor into a Mozilla Firefox Window.

In order to get a better handle on what was happening under the hood, I booted up Microsoft Spy++ and began inspecting what messages were actually getting passed to the Firefox window's message queue. Sure enough, the Firefox window would receive no messages even though my cursor would be situated directly above it while it had focus. When I manually clicked my mouse, the Firefox Spy++ listener would then go nuts and display the full "nHittest:HTCLIENT wMouseMsg:WM_LBUTTONDOWN" that I saw when observing other applications' correct responses to my emulation program.

Can anyone provide an explanation for how/why Mozilla Firefox is one of the only applications that does not even receive any messages at all from the SendInput function and perhaps a suggestion for how to overcome this?

Source Code (Imports/external references removed for clarity):

static void Main(string[] args)
{
    for (; ; )
    {
        System.Threading.Thread.Sleep(5000);
        INPUT[] inp = new INPUT[2];
        inp[0].type = INPUT_MOUSE;
        inp[0].mi = createMouseInput(0, 0, 0, 0, MOUSEEVENTF_LEFTDOWN);
        inp[1].type = INPUT_MOUSE;
        inp[1].mi = createMouseInput(0, 0, 0, 0, MOUSEEVENTF_LEFTUP);
        SendInput((uint)inp.Length, inp, Marshal.SizeOf(inp[0].GetType()));
    }
}

private static MOUSEINPUT createMouseInput(int x, int y, uint data, uint t, uint flag)
{
    MOUSEINPUT mi = new MOUSEINPUT();
    mi.dx = x;
    mi.dy = y;
    mi.mouseData = data;
    mi.time = t;
    mi.dwFlags = flag;
    return mi;
}

[StructLayout(LayoutKind.Sequential)]
struct MOUSEINPUT
{
    public int dx;
    public int dy;
    public uint mouseData;
    public uint dwFlags;
    public uint time;
    public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Explicit)]
private struct INPUT
{
    [FieldOffset(0)]
    public int type;
    [FieldOffset(sizeof(int))] //[FieldOffset(8)] for x64
    public MOUSEINPUT mi;
    [FieldOffset(sizeof(int))] //[FieldOffset(8)] for x64
    public KEYBDINPUT ki;
    [FieldOffset(sizeof(int))] //[FieldOffset(8)] for x64
    public HARDWAREINPUT hi;
}
有帮助吗?

解决方案

Did you run your program as an administrator? It could be an UIPI, i.e. integrity level issue.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top