I'm currently working on a C++ bridge between a pure C++ Api (not my code) and my Gui in C#. I succeeded to use IJW to call native API function from C# but I'm stuck with a struct that doesn't contain anything.
The struct I need to use.
struct original_struct;
typedef struct original_struct *original_struct_t;
I must call a function with a pointer to that struct. This function will initialize it and set a value. Then I can use this struct pointer in another functions.
The function to initialize this struct :
original_create(int login, original_struct * struct);
An exemple of another function :
original_sendSomething(original_struct struct, original_settings * settings);
I saw somewhere that the C++ empty struct can be translated to a sByte or Byte in C#, but I'm not sure how to cast it.
How can I translate this struct to something reusable in C# (object, parameter, Intpr ?), and be sure that the value won't be destroyed ?
EDIT (Working) :
I finally succeeded, with the help of nvoigt
Here is the solution to ascend to C# :
IntPtr ptr = IntPtr(instance_of_struct);
C#ClassHandler->PointerParameter = ptr;
Here is the cast from C# to c++ struct :
original_struct result = (original_struct)(void*)C#ClassHandler->PointerParameter;
I Hope it can help somebody else.
You can always store the address to something in an IntPtr
. It's the native library's task to make sure it does not destroy the instance being pointed to. There is no garbage collection in native code.
If the struct
does not contain anything, there is no point in having anything but an untyped pointer to it because there is no point in ever having a function in native code that takes anything but a pointer.