2

I am trying to import a C++ function in my C# code.

This function is defined as:

int SetPointers(int* ID, int* BufferID, int** Pointer, double** Time, int NumberOfPointers);

with ID an array of int, BufferId an array of int, Pointer an array of int, Time an array of double, and NumberOfPointers an int.

I have tried to use IntPtr without success.

Here is the latest code I tried:

[DllImport("open.dll", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern int SetPointers(int* ID, int* BufferID, ref IntPtr Pointer, ref IntPtr Time, int NumberOfPointers);
public unsafe int _SetPointers(int[] ID, int[] BufferID, ref int[] Pointer, ref double[] Time, int NumberOfPointers)
{
    IntPtr fQueue = IntPtr.Zero;
    IntPtr fTime = IntPtr.Zero;
    int breturn = -1;
    fixed (int* fId = ID)
    fixed (int* fBufferID = BufferID)
    fQueue = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(int)) * Pointer.Length);
    fTime = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(double)) * Timestamp.Length);
    breturn = SetPointers(fId, fBufferID, ref fQueue, ref fTime, NumberOfPointers);
    return breturn;
}

How can I do this?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
diditexas
  • 121
  • 1
  • 10
  • What does `malloc` have to do with the question? – crashmstr Nov 26 '13 at 15:51
  • You need to show, how this function is used in unmanaged code. Or show the function code. – Alex F Nov 26 '13 at 15:54
  • Sorry, I meant marshal – diditexas Nov 26 '13 at 15:54
  • I can't modify that code, and it is very complex. Basically, ID, BufferId and NumberOfPointers are only read by the function and Pointer and Time are like returned values. Does this help? – diditexas Nov 26 '13 at 15:55
  • I dont think you are doing the right thing. Double referencing is probably an out parameter - Please read this http://stackoverflow.com/questions/6810419/c-cli-double-pointer-typecasting-to-ref-intptr-for-c-sharp-access – Sujay Ghosh Nov 26 '13 at 16:05
  • What happens when the 'fixed' statement is not "scoped" with `{}` (like it is in all examples)? Does it actually work as intended in this case? – Peter Mortensen May 30 '22 at 20:15
  • The scope of 'fixed' is [limited to single lines](https://stackoverflow.com/questions/8663394/using-fixed-for-a-pointer-statement/8664063#8664063) (in this case). What consequence does this have? For instance, will `fBufferID` be protected in `breturn = SetPointers(fId, fBufferID, ref fQueue, ref fTime, NumberOfPointers);`? – Peter Mortensen Jun 03 '22 at 21:56
  • From *[Nested 'fixed' statement](https://stackoverflow.com/questions/48782992/)*: *"After the code in the statement is executed, any pinned variables are unpinned and subject to garbage collection. Therefore, do not point to those variables outside the 'fixed' statement."* – Peter Mortensen Jun 03 '22 at 22:07

1 Answers1

2

First all, you might want to use IntPtr for your parameters rather than int[].

After this, I haven't tried it but to marshal pointers on pointers a "ref IntPtr" or "out IntPtr" would work.

public unsafe int _SetPointers(IntPtr ID, IntPtr BufferID, ref IntPtr Pointer, ref IntPtr Time, int NumberOfPointers);

Also have a look to this other question: How do I marshall a pointer to a pointer of an array of structures?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
DarkUrse
  • 2,084
  • 3
  • 25
  • 33