C# interop with complex C++ library (STMCubeProgrammer)

624 Views Asked by At

I am trying to wrap the public API in a DLL file for the STMCubeProgrammer in C#. There is header file, documentation, and some C++ examples provided for the library.

The examples projects works well, so I can connect, read, write the target with them.

When I try to wrap the first function which is used in the example from the DLL, and I try to call it from C# simply just nothing happens.

This is the example code (important part):

debugConnectParameters *stLinkList;
int getStlinkListNb = getStLinkList(&stLinkList, 0);

This is from the header file for the DLL:

int getStLinkList(debugConnectParameters** stLinkList, int shared);

With this I get for getStlinkListNb = 1 as 1 STLink is connected


Here is the C# Dll import part:

[DllImport(@"C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\api\lib\CubeProgrammer_API.dll")]
public static extern int getStLinkList(ref IntPtr stLinkList, int shared);

And here is my code in my test method:

IntPtr list = IntPtr.Zero;
var count = CubeProgrammerAPI.getStLinkList(ref list ,0);

But nothing happens, count is zero, list is zero, there is no error. I tried different verisons like this with ref, also out, used the In, Out attributes in the method prototype.


I tried with unsafe and pointers:

[DllImport(@"C:\Program Files\STMicroelectronics\STM32Cube\STM32CubeProgrammer\api\lib\CubeProgrammer_API.dll")]
public static unsafe extern int getStLinkList(debugConnectParameters** stLinkList, int shared);
CubeProgrammerAPI.debugConnectParameters *dbgParams;
var count = CubeProgrammerAPI.getStLinkList(&dbgParams, 0);

But nothing again


I tried with safe code also, ref array of the struct itself, but nothing happens, no error.

Can somebody help me? Is it normal, that I do not get any error but it is not working either?

Thanks for the help!

1

There are 1 best solutions below

0
On

Okay I found the problem on an other topic. It is not interop related but related to the DLL I wanted to wrap (mostly).

This is the solution: https://stackoverflow.com/a/62924001/6330997