473,796 Members | 2,520 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SendInput Always Returns "0"

Tim
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("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT [] pInputs, int cbSize);

[StructLayout(La youtKind.Explic it)]
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
Nov 16 '05 #1
5 5109
Haven't done this since the DOS dayzzzz!!!!

Anyway, I think there is an easier way to do this in .NET --> Lookup "Keys Enumeration" in MSDN. NumLock etal are all listed and there must be a sample somewhere that shows how to poke/peek NumLock state... {Remember that NumLock behaves like CapsLock and ScrollLock so sample may be for any of those...

--Richard

"Tim" wrote:
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("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT [] pInputs, int cbSize);

[StructLayout(La youtKind.Explic it)]
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

Nov 16 '05 #2
I don't do much with P/Invoke, but the definition of SendInput() includes a
LPINPUT (pointer-to-INPUT) for the second parameter. I would think you could
either declare this as:

[DllImport("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInput, int cbSize);

or

[DllImport("user 32.dll")]
static extern uint SendInput(uint nInputs, INPUT [] pInputs, int cbSize);

depending on whether you intend to pass one or more INPUT items. Either of
the above functions should pass a pointer-to-INPUT to the API. By using "ref
INPUT[]" you're creating a pointer-to-pointer-to-INPUT parameter, which
could certainly cause problems.

Again, that's just a guess -- I haven't tried this so I don't know if it
works or not.

Ken
"Tim" <Ti*@discussion s.microsoft.com > wrote in message
news:CC******** *************** ***********@mic rosoft.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("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT [] pInputs, int cbSize);
[StructLayout(La youtKind.Explic it)]
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

Nov 16 '05 #3
By the way, don't you get an exception when you call Marshal.SizeOf( input)?
The variable "input" is an array, not a struct type, so the call should
fail.

Ken
"Ken Kolda" <ke*******@elli emae-nospamplease.co m> wrote in message
news:%2******** ********@TK2MSF TNGP09.phx.gbl. ..
I don't do much with P/Invoke, but the definition of SendInput() includes a LPINPUT (pointer-to-INPUT) for the second parameter. I would think you could either declare this as:

[DllImport("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT pInput, int cbSize);

or

[DllImport("user 32.dll")]
static extern uint SendInput(uint nInputs, INPUT [] pInputs, int cbSize);

depending on whether you intend to pass one or more INPUT items. Either of
the above functions should pass a pointer-to-INPUT to the API. By using "ref INPUT[]" you're creating a pointer-to-pointer-to-INPUT parameter, which
could certainly cause problems.

Again, that's just a guess -- I haven't tried this so I don't know if it
works or not.

Ken
"Tim" <Ti*@discussion s.microsoft.com > wrote in message
news:CC******** *************** ***********@mic rosoft.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("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT [] pInputs, int cbSize);

[StructLayout(La youtKind.Explic it)]
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


Nov 16 '05 #4
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(La youtKind.Sequen tial)]
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("user 32.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.dwExtraIn fo = IntPtr.Zero;

input.type1 = 0x01;
input.wVk1 = 0x90;
input.wScan1= 0;
input.dwFlags1 = 2; //key-up
input.time1 = 0;
input.dwExtraIn fo1 = IntPtr.Zero;

IntPtr pI = Marshal.AllocHG lobal(28);
Marshal.Structu reToPtr(input, pI, false);
int result;
result = SendInput(1, pI, 28); //Hardcoded size of the MOUSEINPUT tag !!!

if (result == 0 || Marshal.GetLast Win32Error() != 0)
Console.WriteLi ne(Marshal.GetL astWin32Error() );;
}

Willy.

"Tim" <Ti*@discussion s.microsoft.com > wrote in message
news:CC******** *************** ***********@mic rosoft.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("user 32.dll")]
static extern uint SendInput(uint nInputs, ref INPUT [] pInputs, int
cbSize);

[StructLayout(La youtKind.Explic it)]
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

Nov 16 '05 #5
Small typeo in my previous post.

result = SendInput(2, pI, 28); // two structs

Willy.
Nov 16 '05 #6

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

Similar topics

5
8230
by: build | last post by:
G'day All, I have a problem with this loop. There are a number of .txt files in 'myPath'. tmpFile = Dir(myPath & "\*.txt") 'PROCESS FOLDER Do Until tmpFile = "" <lottsa code> <too much to post> tmpFile = Dir
10
4739
by: comp.lang.php | last post by:
echo mime_content_type('/var/www/html/video/small.mov'); // 1.5mb Quicktime video returns "video/quicktime" echo mime_content_type('/var/www/html/video/huge.mov'); // 10.5mb Quicktime video returns "text/plain" Environment: PHP 5.0.4 - Windows XP --enable-mime-magic PHP 4.3.11 - Fedora Core 4 --enable-mime-magic
1
1971
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 the SendInput call always returns "0". Here is some of the code: Beginning of Code... static extern uint SendInput(uint nInputs, ref INPUT pInputs, int cbSize);
5
2873
by: Dan C Douglas | last post by:
I have just installed VS.NET 2003 on my computer. I have a project that I have been developing on VS.NET 2002. I haven't upgraded this project to VS.NET 2003 yet and I am still developing it in VS.NET 2002. When I am putting values from my SQLDataReader into labels and text boxes I am getting results such as 400.0000, or 25.90. For example... ? dr("CostsPerBin") returns
7
6898
by: Jim Carlock | last post by:
Does a SELECT element (listbox) need to be inside a FORM element? The code I'm playing with: <?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
9
10750
by: Klaus Johannes Rusch | last post by:
IE7 returns "unknown" instead of "undefined" when querying the type of an unknown property of an object, for example document.write(typeof window.missingproperty); Has "unknown" been defined as a valid return value for the typeof operator in a later version of ECMAScript or is this a JScript "feature"? -- Klaus Johannes Rusch
2
2169
by: Phillip Vong | last post by:
VS2005 on a SQL 2000 Please help me understand how to fix this problem. In SQL, a column is assigned Data Type of nvarchar(250). In each row, the value looks like this. FirstName LastName "SQARE BOX" a12720 "SQUARE BOX" 14 Meadow Road The "SqareBox" shapes are Returns from the keyboard. When I'm doing a Details View, all this information runs together with just
5
5725
by: yogeshmk | last post by:
I'm writing an application which is required to function with many languages and also compile on Linux & windows. Here's how I find the locale .. # include <stdio.h> # include <locale.h> int main(void) {
2
3048
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, the topic says all. I use this code from C# to call the NtQuerySystemInformation. When i call NtQuerySystemInformation the first time with zero buffer length to get the buffer size needed for the information i get a STATUS_INFO_LENGTH_MISMATCH which is -1073741820 in number, this is ok, but why does the function not store the required
0
9529
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10457
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10231
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9054
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6792
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.