Skip to main content
gamedev.net
🔒 Locked

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

Started by Calin 1 reply 1.2k views
Original Post
Calin
Calin
The function I'm trying to use is ManyMouse_PollEvent the C code looks something like this:

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); 


In C# I'm stuck at [DllImport("ManyMouseDLL.dll",CharSet = CharSet.Ansi)] internal static extern int ManyMouse_PollEvent(??); Any help is greatly appreciated. [EDIT] After some search it looks like I should define the struct in C#. I have adapted what I found to my code:

[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;
        }; 

It this the right way to do things? how to get the C enum working (ManyMouseEventType)?
My project`s facebook page is “DreamLand Page”
benryves
benryves
You could use uint in place of C's unsigned int; to declare the enumeration just use:

enum ManyMouseEventType {    AbsMotion,    RelMotion,    Button,    Scroll,    Disconnect,    Max,}[StructLayout (LayoutKind.Sequential)]struct ManyMouseEvent {    public ManyMouseEventType type;    public uint device;    public uint item;    public int value;    public int minval;    public int maxval;}


If that works for you, that's good; however I'm more used to doing it manually, grabbing a GCHandle to the struct and passing an IntPtr to the unmanaged code.
[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]
Calin
Calin
Thanks, you saved my day.
My project`s facebook page is “DreamLand Page”

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.