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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Calin

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

    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
    } ManyMouseEventT ype;

    __declspec(dlle xport) typedef struct
    {
    ManyMouseEventT ype type;
    unsigned int device;
    unsigned int item;
    int value;
    int minval;
    int maxval;
    } ManyMouseEvent;

    __declspec(dlle xport) int ManyMouse_PollE vent(ManyMouseE vent *event);

    C# code:

    [DllImport("Many MouseDLL.dll")]
    public static extern int ManyMouse_PollE vent( );

  • Calin

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

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

    [DllImport("Many MouseDLL.dll",C harSet = CharSet.Ansi)]
    internal static extern int
    ManyMouse_PollE vent([MarshalAs(Unman agedType.LPStru ct)] ManyMouseEvent
    MouseEvent);

    [StructLayout (LayoutKind.Seq uential)]
    internal struct ManyMouseEvent
    {

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


    Any help is appreciated

    On Mar 29, 4:32 am, "Calin" <calinne...@gma il.comwrote:
    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
    >
    } ManyMouseEventT ype;
    >
    __declspec(dlle xport) typedef struct
    {
    ManyMouseEventT ype type;
    unsigned int device;
    unsigned int item;
    int value;
    int minval;
    int maxval;
    >
    } ManyMouseEvent;
    >
    __declspec(dlle xport) int ManyMouse_PollE vent(ManyMouseE vent *event);
    >
    C# code:
    >
    [DllImport("Many MouseDLL.dll")]
    public static extern int ManyMouse_PollE vent( );

    Comment

    • Mattias Sjögren

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

      >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 ManyMouseEventT ype
      {
      MANYMOUSE_EVENT _ABSMOTION = 0,
      MANYMOUSE_EVENT _RELMOTION,
      MANYMOUSE_EVENT _BUTTON,
      MANYMOUSE_EVENT _SCROLL,
      MANYMOUSE_EVENT _DISCONNECT,
      MANYMOUSE_EVENT _MAX
      }
      >[DllImport("Many MouseDLL.dll",C harSet = CharSet.Ansi)]
      internal static extern int
      >ManyMouse_Poll Event([MarshalAs(Unman agedType.LPStru ct)] ManyMouseEvent
      >MouseEvent);
      Make that

      internal static extern int ManyMouse_PollE vent(ref ManyMouseEvent
      MouseEvent);


      Mattias

      --
      Mattias Sjögren [C# MVP] mattias @ mvps.org
      http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
      Please reply only to the newsgroup.

      Comment

      • Calin

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

        On Mar 29, 10:02 pm, Mattias Sjögren <mattias.dont.w ant.s...@mvps.o rg>
        wrote:
        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 ManyMouseEventT ype
        {
        MANYMOUSE_EVENT _ABSMOTION = 0,
        MANYMOUSE_EVENT _RELMOTION,
        MANYMOUSE_EVENT _BUTTON,
        MANYMOUSE_EVENT _SCROLL,
        MANYMOUSE_EVENT _DISCONNECT,
        MANYMOUSE_EVENT _MAX
        >
        }
        [DllImport("Many MouseDLL.dll",C harSet = CharSet.Ansi)]
        internal static extern int
        ManyMouse_PollE vent([MarshalAs(Unman agedType.LPStru ct)] ManyMouseEvent
        MouseEvent);
        >
        Make that
        >
        internal static extern int ManyMouse_PollE vent(ref ManyMouseEvent
        MouseEvent);
        >
        Mattias
        >
        --
        Mattias Sjögren [C# MVP] mattias @ mvps.orghttp://www.msjogren.ne t/dotnet/|http://www.dotnetinterop.com
        Please reply only to the newsgroup.
        Thanks that works fine.

        Comment

        Working...