// code in C#
[StructLayout(LayoutKind.Sequential)]
public struct Point
{
[MarshalAs(UnmanagedType.I4)]
public int x;
[MarshalAs(UnmanagedType.LPStr)]
public string y;
}
public static class DllExports
{
[DllExport]
public static void Test(ref Point x)
{
MessageBox.Show($"Data: {JsonConvert.SerializeObject(x)}");
}
}
// code in C++
struct X{
int x;
const char* y;
};
int main()
{
HWND lib = LoadLibrary("library.dll");
HWND f_addr = GetProcAddress(lib, "Test");
void(*Test)(X*) = reinterpret_cast<void(*)(X*)>(f_addr);
X *x = new X();
x->x = 5;
x->y = "abc";
Test(x);
// result:
// Data: {"x":5,"y":"abc"}
}