473,513 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declare a user32.dll function to accept an Array

I'm successfully using SendInput to emulate mouse events
for a legacy app, delcared like so:

[DllImport("User32.dll")]
public extern static int SendInput( int theCount, ref
INPUT pInputs, int theSize );

From the MSDN docs, SendInput appears to take an array of
inputs:

"pInputs
[in] Pointer to an array of INPUT structures. Each
structure represents an event to be inserted into the
keyboard or mouse input stream."

However I'm not sure how to pass it in managed C#. I
recklessly made an attempt by simply changing the
declaration to an array (as below). And as I suspected it
does *not* work when I pass in a valid array of INPUT
structs.

[DllImport("User32.dll")]
public extern static int SendInput( int theCount, ref INPUT
[] pInputs, int theSize );

I'm wondering:
a) How to declare the external function correctly
b) How to create and pass my INPUT structs array.
c) I'd like to know the why of the internals? I.E. Does it
not work because .NET is also marshalling the 4 bytes of
length info at the begining of the array?

Thanks
Nov 13 '05 #1
1 6962
Richard,
a) How to declare the external function correctly
Just remove the ref keyword.

[DllImport("User32.dll")]
public extern static int SendInput( int theCount, INPUT[] pInputs, int
theSize );

b) How to create and pass my INPUT structs array.
Just like you would when calling a managed method.

c) I'd like to know the why of the internals? I.E. Does it
not work because .NET is also marshalling the 4 bytes of
length info at the begining of the array?


An array is a reference type, and when you pass an array by value to
unmanaged code it will get a pointer to the first item. Adding the ref
modifier adds another level of indirection, so the unmanaged code gets
a pointer to a pointer to an INPUT struct.

You may want to look at this MSDN TV episode for more information

http://msdn.microsoft.com/msdntv/epi...K/manifest.xml

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 13 '05 #2

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

Similar topics

3
14910
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
7
5065
by: Vaca Louca | last post by:
Hello, My setup: Debian sarge on dual Pentium 4. g++ 3.3.5-3. (the other system is Windows XP with MS Visual Studio .NET 2003) I have an auto_array<T> template (based on a template taken from...
5
2868
by: Ingo Brueckl | last post by:
I need to declare a fixed array of (already defined and working) sort functions (sortfunc1, sortfunc2, sortfunc3) to pass one element of the array to qsort itself: int index; ??? func =...
2
12526
by: Lance Geeck | last post by:
I have many items that I lifted off from Microsoft's website several years ago. These samples were in VB6. I now want to convert an application to VB.NET. I am getting an error that says "As Any...
8
14463
by: John Dann | last post by:
Trying to declare a structure that will contain a couple of fixed-size arrays. I'm trying eg: Structure IndexRecord Dim testarray(16) as Byte etc End Structure But vb.net is refusing to let...
3
1690
by: Just Me | last post by:
I declare the following and use it if the msg returns values. I also declare an SendMessage using ByVal for other calls. Isn't there a way I can used the SendMessage and in the call tell it to...
6
2114
by: **Developer** | last post by:
Notice below I sometimes used the "A" version. I found by cut-and-try that only the "A" version would work correctly. Anyone have a suggestion of why the "W" version would not work correctly? ...
6
3318
by: CJ | last post by:
Functions can accept "argv like" variable definitions, i.e. foo(int argc, char *argv), which was defined/initialized in the C start up stubs, but we can't declare one for our own use. Example: ...
6
2136
by: pallavi27 | last post by:
HI, i want to declare an array that should accept any size given by the user.how to declare such array?please explain me with the help of a code..it should display on the screen "enter the no of...
0
7260
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
7384
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,...
0
7539
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...
0
7525
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...
0
5686
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,...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
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...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.