-1

I have a code of C:

typedef void (* FPS_PositionCallback) ( unsigned int   devNo,
                                    unsigned int   length,
                                    unsigned int   index,
                                    const double * const positions[3],
                                    const bln32  * const markers[3]   );

and I need to write same thing in C#. Any Idea?

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
shubhendu
  • 213
  • 1
  • 3
  • 10

2 Answers2

1

You will need to define a delegate and mark it up with UnmanagedFunctionPointer

e.g. something like

[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void FPS_PositionCallback(
    Int32 devNo, 
    Int32 length,
    Int32 index, 
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]double[] positions, 
    [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)]double[] markers);

I am assuming that bln32 is a double, but you may also need CDecl instead of StdCall and Int16 instead of Int32.

you can then pass this to your c-function e.g. something that might be declared like this

[DllImport("FPSLIB.dll")]
public static extern void setPositionCallback([MarshalAs(UnmanagedType.FunctionPtr)] FPS_PositionCallback callbackPointer);

then execute e.g.

FPS_PositionCallback callback =
    (devNo, length, index, positions, markers) =>
    {
    };

setPositionCallback(callback);

You will probably have a lot of fiddling to do to get it exactly right.

Shaun Wilde
  • 8,228
  • 4
  • 36
  • 56
0

Instead of defining a function pointer, in C# you would define a delegate which describes the method it can hold. In your case, that could look like:

// define the delegate
public delegate void FPS_PositionCallback(
        int   devNo,
        int   length,
        int   index,
        double[] positions,
        double[] markers);

You can then use your delegate like you would use your function pointer typedef and reference and execute a method with it:

    // the method you want to call with the delegate
    public void Method(int devNo, int length, int index, double[] positions, double[] markers);

    public void DoSomething()
    {
        // store a reference to the function with your delegate
        FPS_PositionCallback callback = this.Method;
        // call the method via the delegate
        callback(1, 1, 1, new[]{ 1 }, new[]{ 2 });
    }
pgenfer
  • 597
  • 3
  • 13