473,787 Members | 2,938 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("User 32.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("User 32.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 6973
Richard,
a) How to declare the external function correctly
Just remove the ref keyword.

[DllImport("User 32.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
14952
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
7
5086
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 the Corona project hosted at SourceForge) which basically wants to implement std::auto_ptr<T> semantics for an array.
5
2884
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 = {sortfunc1, sortfunc2, sortfunc3}; index = /* calculated somehow */; qsort(what, what_elements, sizeof(what_element), func);
2
12556
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 is not supported in a declare statement" during the automated Conversion process. Some examples of this are: Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory"(ByRef
8
14480
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 me do this saying that I can't declare a structure with fixed size arrays. Yet the book I'm looking at shows
3
1703
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 use ByRef? Public Declare Auto Function SendMessageRef Lib "user32.dll" Alias "SendMessage" (ByVal hWnd As IntPtr, ByVal msg As Integer, ByRef wParam As Integer, ByRef lParam As Integer) As Integer
6
2134
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? One reason is that the ByRef or ByVal does not show by InteliSence so the "p" would help there. ------ Secondly, I'd like to be consistent with the parameter names. I'd like to
6
3348
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: foo(char *args) { /* implementation excluded }; // ok int main(int argc,char *argv) {
6
2151
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 rows" and" enter the no of columns" and it should accept any size specified by the user.
0
9655
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
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
10172
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...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9964
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
8993
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...
1
7517
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5398
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3670
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.