How to marshal c++ struct in c# - c#

Below is the C++ struct. Its implementation is done in a C++ DLL:
typedef char CusDate[sizeof("2023-02-02")];
typedef enum cycletype {
NOES=0,
LET=1,
WARN=2,
GRACE=3,
EXP=4,
} MYCYCLETYPE;
typedef struct Info {
MYCYCLETYPE midcycle;
int type;
int porotype;
long sways;
CusDate sedate;
long days;
CusDate elate;
long edays;
CusDate edate;
const char* feature;
const char* ver;
const char* hid;
}SINFO, *SINFO;
int Initialize(SINFO lp)
{
lp->mycycle = NOES;
......
.....
lp->hid = "XYZ";
}
Now, below is the C# code:
[DllImport(#"abc.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int Initialize(ref SINFO lp);
In C# main():
SINFO mydata;
Initialize(ref mydata);
Now, I am unable to access the struct data filled by C++ code in C#.
How to access the C++ struct data in C#?

typedef struct Info {
...
} SINFO, *SINFO;
The above declaration is not valid C/C++, as you can't have the same identifier (SINFO) refer to two different types (Info and Info*). Perhaps you meant something more like this instead:
typedef struct Info {
...
} SINFO, *PSINFO;
^
Or, the more C++-ish style (as the above style originates from C):
struct SINFO {
...
};
typedef SINFO* PSINFO;
// or: using PSINFO = SINFO*;
Either way, Initialize() can then take either a SINFO& reference or a PSINFO pointer as its parameter, either of which would be compatible with ref SINFO on the C# side:
int Initialize(SINFO& lp)
int Initialize(PSINFO lp)
Where the C# code would look something like this:
public enum MYCYCLETYPE {
NOES = 0,
LET = 1,
WARN = 2,
GRACE = 3,
EXP = 4,
};
[StructLayout(LayoutKind.Sequential)]
public struct SINFO {
public MYCYCLETYPE midcycle;
public int type;
public int porotype;
public long sways; // <-- or int, depending on the C++ compiler!
[MarshalAs(UnmanagedType.ByValArray, SizeConst=11)]
public byte[] sedate = new byte[11];
// alternatively:
// [MarshalAs(UnmanagedType.ByValArray, SizeConst=11, ArraySubType=UnmanagedType.LPStr)]
// public String sedate;
public long days; // <-- or int, depending on the C++ compiler!
[MarshalAs(UnmanagedType.ByValArray, SizeConst=11)]
public byte[] elate = new byte[11];
// alternatively:
// [MarshalAs(UnmanagedType.ByValArray, SizeConst=11, ArraySubType=UnmanagedType.LPStr)]
// public String elate;
public long edays; // <-- or int, depending on the C++ compiler!
[MarshalAs(UnmanagedType.ByValArray, SizeConst=11)]
public byte[] edate = new byte[11];
// alternatively:
// [MarshalAs(UnmanagedType.ByValArray, SizeConst=11, ArraySubType=UnmanagedType.LPStr)]
// public String edate;
public IntPtr feature; // <-- use Marshal.PtrToStringAnsi() to read this
// alternatively:
// [MarshalAs(UnmanagedType.LPStr)]
// public String feature;
public IntPtr ver; // <-- use Marshal.PtrToStringAnsi() to read this
// alternatively:
// [MarshalAs(UnmanagedType.LPStr)]
// public String ver;
public IntPtr hid; // <-- use Marshal.PtrToStringAnsi() to read this
// alternatively:
// [MarshalAs(UnmanagedType.LPStr)]
// public String hid;
};
[DllImport("abc.dll", CallingConvention = CallingConvention.Cdecl)]
static extern int Initialize(ref SINFO lp);
Also, on a side note: you have Initialize() declared as returning an int, but the C++ code you have shown is not actually return'ing a value, which is undefined behavior.

Related

C struct and use C dll in C#

everyone, I'm newbie in C#, and I have a problem with using "C" dll in C#, below is the C side code which work fine.
// myCode.h
typedef struct _SubABC
{
unsigned short WordCount;
unsigned char *WordData;
unsigned char SpeedUp;
} SubABC;
typedef struct _ABC
{
unsigned char SubFontNum;
SubABC subABC[5];
} ABC
extern __declspec(dllimport) int __stdcall MY_SetSth(unsigned char SerialNum, ABC *pABC);
//myCode.c
ABC myFun = { '\0' };
unsigned char text_c[] =
{
0x00,0x27,0xff,0xA5,0xC3, 0x00,0x27,0xff,0xA6,0x26, 0x00,0x23,0xff,0xA7,0xAE, 0x00,0x27,0xff,0xBA,0x7E,
0x00,0x27,0xff,0xC1,0x52, 0x00,0x27,0xff,0xAC,0xF9, 0x00,0x27,0xff,0x20,0x00,0x27,0xff,0x31, 0x00,0x27,0xff,0x20, 0x00,0x27,0xff,0xA4,0xC0, 0x00,0x27,0xff,0xC4,0xC1,
};
myFun.SubFontNum = 1;
myFun.subMABC[0].WordCount = sizeof(text_c);
myFun.subMABC[0].WordData = text_c;
myFun.subMABC[0].SpeedUp = 0;
int retvalue = MY_SetSth(1, &myFun);
and my C# code is below
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SubABC
{
public ushort WordCount;
[MarshalAs(UnmanagedType.LPArray)]
public byte[] WordData;
public byte SpeedUp;
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct ABC
{
public byte SubFontNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 5)]
public SubABC[] subABC;
}
[DllImport("myDLL.dll", CharSet = CharSet.Ansi)]
public extern static int MY_SetSth(byte SerialNum, ref ABC pABC);
ABC myFun = new ABC();
ABC.subABC = new SubABC[5];
unsigned char text_c[] =
{
0x00,0x27,0xff,0xA5,0xC3, 0x00,0x27,0xff,0xA6,0x26, 0x00,0x23,0xff,0xA7,0xAE, 0x00,0x27,0xff,0xBA,0x7E,
0x00,0x27,0xff,0xC1,0x52, 0x00,0x27,0xff,0xAC,0xF9, 0x00,0x27,0xff,0x20,0x00,0x27,0xff,0x31, 0x00,0x27,0xff,0x20, 0x00,0x27,0xff,0xA4,0xC0, 0x00,0x27,0xff,0xC4,0xC1,
};
myFun.SubFontNum = 1;
myFun.subMABC[0].WordCount = (ushort)text_c.Length;
myFun.subMABC[0].WordData = text_c;
myFun.subMABC[0].SpeedUp = 0;
int retvalue = MY_SetSth(1, ref myFun);
I always get TypeLoadException: Cannot marshal field 'WordData' of type 'subABC': Invalid managed/unmanaged type combination (Array fields must be paired with ByValArray or SafeArray), because WordData is unknown size, I can't use ByValArray, and SafeArray get AccessViolationException: Attempted to read or write protected memory
I try to use IntPtr like below
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct SubABC
{
public ushort WordCount;
public IntPtr WordData;
public byte SpeedUp;
}
ABC.subABC[0].WordData = Marshal.AllocHGlobal(text_c.Length); ;
Marshal.Copy(text_c, 0, ABC.subABC[0].WordData, text_c.Length);
int retvalue = MY_SetSth(1, ref myFun);
I get error message AccessViolationException: Attempted to read or write protected memory
Can anyone help me? thank a lot.

How to fix 'Cannot marshal 'return value': Invalid managed/unmanaged type combination (Int/UInt must be paired with SysInt or SysUInt).'

I'm trying to return a struct from a C++ callback from C# and I get the error as described in the title.
I appreciate there is a lot of information, but I wanted to include too much rather than not enough.
I've tried returning the function as a structure, but then read it may be easier to return the function as an IntPtr and use Marshal.PtrtoStructure.
The function that I want to call is from a .dll that comes from a different source and the input is:
C++:
rVDACQ_Connect(int, tVDACQ_CallBackProc, void*, short*, int)
the function in the sample (C++) code returns a struct (rACQ_CallBackRec) which is as follows:
rACQ_CallBackRec = theApp.m_Intf.rVDACQ_Connect(cVDACQ_FBright, CALLBACK_Acquisition, this, m_FrmBuffer, 0);
//rACQ_CallBackRec is the struct type tVDACQ_CallBackRec (as described below)
With CALLBACK_Aquisition being:
extern "C" {
__declspec(dllexport) void _stdcall CALLBACK_Acquisition(tVDACQ_CallBackRec* AR)
{
tVDACQ_CallBackProc test;
((CPreviewDlg*)AR->rUserParam)->My_ACQ_CallBack(AR, test);
}
}
The layout of the struct is as follows:
typedef struct {
int rFlags, // combination of cVDACQ_Fxxxx
rType, // cVDACQ_ETxxx
rEvent, // cVDACQ_Exxx
rSocket; // 0:no relation to a 'socket'; otherwise socket's ID>0 (event's source ID)
TCHAR rMsg[256]; // message (trace, wrn, err)
int rFrameWidth, // full frame width
rFrameHeight; // full frame height
short* rFrameBuffer; // user supplied frame buffer "AFrameBuffer"
union {
int rCaptureRows; // # of received rows (for single frame acquisition)
int rCaptureFrames; // # of received full frames (for framegrabber)
};
int rCapturePercent; // received data in percents
void* rUserCallBackProc, // user supplied "ACallBackProc"
* rUserParam; // user supplied "AUserParam"
int rAborted; // 1: VDACQ_Abort -1:internally
void* rPacketData; // pointer to received packet; usually it is nil
int rFGControl; // frame-grabber's control flags
} tVDACQ_CallBackRec;
typedef void(_stdcall* tVDACQ_CallBackProc)(tVDACQ_CallBackRec*); //The Callback
C#
I've created the callback procedure in C#, as well as the struct and PInvoke:
[UnmanagedFunctionPointer(CallingConvention.StdCall)]
public delegate void tVDACQ_CallBackProc(tVDACQ_CallBackRec AR);
[DllImport("C:\\Users\\jch\\source\\repos\\FlatPanelSensor\\x64\\Debug\\VADAV_AcqS.dll", EntryPoint = "CALLBACK_Acquisition", CallingConvention = CallingConvention.Cdecl)]
public unsafe static extern void CALLBACK_Acquisition(tVDACQ_CallBackRec AR);
[DllImport("C:\\Users\\jch\\source\\repos\\FlatPanelSensor\\FlatPanelSensor\\bin\\Debug\\VADAV_FGM_64.dll", EntryPoint = "VDACQ_Connect", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.I4)]
public unsafe static extern IntPtr VDACQ_Connect(int i, [MarshalAs(UnmanagedType.FunctionPtr)] tVDACQ_CallBackProc proc, dynamic n, IntPtr[] buffer, int j);
Struct:
[StructLayout(LayoutKind.Sequential)]
public struct tVDACQ_CallBackRec
{
public int rFlags;
public int rType;
public int rEvent;
public int rSocket;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public string rMsg;
public int rFrameWidth;
public int rFrameHeight;
public IntPtr rFrameBuffer;
public int rCaptureRows;
public int rCaptureFrames;
public int rCapturePercent;
public IntPtr rUserCallBackProc;
public IntPtr rUserParam;
public int rAborted;
public IntPtr rPacketData;
public int rFGControl;
}
CallBack:
tVDACQ_CallBackProc callBack =
(AR) =>
{
CALLBACK_Acquisition(AR); //Don't know how necessary this
is
};
Function call:
IntPtr work = VDACQ_Connect(0, callBack, this, m_FrmBuffer, 0);
//I know 'this' isn't defined as a void* in my DLLImport,
//but making it an IntPtr didn't work either so
//the only type I could think of was dynamic.
If I could return this as a IntPtr or, even better, the struct I defined in my C# code that would be great.
If you need anymore information then please let me know, as I think I included everything.

consume c# array of structs using c++ unmanaged Dll

as i am new to the unmanaged world, and after a success test of interacting with c++ DLL on a simple task,
i am now trying to consume in c#, data manipulated by c++ via an array of struct
i have also tried to wrap the struct[] within another struct to simplify the marshal but i did not succeed in any way.
what is the correct (and most efficient performance wise) way to handle the data returned from c++ given the code below.
C#
[StructLayout(LayoutKind.Sequential)]
public struct chars
{
public int V;
//[MarshalAsAttribute(UnmanagedType.ByValTStr, SizeConst = 10)]
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
public char[] testStr;
}
[DllImport("ExportStructArr.dll", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity]
public static extern void dodata(int requestedMasterArrLength, int requestedStringSize,[MarshalAs(UnmanagedType.LPArray), Out()] chars[] Cars);
//tried also this signature
public static extern void dodata(
int requestedMasterArrLength,
int requestedStringSize,
out IntPtr chars);
c++
#ifdef EXPORTSTRUCTARR_EXPORTS
#define EXPORTSTRUCTARR_API extern "C" __declspec(dllexport)
typedef struct {
int Id;
char *StrVal;
}Unit;
EXPORTSTRUCTARR_API void dodata(int requestedMasterArrLength,int requestedStringSize, Unit **UntArr)
{
int ccountr,count;
count=0;
*UntArr = (Unit*)LocalAlloc(0, requestedMasterArrLength * sizeof(Unit));
Unit *CurU = *UntArr;
while(count!= requestedMasterArrLength)
{
char dummyStringDataObject[]= "abcdefgHi";
CurU[count].StrVal = NULL;
dummyStringDataObject[0] = count+'0';
CurU[count].Id = count;
CurU[count].StrVal= (char*) LocalAlloc(0,(sizeof(char)* requestedStringSize));
ccountr=0;
while(ccountr!= requestedStringSize){
CurU[count].StrVal[ccountr] = dummyStringDataObject[ccountr];
++ccountr;
}
++count;
}
}

Passing C# structure with string array to c++ function which accepts void * for c# structure and char** for c# string array

I want to send a C# structure with string array to a C++ function which accepts void * for the c# structure and char** for the c# structure string array member.
I was able to send the structure to c++ function,but the issue is , not able to access the string array data member of c# structure from c++ function. When sending the string array separately,i was able to access the array elements.
Sample code is-
C# Code:
[StructLayout(LayoutKind.Sequential)]
public struct TestInfo
{
public int TestId;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public String[] Parameters;
}
[DllImport("TestAPI.dll", CallingConvention = CallingConvention.StdCall, EntryPoint "TestAPI")]
private static extern void TestAPI(ref TestInfo data);
static unsafe void Main(string[] args)
{
TestInfo testinfoObj = new TestInfo();
testinfoObj.TestId = 1;
List<string> names = new List<string>();
names.Add("first");
names.Add("second");
names.Add("third");
testinfoObj.Parameters=names.ToArray();
TestAPI(ref testinfoObj);
}
VC++ Code:
/*Structure with details for TestInfo*/
typedef struct TestInfo
{
int TestId;
char **Parameters;
}TestInfo_t;
//c++ function
__declspec(dllexport) int TestAPI(void *data)
{
TestInfo *cmd_data_ptr= NULL;
cmd_data_ptr = (TestInfo) data;
printf("ID is %d \r\n",cmd_data_ptr->TestId);//Working fine
for(i = 0; i < 3; i++)
printf("value: %s \r\n",((char *)cmd_data_ptr->Parameters)[i]);/*Error-Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt*/
}
When analyzing the memory stack, it is observed that,when i print
((char *)cmd_data_ptr->Parameters), the first array element("first") is getting printed,
but using ((char *)cmd_data_ptr->Parameters)[i], not able access elements and above mentioned exception is coming.
The structure memory address contains address of all the structure elements,but while accessing the data from c++,it is accessing only the first element of the string array.
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2)]
public String[] Parameters;
is an inline array. The C++ declaration that matches is:
char* Parameters[2];
But you are trying to match it to:
char** Parameters;
and that's completely different.
You will need to marshal this by hand. In the C# struct declare Parameters to be IntPtr. Then allocate native memory with Marshal.AllocHGlobal to hold an array of pointers. And then populate those pointers with pointers to your strings.
[StructLayout(LayoutKind.Sequential)]
public struct TestInfo
{
public int TestId;
public IntPtr Parameters;
}
static void Main(string[] args) // no need for unsafe
{
TestInfo testInfo;
testInfo.TestId = 1;
testInfo.Parameters = Marshal.AllocHGlobal(2*Marshal.SizeOf(typeof(IntPtr)));
IntPtr ptr = testInfo.Parameters;
Marshal.WriteIntPtr(ptr, Marshal.StringToHGlobalAnsi("foo"));
ptr += Marshal.SizeOf(typeof(IntPtr));
Marshal.WriteIntPtr(ptr, Marshal.StringToHGlobalAnsi("bar"));
TestAPI(ref testinfoObj);
// now you want to call FreeHGlobal, I'll leave that code to you
}
An alternative would be to use a pinned IntPtr[] and put that in testInfo.Parameters.
This is really more of an expansion/extension to David's answer, but here's one way to wrap up the custom marshalling:
public struct LocalTestInfo
{
public int TestId;
public IEnumerable<string> Parameters;
public static explicit operator TestInfo(LocalTestInfo info)
{
var marshalled = new TestInfo
{
TestId = info.TestId,
};
var paramsArray = info.Parameters
.Select(Marshal.StringToHGlobalAnsi)
.ToArray();
marshalled.pinnedHandle = GCHandle.Alloc(
paramsArray,
GCHandleType.Pinned);
marshalled.Parameters =
marshalled.pinnedHandle.AddrOfPinnedObject();
return marshalled;
}
}
[StructLayout(LayoutKind.Sequential)]
public struct TestInfo : IDisposable
{
public int TestId;
public IntPtr Parameters;
[NonSerialized]
public GCHandle pinnedHandle;
public void Dispose()
{
if (pinnedHandle.IsAllocated)
{
Console.WriteLine("Freeing pinned handle");
var paramsArray = (IntPtr[])this.pinnedHandle.Target;
foreach (IntPtr ptr in paramsArray)
{
Console.WriteLine("Freeing # " + ptr);
Marshal.FreeHGlobal(ptr);
}
pinnedHandle.Free();
}
}
}
Note for my test I swapped over to CDecl:
[DllImport(#"Test.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int TestAPI(ref TestInfo info);
Also I think you had a typo in the C++ side:
extern "C"
__declspec(dllexport) int TestAPI(void *data)
{
TestInfo *cmd_data_ptr= NULL;
cmd_data_ptr = (TestInfo*) data;
printf("ID is %d \r\n",cmd_data_ptr->TestId);
// char**, not char*
char** paramsArray = ((char **)cmd_data_ptr->Parameters);
for(int i = 0; i < 3; i++)
{
printf("value: %s \r\n",paramsArray[i]);
}
return 0;
}
And a test rig:
static void Main(string[] args)
{
var localInfo = new LocalTestInfo()
{
TestId = 1,
Parameters = new[]
{
"Foo",
"Bar",
"Baz"
}
};
TestInfo forMarshalling;
using (forMarshalling = (TestInfo)localInfo)
{
TestAPI(ref forMarshalling);
}
}
The reverse marshalling operator is left as an exercise to the reader, but should basically look like the inverse of the explicit TestInfo operator.

Invalid Managed/Unmanaged Type Combination With Embedded, Dynamically-Allocated Array

I have a common construct in an unmanaged Win32 C++ DLL:
// FirstElemPtrContainer.h
#include "stdafx.h"
typedef unsigned char elem_type; // a byte
typedef struct FirstElemPtrContainer {
unsigned char num_elems;
void *allocd_ary;
} FirstElemPtrContainer;
The void* in the struct is meant to contain a pointer to the first element of an allocated byte array.
The DLL that uses this definition then exports functions to allocate, populate, and deallocate the struct:
// The exported allocator function.
extern "C" _declspec(dllexport)
FirstElemPtrContainer *BuildStruct(int elem_count)
{
FirstElemPtrContainer *fepc_ptr = new FirstElemPtrContainer;
fepc_ptr->num_elems = elem_count;
elem_type *ary = new elem_type[fepc_ptr->num_elems];
for (int i = 0; i < fepc_ptr->num_elems; i++)
{
ary[i] = ((i + 1) * 5); // multiples of 5
}
fepc_ptr->allocd_ary = ary;
return fepc_ptr;
}
// The exported deallocator function.
extern "C" _declspec(dllexport) void
DestroyStruct(FirstElemPtrContainer *fepc_ptr)
{
delete[] fepc_ptr->allocd_ary;
delete fepc_ptr;
}
These work just fine for a native caller.
In C#, I try to describe this same structure via PInvoke:
[StructLayout(LayoutKind.Sequential)]
public struct FirstElemPtrContainer
{
public byte num_elems;
[MarshalAs(UnmanagedType.LPArray,
ArraySubType = UnmanagedType.U1, SizeConst = 4)]
public IntPtr allocd_ary;
}
... and describe the call interface like so:
public static class Imports
{
[DllImport("MyLib", CallingConvention = CallingConvention.Winapi)]
public static extern IntPtr BuildStruct(int elem_count);
[DllImport("MyLib", CallingConvention = CallingConvention.Winapi)]
public static extern void DestroyStruct(IntPtr fepc_ptr);
}
Now I attempt to call my interface:
class Program
{
const int NUM_ELEMS = 4;
static void Main(string[] args)
{
IntPtr fepc_ptr = Imports.BuildStruct(NUM_ELEMS);
if ( fepc_ptr == IntPtr.Zero )
{
Console.WriteLine("Error getting struct from PInvoke.");
return;
}
FirstElemPtrContainer fepc =
(FirstElemPtrContainer)Marshal.PtrToStructure(fepc_ptr,
typeof(FirstElemPtrContainer));
//...
}
}
The PtrToStructure() call gives the error "Cannot marshal field 'allocd_ary' of type 'MyLibInvoke.FirstElemPtrContainer': Invalid managed/unmanaged type combination (Int/UInt must be paired with SysInt or SysUInt)."
You can see that I've hard-coded a particular number of elements, which we'll assume the caller adheres to. I've also added an ArraySubType clause, though it seems not to make a difference. Why the type mismatch complaint?
Your struct should be declared like this:
[StructLayout(LayoutKind.Sequential)]
public struct FirstElemPtrContainer
{
public byte num_elems;
public IntPtr allocd_ary;
}
it has to be done this way because allocd_ary is a pointer to unmanaged memory and cannot be marshalled by the p/invoke marshaller.
In order to read the contents of allocd_ary you can use Marshal.Copy.
FirstElemPtrContainer fepc = (FirstElemPtrContainer)Marshal.
PtrToStructure(fepc_ptr, typeof(FirstElemPtrContainer));
byte[] ary = new byte[fepc.num_elems];
Marshal.Copy(fepc.allocd_ary, ary, 0, ary.Length);
I suspect that CallingConvention.Winapi is wrong and that you should be using CallingConvention.Cdecl.

Categories

Resources