Answer the question
In order to leave comments, you need to log in
Export structure containing arrays from C++ to C#?
In general, I encountered something incomprehensible when reading data from a DLL
Structure in a dll
#pragma pack(push,1)
typedef struct SIGMPGroup{
int temp;
char name[50]; //name of group
int port;
char addr[50]; //network address "229."
//int ttl; //
//int loop; // 0 - no loop back, 1 - loop back
SIGMPGroup():
temp(0),
port(0)
//ttl(-1),
//loop(0)
{}
};
#pragma pack(pop)
#include "StreamEth.h"
#include <stdio.h>
#define dll extern "C" __declspec(dllexport)
StreamEth Eth;
dll
void Leave(const SIGMPGroup& data){
Eth.leave(data);
}
public static SIGMPGroup DTIGMPGroup = new SIGMPGroup { temp = 56, name = "Example",port = 4000 };
[StructLayout(LayoutKind.Explicit, Pack = 1, CharSet = CharSet.Ansi, Size = 108)]
public struct SIGMPGroup{
[FieldOffset(0)]
public int temp;
[ FieldOffset(4), MarshalAs(UnmanagedType.ByValTStr,SizeConst=50)]
public string name; //name of group
[FieldOffset(54)]
public int port;
[FieldOffset(58), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string addr;
}
[DllImport(Eth_DLL, CallingConvention = CallingConvention.Cdecl)]
public static extern void Leave(ref SIGMPGroup data);
ImportFunctionEth.Leave(ref ImportFunctionEth.DTIGMPGroup);
[FieldOffset(58), MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string addr;
Answer the question
In order to leave comments, you need to log in
It turned out that the code would work, the only way
[StructLayout(LayoutKind.Sequential, Pack = 1, CharSet = CharSet.Ansi)]
public struct SIGMPGroup
{
public int temp;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string name; //name of group
public int port;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 50)]
public string addr;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question