I've this C-C# code that works:
.h
typedef struct {
float a;
float b;
} MyStruct;
extern MyStruct mystruct;
__declspec(dllexport) void GetMyStruct (MyStruct* s);
__declspec(dllexport) void SetMyStruct (MyStruct* s);
.c
MyStruct mystruct;
void GetMyStruct (MyStruct* s)
{
*s = AeroLink_IOPkt;
}
void SetMyStruct (MyStruct* s)
{
AeroLink_IOPkt = *s;
}
void test()
{
// some code that update element in struct
// mystruct.a = 0.4;
// mystruct.a = 0.1;
}
.cs
public struct MyStruct
{
public float a;
public float b;
}
[DllImport(DLL_NAME, EntryPoint = "GetMyStruct")]
protected static extern void GetMyStruct(ref MyStruct s);
[DllImport(DLL_NAME, EntryPoint = "SetMyStruct")]
protected static extern void SetMyStruct(ref MyStruct s);
This way, every time I need to set data from C# to C, I must call void SetMyStruct
and vice-versa if I want to get data from C (updated from void test) to C# I must call GetMyStruct
. I must do this 50 times per seconds.
Is there a way to avoid calling SetMyStruct
and GetMyStruct
every time? I would like to use SetMyStruct
one time and then have all changes be reflected, from and to. I do not know if this is possible.
struct
toclass
and remove the variousref
keywordsunsafe
code in your C# program to directly access its a and b members. But you did make one mistake, you did not check if you needed to optimize this code. Calling it only 60 times per second, you most definitely don't. This is very fast code, you can call it hundreds of millions of times per second.c
tag from this question?