1. You have to marshal the struct yourself.
You can do this by flatten the array and marshal the struct to an unmanaged
buffer, when calling SendInput you simply pass the pointer to the unmanaged
buffer as an IntPtr.
2. Also you need to be aware the the struct contains a union, so you need to
set the length of the longest element (the MOUSEINPUT struct) as third
argument.
3. You need to inject two structs ( one for keydown another for keyup) in
the stream.
Here's a working sample that sets numlock on.
class Tester
{
[StructLayout(LayoutKind.Sequential)]
public struct INPUT
{
public int type;
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
public int type1;
public short wVk1;
public short wScan1;
public int dwFlags1;
public int time1;
public IntPtr dwExtraInfo1;
}
[DllImport("user32.dll")]
static extern int SendInput(uint nInputs, IntPtr pInputs, int cbSize);
static void Main()
{
INPUT input = new INPUT();
input.type = 0x01; //INPUT_KEYBOARD
input.wVk = 0x90; //VK_NUMLOCK
input.wScan = 0;
input.dwFlags = 0; //key-down
input.time = 0;
input.dwExtraInfo = IntPtr.Zero;
input.type1 = 0x01;
input.wVk1 = 0x90;
input.wScan1= 0;
input.dwFlags1 = 2; //key-up
input.time1 = 0;
input.dwExtraInfo1 = IntPtr.Zero;
IntPtr pI = Marshal.AllocHGlobal(28);
Marshal.StructureToPtr(input, pI, false);
int result;
result = SendInput(1, pI, 28); //Hardcoded size of the MOUSEINPUT tag !!!
if (result == 0 || Marshal.GetLastWin32Error() != 0)
Console.WriteLine(Marshal.GetLastWin32Error());;
}
Willy.
"Tim" <Ti*@discussions.microsoft.com> wrote in message
news:CC**********************************@microsof t.com...
Hello All,
I am writing a program that checks for the NumLock status and turns the
NumLock on if it is off.
I'm not sure what I'm overlooking at this point, but the code will compile
and run, but the SendInput call always returns "0".
Here is some of the code:
Beginning of Code...
[DllImport("user32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT [] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
public struct INPUT
{
[FieldOffset(0)] public int type;
[FieldOffset(4)] public KEYBDINPUT ki;
}
public struct KEYBDINPUT
{
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}
...Code Deleted...
INPUT [] input = new INPUT [1];
input[0].ki.wVk = 0x90;
input[0].type = 0x01;
uint result;
result = SendInput(1, ref input, Marshal.SizeOf(input));
...End of Code
result will always equal "0". Does anyone know what would cause this or,
even better, how to get this to work? I'm at a complete loss.
Thank you in advance for your help,
-Tim