473,386 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

SendInput just doesn't work.

Hi. I need to simulate keyboard and mouse events. For the first step,
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.

Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.

PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.

--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;

INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;

INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
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
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}

Aug 9 '07 #1
7 12951
While wating for a reply from kind people, I tried myself that code in
native C++ code.

#include "stdafx.h"
#include "windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwExtraInfo=0;
mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
mi.mouseData=0;
INPUT pInputs[1];
pInputs[0].mi=mi;
pInputs[0].type = INPUT_MOUSE;
SendInput(1,pInputs,sizeof(INPUT));
return 0;
}
It just worked fine. Why the C# code below doesn't work?
Please give me a hint. Thank you very much.
On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmail.comwrote:
Hi. I need to simulate keyboard and mouse events. For the first step,
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.

Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.

PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.

--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;

class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;

INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;

INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
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
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;

}

Aug 9 '07 #2
I have found out the reason. My Windows is 64bit version and the .NET
app becomes 64bit application. I changed the build option to x86, and
it worked.

Still don't know why the same API call fails if the .NET app is 64bit.
Do you know why?
On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmail.comwrote:
While wating for a reply from kind people, I tried myself that code in
native C++ code.

#include "stdafx.h"
#include "windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwExtraInfo=0;
mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
mi.mouseData=0;
INPUT pInputs[1];
pInputs[0].mi=mi;
pInputs[0].type = INPUT_MOUSE;
SendInput(1,pInputs,sizeof(INPUT));
return 0;}

It just worked fine. Why the C# code below doesn't work?
Please give me a hint. Thank you very much.

On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmail.comwrote:
Hi. I need to simulate keyboard and mouse events. For the first step,
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.
Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.
PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.
--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;
INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
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
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}

Aug 9 '07 #3
I have found out the reason. My Windows is 64bit version and the .NET
app becomes 64bit application. I changed the build option to x86, and
it worked.

Still don't know why the same API call fails if the .NET app is 64bit.
Do you know why?
On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmail.comwrote:
While wating for a reply from kind people, I tried myself that code in
native C++ code.

#include "stdafx.h"
#include "windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwExtraInfo=0;
mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
mi.mouseData=0;
INPUT pInputs[1];
pInputs[0].mi=mi;
pInputs[0].type = INPUT_MOUSE;
SendInput(1,pInputs,sizeof(INPUT));
return 0;}

It just worked fine. Why the C# code below doesn't work?
Please give me a hint. Thank you very much.

On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmail.comwrote:
Hi. I need to simulate keyboard and mouse events. For the first step,
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.
Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.
PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.
--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;
INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
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
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}

Aug 9 '07 #4
I have found out the reason. My Windows is 64bit version and the .NET
app becomes 64bit application. I changed the build option to x86, and
it worked.

Still don't know why the same API call fails if the .NET app is 64bit.
Do you know why?
On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmail.comwrote:
While wating for a reply from kind people, I tried myself that code in
native C++ code.

#include "stdafx.h"
#include "windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwExtraInfo=0;
mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
mi.mouseData=0;
INPUT pInputs[1];
pInputs[0].mi=mi;
pInputs[0].type = INPUT_MOUSE;
SendInput(1,pInputs,sizeof(INPUT));
return 0;}

It just worked fine. Why the C# code below doesn't work?
Please give me a hint. Thank you very much.

On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmail.comwrote:
Hi. I need to simulate keyboard and mouse events. For the first step,
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.
Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.
PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.
--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;
INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
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
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}

Aug 9 '07 #5
I have found out the reason. My Windows is 64bit version and the .NET
app becomes 64bit application. I changed the build option to x86, and
it worked.

Still don't know why the same API call fails if the .NET app is 64bit.
Do you know why?
On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmail.comwrote:
While wating for a reply from kind people, I tried myself that code in
native C++ code.

#include "stdafx.h"
#include "windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwExtraInfo=0;
mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
mi.mouseData=0;
INPUT pInputs[1];
pInputs[0].mi=mi;
pInputs[0].type = INPUT_MOUSE;
SendInput(1,pInputs,sizeof(INPUT));
return 0;}

It just worked fine. Why the C# code below doesn't work?
Please give me a hint. Thank you very much.

On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmail.comwrote:
Hi. I need to simulate keyboard and mouse events. For the first step,
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.
Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.
PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.
--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;
INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
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
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}

Aug 9 '07 #6
I have found out the reason. My Windows is 64bit version and the .NET
app becomes 64bit application. I changed the build option to x86, and
it worked.

Still don't know why the same API call fails if the .NET app is 64bit.
Do you know why?
On Aug 9, 4:13 pm, Sin Jeong-hun <typing...@gmail.comwrote:
While wating for a reply from kind people, I tried myself that code in
native C++ code.

#include "stdafx.h"
#include "windows.h"
int _tmain(int argc, _TCHAR* argv[])
{
MOUSEINPUT mi;
mi.dx=0;
mi.dy=0;
mi.dwExtraInfo=0;
mi.dwFlags=MOUSEEVENTF_ABSOLUTE|MOUSEEVENTF_MOVE;
mi.mouseData=0;
INPUT pInputs[1];
pInputs[0].mi=mi;
pInputs[0].type = INPUT_MOUSE;
SendInput(1,pInputs,sizeof(INPUT));
return 0;}

It just worked fine. Why the C# code below doesn't work?
Please give me a hint. Thank you very much.

On Aug 9, 2:05 pm, Sin Jeong-hun <typing...@gmail.comwrote:
Hi. I need to simulate keyboard and mouse events. For the first step,
I tried to move mouse using SendInput. But it didn't work. I searched
all over the internet for an example, but only questions were there.
Please give me what was wrong in the following code. I simplified the
code so that you can just copy it to a console app project, and run
it. Thank you for any help.
PS: I need to work with SendInput the way it is, not SendMessage or
modified INPUT structure, because there are a lot of other things to
do with SendInput.
Thank you.
--------Code (Program.cs)-------------
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
class Program
{
static void Main(string[] args)
{
MOUSEINPUT m = new MOUSEINPUT();
m.dx = 0;
m.dy = 0;
m.mouseData = 0;
m.time = 0;
m.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_MOVE;
INPUT i = new INPUT();
i.type = INPUT_MOUSE;
i.mi = m;
INPUT[] inputs = new INPUT[] { i };
int isize = Marshal.SizeOf(i);
SendInput(1, inputs, isize);
}
[DllImport("user32.dll", SetLastError = true)]
static extern uint SendInput(uint nInputs, INPUT[] pInputs, int
cbSize);
[StructLayout(LayoutKind.Explicit)]
struct INPUT
{
[FieldOffset(0)]
public int type;
[FieldOffset(4)]
public MOUSEINPUT mi;
[FieldOffset(4)]
public KEYBDINPUT ki;
[FieldOffset(4)]
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
{
ushort wVk;
ushort wScan;
uint dwFlags;
uint time;
IntPtr dwExtraInfo;
}
[StructLayout(LayoutKind.Sequential)]
struct HARDWAREINPUT
{
uint uMsg;
ushort wParamL;
ushort wParamH;
}
const uint MOUSEEVENTF_MOVE = 0x0001;
const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
const uint MOUSEEVENTF_LEFTUP = 0x0004;
const uint MOUSEEVENTF_RIGHTDOWN = 0x0008;
const uint MOUSEEVENTF_RIGHTUP = 0x0010;
const uint MOUSEEVENTF_MIDDLEDOWN = 0x0020;
const uint MOUSEEVENTF_MIDDLEUP = 0x0040;
const uint MOUSEEVENTF_XDOWN = 0x0080;
const uint MOUSEEVENTF_XUP = 0x0100;
const uint MOUSEEVENTF_WHEEL = 0x0800;
const uint MOUSEEVENTF_VIRTUALDESK = 0x4000;
const uint MOUSEEVENTF_ABSOLUTE = 0x8000;
const int INPUT_MOUSE = 0;
const int INPUT_KEYBOARD = 1;
const int INPUT_HARDWARE = 2;
}

Aug 9 '07 #7
>Still don't know why the same API call fails if the .NET app is 64bit.
>Do you know why?
I believe all the FieldOffset(4) must be FieldOffset(8) when running
as a 64-bit app.

Since you're only using the MOUSEINPUT part of the union, and it
happens to be the biggest one of the three, you can remove the other
two and change the declaration of INPUT to

struct INPUT
{
public int type;
public MOUSEINPUT mi;
}

That should hopefully make the code work on both 32-bit and 64-bit.

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Aug 9 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matthew Kelly | last post by:
I have pulled together a VB.net project that hooks the keyboard (Ref. Paul Kimmel's hooking program) and allow the user to send "mouse right clicks" via the SendInpuut function (mouse emulation...
6
by: Richard A. Lowe | last post by:
I'm using P/Invoke to call SendInput (using code culled from these newsgroups!) to send mouse events to a window. But I'm unsure how to send double-clicks. A VB6 article I saw on SendInput...
5
by: Tim | last post by:
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...
1
by: Tim | last post by:
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...
0
by: fisj | last post by:
Has anyone managed to get the sendinput api to work with vb.net? I've tried converting the vb6 code from allapi.net, but its proving extremely difficult. Example and def is here: ...
2
by: Louise | last post by:
Hi, Can anyone send me a working example of using the sendinput API with VB.NET to send keystrokes and mouse commands to other active windows? I’ve been struggling with this for ages and just...
7
by: aam | last post by:
I'm trying to find a code sample on how to use the SendInput Function to simulate a mouse click on a button of another form. I've looked all over and can't find one. Thanks.
1
by: kumar_subrahmanya | last post by:
Hi, I am facing problems in sending mouse clicks via SendInput API. Mouse clicks are being sent but at the X,Y co-ordinates. I am mapping my monitor to the (0,0,65535,65535) virtual monitor as...
2
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
I am trying to find a good example of SendInput. Doing a search on google I found two, one is incomplete and vague from the start and the other I copied the code and tried running it and it was...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.