首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SendInput在64位上失败

SendInput在64位上失败
EN

Stack Overflow用户
提问于 2012-01-22 17:02:04
回答 1查看 6.8K关注 0票数 5

可能重复:

SendInput and 64bits

我使用的是来自SendInput的.NET代码(PInvoke)。

代码过去在32位操作系统上运行良好,但现在在WIN7 SendInput上返回0,最后一个错误被设置为57 (ERROR_INVALID_PARAMETER)。

我无法将代码编译为x86,因为我被加载到64位主机中。此外,我尝试了关于结构大小和字段偏移的各种解决方案,但都没有效果。

这些是我的PInvoke导入和类型:

代码语言:javascript
复制
[StructLayout(LayoutKind.Sequential)]
struct KEYBOARD_INPUT
{
    public uint type;
    public ushort vk;
    public ushort scanCode;
    public uint flags;
    public uint time;
    public uint extrainfo;
    public uint padding1;
    public uint padding2;
}

[DllImport("User32.dll", SetLastError=true)]
private static extern uint SendInput(
    uint numberOfInputs, 
   [MarshalAs(UnmanagedType.LPArray, SizeConst = 1)] KEYBOARD_INPUT[] input,
   int structSize);

代码使用情况是:

代码语言:javascript
复制
uint result = SendInput(
       (uint)inputs.Count, 
       inputs.ToArray(), 
       Marshal.SizeOf(inputs[0]));

其中输入数组包含一个KEYBOARD_INPUT结构。

这将产生结果= 0,当我检查最后一个错误时,最后一个错误被设置为57 (ERROR_INVALID_PARAMETER,参数不正确)。

在WIN7 64位操作系统中,是否有一种在64位主机下工作的方法?这适用于XP..。

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-22 17:43:21

尝试使用以下定义(由pinvoke.net提供):

代码语言:javascript
复制
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
const uint KEYEVENTF_EXTENDEDKEY = 0x0001;
const uint KEYEVENTF_KEYUP = 0x0002;
const uint KEYEVENTF_UNICODE = 0x0004;
const uint KEYEVENTF_SCANCODE = 0x0008;

struct INPUT
{
    public int type;
    public InputUnion u;
}

[StructLayout(LayoutKind.Explicit)]
struct InputUnion
{
    [FieldOffset(0)]
    public MOUSEINPUT mi;
    [FieldOffset(0)]
    public KEYBDINPUT ki;
    [FieldOffset(0)]
    public HARDWAREINPUT hi;
}

[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
{
    /*Virtual Key code.  Must be from 1-254.  If the dwFlags member specifies KEYEVENTF_UNICODE, wVk must be 0.*/
    public ushort wVk;
    /*A hardware scan code for the key. If dwFlags specifies KEYEVENTF_UNICODE, wScan specifies a Unicode character which is to be sent to the foreground application.*/
    public ushort wScan;
    /*Specifies various aspects of a keystroke.  See the KEYEVENTF_ constants for more information.*/
    public uint dwFlags;
    /*The time stamp for the event, in milliseconds. If this parameter is zero, the system will provide its own time stamp.*/
    public uint time;
    /*An additional value associated with the keystroke. Use the GetMessageExtraInfo function to obtain this information.*/
    public IntPtr dwExtraInfo;
}

[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
    public uint uMsg;
    public ushort wParamL;
    public ushort wParamH;
}

[DllImport("user32.dll")]
static extern IntPtr GetMessageExtraInfo();

[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

然后,在客户端代码中使用:

代码语言:javascript
复制
INPUT[] inputs = new INPUT[]
{
    new INPUT
    {
        type = INPUT_KEYBOARD,
        u = new InputUnion
        {
            ki = new KEYBDINPUT
            {
                wVk = key,
                wScan = 0,
                dwFlags = 0,
                dwExtraInfo = GetMessageExtraInfo(),
            }
        }
    }
};

SendInput((uint)inputs.Length, inputs, Marshal.SizeOf(typeof(INPUT)));
票数 14
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8962850

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档