以下是我用于通过SendInput API模拟按键的一些代码的摘录。如果我将我的应用程序设置为为x86 CPU编译,但这对x64 CPU编译不起作用,则此方法可以正常工作。
我猜它有一些东西,因为x64使用双倍大小的指针,但我试图将此[FieldOffset(4)]
更改为此[FieldOffset(8)]
,但它不起作用。
它是否与导入32位版本的user32.dll的事实有关?
#region SendInput API
[DllImport("user32.dll", EntryPoint = "SendInput", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
[DllImport("user32.dll", EntryPoint = "GetMessageExtraInfo", SetLastError = true)]
static extern IntPtr GetMessageExtraInfo();
private enum KeyEvent
{
KeyUp = 0x0002,
KeyDown = 0x0000,
ExtendedKey = 0x0001
}
private struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public long time;
public uint dwExtraInfo;
};
[StructLayout(LayoutKind.Explicit, Size = 28)]
private struct INPUT
{
[FieldOffset(0)]
public uint type;
[FieldOffset(4)]
public KEYBDINPUT ki;
};
#endregion
public void sendKey(KeyCode Key)
{
INPUT[] InputList = new INPUT[2];
INPUT keyInput = new INPUT();
keyInput.type = 1;
keyInput.ki.wScan = 0;
keyInput.ki.time = 0;
keyInput.ki.dwFlags = (int)KeyEvent.KeyDown;
keyInput.ki.dwExtraInfo = (uint)GetMessageExtraInfo();
keyInput.ki.wVk = (ushort)Key;
InputList[0] = keyInput;
keyInput.ki.dwFlags = (int)KeyEvent.KeyUp;
InputList[1] = keyInput;
SendInput((uint)2, InputList, Marshal.SizeOf(InputList[0]));
}
答案 0 :(得分:12)
除了SLaks发现的错误之外,您剩下的问题是INPUT
的大小不正确。这意味着SendInput
失败,因为它收到INPUT[]
类型的参数。您无法使用StructLayout(LayoutKind.Explicit, Size = 28)
指定大小,因为您需要同时处理x86和x64的代码。
这一切都源于您只在KEYBRDINPUT
中包含INPUT
结构的事实。 MOUSEINPUT
结构大于KEYBRDINPUT
,这是导致问题的原因。
最佳解决方案是正确定义INPUT结构,包括联合部分。这样做(从pinvoke.net获取声明)。
[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.Sequential)]
struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
[StructLayout(LayoutKind.Explicit)]
struct MouseKeybdHardwareInputUnion
{
[FieldOffset(0)]
public MOUSEINPUT mi;
[FieldOffset(0)]
public KEYBDINPUT ki;
[FieldOffset(0)]
public HARDWAREINPUT hi;
}
[StructLayout(LayoutKind.Sequential)]
struct INPUT
{
public uint type;
public MouseKeybdHardwareInputUnion mkhi;
}
答案 1 :(得分:5)
dwExtraInfo
是一个指针
因此,它需要在32位代码中为4字节宽,在64位代码中为8字节。
要在C#中执行此操作,请使用IntPtr
(不是uint
,总是4个字节)