importing the sendInput function from user32.lib

7 vues
Accéder directement au premier message non lu

Andrei Aron

non lue,
15 août 2002, 17:25:2815/08/2002
à
Hi, i have a question about importing C++ funnctions (windows.h) into C#
code.

I'm trying to use the following function, which is defined in user32.lib, in
the following way:

[System.Runtime.InteropServices.DllImportAttribute("user32.dll")]

private static extern UINT SendInput(
UINT nInputs, // count of input events
LPINPUT pInputs, // array of input events
int cbSize // size of structure
);

The only problem is that c# does not uunderstand UINT or LPINPUT.

Now, the UINT i can probably replace with a uint keyword (which C#
understands), but i have no idea what to do about the LPINPUT array of
INPUT structures, each of which contain a union.

There's also another problem. The documentation states that this function is
defined in user32.lib NOT user32.dll. Is my importing the dll instead of the
lib ok?

Sorry about posting this question, this is only my second try at importing a
c++ function out of a windows api dll.

I don't know where in the documentation to look for the answers to these
questions.

thanks,

Andrei

Mattias Sjögren

non lue,
15 août 2002, 18:05:4115/08/2002
à
Andrei,

>Now, the UINT i can probably replace with a uint keyword

Yep.


>but i have no idea what to do about the LPINPUT array of
>INPUT structures, each of which contain a union.

Do you actually need all the different kinds of input? If not, you can
skip the union, and only include the members for, say, KEYBDINPUT. You
also have to make sure that you fill with padding members if needed so
the union size if correct.

struct INPUT {
public int type;
// KEYBDINPUT members follows
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
// dummy members to correct size
private int _pad0, _pad1;
}

If you actually want to define a union and provide different kinds of
input, that's possible too. Use the StructLayout attribute on the
struct to set explicit layout, then the FieldOffset attribute on each
member to align them on the right offset, and make sure the different
INPUT struct members overlap.

struct KEYBDINPUT {
public short wVk;
public short wScan;
public int dwFlags;
public int time;
public IntPtr dwExtraInfo;
}

struct MOUSEINPUT { ... }

struct HARDWAREINPUT { ... }

[StructLayout(LayoutKind.Explicit)]
struct INPUT {
[FieldOffset(0)] public int type;
[FieldOffset(4)] public KEYBDINPUT ki;
[FieldOffset(4)] public MOUSEINPUT mi;
[FieldOffset(4)] public HARDWAREINPUT hi;
}


>There's also another problem. The documentation states that this function is
>defined in user32.lib NOT user32.dll. Is my importing the dll instead of the
>lib ok?

Yes, the actual implementation is in user32.dll.


Mattias

===
Mattias Sjögren (VB MVP)
mattias @ mvps.org
http://www.msjogren.net/dotnet/

Andrei Aron

non lue,
15 août 2002, 19:58:2915/08/2002
à
thank you very much!


"Mattias Sjögren" <mattias.don...@mvps.org> wrote in message
news:eoDYbeKRCHA.1652@tkmsftngp09...

Répondre à tous
Répondre à l'auteur
Transférer
0 nouveau message