T
Tony_rivermsfly
Guest
The following is a complete SendInput example ready for test. Just simply create a console project and paste the code. When running, press space key and if SendInput works then two 'a's will be printed on the console; otherwise nothing will appear.
My problem is this code works only when compiled with x86 option (Right click the project root in the Solution Explorer, click Properties, and select Build tab. The problem is about a combobox with 4 options : Any CPU, x64, x86, Itanium). I want to get the code to work also with x64 option. If anyone could help me. Thanks a lot!
By the way, I'm using vs2010, win7 x64, uac turned off
using System;
using System.Runtime.InteropServices;
namespace SETest
{
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT
{
public ushort wVk;
public short wScan;
public uint dwFlags;
public int time;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public uint dwFlags;
public int time;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
[StructLayout(LayoutKind.Explicit)]
internal struct INPUT
{
[FieldOffset(0)]
public uint type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
internal static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
static void Main(string[] args)
{
Console.ReadKey();
INPUT[] keyInput = new INPUT[4];
keyInput[0].type = 1;
keyInput[1].type = 1;
keyInput[2].type = 1;
keyInput[3].type = 1;
KEYBDINPUT[] key = new KEYBDINPUT[4];
key[0].wVk = 65;
key[0].wScan = 65;
key[0].dwFlags = 0;
key[1].wVk = 65;
key[1].wScan = 65;
key[1].dwFlags = 0;
key[2].wVk = 65;
key[2].wScan = 65;
key[2].dwFlags = 0x02;
key[3].wVk = 65;
key[3].wScan = 65;
key[3].dwFlags = 0x02;
keyInput[0].ki = key[0];
keyInput[1].ki = key[1];
keyInput[2].ki = key[2];
keyInput[3].ki = key[3];
SendInput(4, keyInput, Marshal.SizeOf(typeof(INPUT)));
Console.ReadKey();
Console.ReadKey();
Console.ReadKey();
}
}
}
Continue reading...
My problem is this code works only when compiled with x86 option (Right click the project root in the Solution Explorer, click Properties, and select Build tab. The problem is about a combobox with 4 options : Any CPU, x64, x86, Itanium). I want to get the code to work also with x64 option. If anyone could help me. Thanks a lot!
By the way, I'm using vs2010, win7 x64, uac turned off
using System;
using System.Runtime.InteropServices;
namespace SETest
{
[StructLayout(LayoutKind.Sequential)]
internal struct KEYBDINPUT
{
public ushort wVk;
public short wScan;
public uint dwFlags;
public int time;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct MOUSEINPUT
{
public int dx;
public int dy;
public int mouseData;
public uint dwFlags;
public int time;
public int dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
internal struct HARDWAREINPUT
{
public int uMsg;
public short wParamL;
public short wParamH;
}
[StructLayout(LayoutKind.Explicit)]
internal struct INPUT
{
[FieldOffset(0)]
public uint type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
public HARDWAREINPUT hi;
}
class Program
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)]
internal static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
static void Main(string[] args)
{
Console.ReadKey();
INPUT[] keyInput = new INPUT[4];
keyInput[0].type = 1;
keyInput[1].type = 1;
keyInput[2].type = 1;
keyInput[3].type = 1;
KEYBDINPUT[] key = new KEYBDINPUT[4];
key[0].wVk = 65;
key[0].wScan = 65;
key[0].dwFlags = 0;
key[1].wVk = 65;
key[1].wScan = 65;
key[1].dwFlags = 0;
key[2].wVk = 65;
key[2].wScan = 65;
key[2].dwFlags = 0x02;
key[3].wVk = 65;
key[3].wScan = 65;
key[3].dwFlags = 0x02;
keyInput[0].ki = key[0];
keyInput[1].ki = key[1];
keyInput[2].ki = key[2];
keyInput[3].ki = key[3];
SendInput(4, keyInput, Marshal.SizeOf(typeof(INPUT)));
Console.ReadKey();
Console.ReadKey();
Console.ReadKey();
}
}
}
Continue reading...