0

I want to create a C# wrapper class with the functions in mcp23017.c within the WiringPi C library. I took this existing C# WiringPi WrapperClass for the other functions. I want to extend this wrapper class to use the functions for mcp23017. I tried creating a new class with one function within the wrapper:

public class mcp23017
{

    [DllImport("libmcp23017.so", EntryPoint = "myPinMode")]
    public static extern void myPinMode(struct wiringPiNodeStruct *node,Int32 pin, Int32 mode);

}

But I get these errors for the struct element.

) expected.
; expected.
{ expected.
} expected.
Invalid token "*" in class struct or interface member declaration

Do I have to define a struct in the wrapper class? How does it work? Not familiar with this as I am new to using C#.

Alex Krish
  • 93
  • 1
  • 13

1 Answers1

0

It's not valid to use struct on a parameter in C#. The marshaller should take care of this behavior for you. Perhaps you meant something like this.

[StructLayout(LayoutKind.Sequential)]
public struct WiringNode
{
    //Struct members here
}

[DllImport("libmcp23017.so", EntryPoint = "myPinMode")]
public static extern void myPinMode(ref WiringNode node, Int32 pin, Int32 mode);

Then called as:

var myStruct = new WiringStruct();
//Set struct members

myPinMode(ref myStruct, whatever, whatever);
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • But I want to use the `struct wiringPiNodeStruct` defined in the C library in file wiringPi.h (link: WiringPI). Can I refer the struct in the Wrapper class? – Alex Krish Aug 09 '16 at 13:51
  • Not directly. You'll need to recreate the struct on the .net side and then let marshalling handle converting the memory between .net and c – Jarrett Robertson Aug 09 '16 at 13:53
  • Okay. Any examples or references on how that can be done? Or any other way to use the functions from C library without creating a wrapper class? – Alex Krish Aug 09 '16 at 13:54
  • 2
    http://stackoverflow.com/questions/7923871/pinvoke-struct-translation-from-c would get you started. Basically you'll want to search pinvoke struct to determine how each member should be declared. – Jarrett Robertson Aug 09 '16 at 13:58
  • Perfect. Thanks! Will try and get back. – Alex Krish Aug 09 '16 at 14:05