由于某种原因,SendInput() Pinvoke 不起作用。SendInput() 总是返回 0,我试图以管理员身份运行它,仍然没有任何变化。它在 C++ 中完美运行。我一直在寻找几个小时,请帮助我。我正在运行 Windows 8 64x 位计算机。也许它与我的操作系统有关?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
[DllImport("User32.dll", SetLastError = true)]
public static extern int SendInput(int nInputs, INPUT[] inputs, int cbSize);
public struct INPUT
{
public int type;
public KEYBDINPUT ki;
}
private static KEYBDINPUT createKeybdInput(ushort wVK, uint flag)
{
KEYBDINPUT i = new KEYBDINPUT();
i.wVk = wVK;
i.wScan = 0;
i.time = 0;
i.dwExtraInfo = IntPtr.Zero;
i.dwFlags = flag;
return i;
}
[StructLayout(LayoutKind.Sequential)]
public struct KEYBDINPUT
{
public ushort wVk;
public ushort wScan;
public uint dwFlags;
public uint time;
public IntPtr dwExtraInfo;
}
public static void sim_type()
{
INPUT[] inp = new INPUT[2];
inp[0].type = 1;
inp[0].ki = createKeybdInput(0x41, 0x0001);
inp[1].type = 1;
inp[1].ki = createKeybdInput(0x41, 0x0002);
if (SendInput(2, inp, Marshal.SizeOf(typeof(INPUT))) == 0)
{
Console.WriteLine("error");
}
}
static void Main(string[] args)
{
Console.ReadLine();
sim_type();
Console.ReadLine();
}
}
}
谢谢!