Calling function with pointer from a C like dll into a C# program.

C

Calin

I'm trying to build a wrapper but the pointer gives me a hard time.

the C code:

typedef enum
{
MANYMOUSE_EVENT_ABSMOTION = 0,
MANYMOUSE_EVENT_RELMOTION,
MANYMOUSE_EVENT_BUTTON,
MANYMOUSE_EVENT_SCROLL,
MANYMOUSE_EVENT_DISCONNECT,
MANYMOUSE_EVENT_MAX
} ManyMouseEventType;

__declspec(dllexport) typedef struct
{
ManyMouseEventType type;
unsigned int device;
unsigned int item;
int value;
int minval;
int maxval;
} ManyMouseEvent;

__declspec(dllexport) int ManyMouse_PollEvent(ManyMouseEvent *event);

C# code:

[DllImport("ManyMouseDLL.dll")]
public static extern int ManyMouse_PollEvent( );
 
C

Calin

I think I've figured it to a certain degree but I still don't know how
to 'fix' the enumeration.

[DllImport("ManyMouseDLL.dll",CharSet = CharSet.Ansi)]
internal static extern int
ManyMouse_PollEvent([MarshalAs(UnmanagedType.LPStruct)] ManyMouseEvent
MouseEvent);

[StructLayout (LayoutKind.Sequential)]
internal struct ManyMouseEvent
{

public int device;
public int item;
public int value;
public int minval;
public int maxval;
};


Any help is appreciated
 
M

Mattias Sjögren

I think I've figured it to a certain degree but I still don't know how
to 'fix' the enumeration.

What you have is almost valid C# code. Just strip off the typedef
stuff.

enum ManyMouseEventType
{
MANYMOUSE_EVENT_ABSMOTION = 0,
MANYMOUSE_EVENT_RELMOTION,
MANYMOUSE_EVENT_BUTTON,
MANYMOUSE_EVENT_SCROLL,
MANYMOUSE_EVENT_DISCONNECT,
MANYMOUSE_EVENT_MAX
}
[DllImport("ManyMouseDLL.dll",CharSet = CharSet.Ansi)]
internal static extern int
ManyMouse_PollEvent([MarshalAs(UnmanagedType.LPStruct)] ManyMouseEvent
MouseEvent);

Make that

internal static extern int ManyMouse_PollEvent(ref ManyMouseEvent
MouseEvent);


Mattias
 
C

Calin

I think I've figured it to a certain degree but I still don't know how
to 'fix' the enumeration.

What you have is almost valid C# code. Just strip off the typedef
stuff.

enum ManyMouseEventType
{
MANYMOUSE_EVENT_ABSMOTION = 0,
MANYMOUSE_EVENT_RELMOTION,
MANYMOUSE_EVENT_BUTTON,
MANYMOUSE_EVENT_SCROLL,
MANYMOUSE_EVENT_DISCONNECT,
MANYMOUSE_EVENT_MAX

}
[DllImport("ManyMouseDLL.dll",CharSet = CharSet.Ansi)]
internal static extern int
ManyMouse_PollEvent([MarshalAs(UnmanagedType.LPStruct)] ManyMouseEvent
MouseEvent);

Make that

internal static extern int ManyMouse_PollEvent(ref ManyMouseEvent
MouseEvent);

Mattias

Thanks that works fine.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top