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

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;
}
}

Related

How to assign value to a char array (C-String) within C++ DLL through C# code

I have a DLL developed in C++ that performs some computations. I am trying to link this DLL into my C# application.
I have a struct within a struct which contains a char array (C string). I have a pointer to the object created by the C++ DLL within my C# application.
namespace Test
[StructLayout(LayoutKind.Sequential)]
public unsafe struct Child
{
public float number1;
public float number2;
[MarshalAs(UnmanagedType.LPArray)]
public char[] name;
}
[StructLayout(LayoutKind.Sequential)]
public unsafe struct DataStructure
{
public Child child;
...
}
.
.
.
public unsafe partial class Form1:Form {
[DllImport("Calculation.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern IntPtr createInstance();
[DllImport("Calculation.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern DataStructure* processData(IntPtr source); // Setup of data
[DllImport("Calculation.dll", CallingConvention = CallingConvention.Cdecl)]
unsafe public static extern int calculate(IntPtr calc); // Perform calculation
.
.
public Form2()
{
private void button1_Click_1(object sender, EventArgs e)
{
// test functions
unsafe
{
IntPtr ptr1 = createInstance(); // Returns pointer to the instance
DataStructure* data = processData(ptr1); // Returns pointer to DataStructure object created through DllImport
data->child.number1 = 1.234F; // Works
data->child.number1 = 9.876F; // Works
data->child.name = "......" // This doesn't work!!
int result = calculate(ptr1); // Returns value when name commented
}
}
}
}
If I don't comment name, I get the Error - Cannot take the address of managed object.
If I comment name, I am able to run the calculation without issue.
I wish to update the value of the char array name but can't seem to figure out the solution. Below is my usual C solution. Thanks!
#include "DataStructure.h"
#define DLLNAME "Calculation.dll"
int main(int argc, char** argv) {
HINSTANCE dllHandle = LoadLibraryA(fileNameDll);
const void* ptr1 = 0;
int result = 0;
DataStructure *data;
ptr1 = createInstance();
data = processData(ptr1);
data->child.number1 = 1.234;
data->child.number2 = 9.876;
sprintf(data->child.name, "3104-03");
result = calculate(ptr1);
}

Sending array of struct from c++ to c#

I am trying to send an array of simple struct from c# to a c++ DLL and to get the size elements of the array.
My c++ code is:
struct TestStruct
{
int m_Int;
};
unsigned int _declspec(dllexport) __stdcall GetSysData(LPSAFEARRAY FAR* ppsaFiles)
{
return (*ppsaFiles)->rgsabound[1].cElements;
}
My c# code is:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct InnerStruct
{
public int m_Int;
}
[DllImport("DimMonitorDll.dll", EntryPoint = "GetSysData", CallingConvention = CallingConvention.StdCall)]
public static extern int GetSysData([MarshalAs(UnmanagedType.LPArray)] ref InnerStruct[] x);
public void run()
{
InnerStruct[] test_struct;
test_struct = new InnerStruct[5];
var count = GetSysData(ref test_struct);
}
The problem is that I get count = 0 .
I have tried many things I found on the web, but I didn't success to figure it out.
I will appreciate any help,
Thanks!

C# P/Invoke 2 dimensional array

I am trying to call a C function from C# code:
typedef char IPTriggerNameType[256];
typedef unsigned long COMM_HANDLE;
typedef BYTE GUID_TYPE[16];
typedef long LONGINT;
typedef struct {
GUID_TYPE Guid;
IPTriggerNameType Name;
}IPCAM_GENERIC_EVENT_ID;
typedef struct {
IPCAM_GENERIC_EVENT_ID EventId;
LONGINT RelatedTriggerId;
LONGINT ObsoleteEvent;
}IPCAM_GENERIC_EVENT_INFO;
typedef struct
{
LONGINT NumOfEvents;
IPCAM_GENERIC_EVENT_INFO *GenericEventsList;
}VID_CHANNEL_GENERIC_EVENTS_STRUCT;
int __stdcall GetGenericEvents(
/* Inputs: */
COMM_HANDLE Handle,
LONGINT MaxNumOfChannelsInTable,
LONGINT MaxNumOfEventsPerChannel,
/* Outputs: */
LONGINT *NumOfChannels,
VID_CHANNEL_GENERIC_EVENTS_STRUCT *ChannelsEventsTable);
and the C# equivalent is as follows:
[StructLayout(LayoutKind.Sequential)]
public struct IPCAM_GENERIC_EVENT_ID
{
[MarshalAs(UnmanagedType.LPArray, SizeConst = 16)]
public byte[] Guid;
[MarshalAs(UnmanagedType.LPArray, SizeConst = 256)]
public char[] Name;
};
[StructLayout(LayoutKind.Sequential)]
public struct IPCAM_GENERIC_EVENT_INFO
{
public IPCAM_GENERIC_EVENT_ID EventId;
public int RelatedTriggerId;
public int ObsoleteEvent;
}
[StructLayout(LayoutKind.Sequential)]
public struct VID_CHANNEL_GENERIC_EVENTS_STRUCT
{
public int NumOfEvents;
[MarshalAs(UnmanagedType.LPArray, SizeConst = 100)]
public IPCAM_GENERIC_EVENT_INFO[] GenericEventsList;
}
[DllImport(dllName)]
public static extern int GetGenericEvents(
/* Inputs: */
int Handle,
int MaxNumOfChannelsInTable,
int MaxNumOfEventsPerChannel,
/* Outputs: */
out int NumOfChannels,
out VID_CHANNEL_GENERIC_EVENTS_STRUCT[] ChannelsEventsTable);
int numOfChannels = 16, actualNumOfChannels = 0;
int maxNumOfEvents = 100;
VID_CHANNEL_GENERIC_EVENTS_STRUCT[] genericEventsList = new VID_CHANNEL_GENERIC_EVENTS_STRUCT[numOfChannels];
for (int i = 0; i < numOfChannels; i++)
{
genericEventsList[i].GenericEventsList = new IPCAM_GENERIC_EVENT_INFO[maxNumOfEvents];
}
GetGenericEvents(conn, numOfChannels, maxNumOfEvents, out actualNumOfChannels, out genericEventsList);
when calling the C# method I get exception which crashes the application:
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a
problem in 'My.exe'.
Additional information: The runtime has encountered a fatal error. The
address of the error was at 0x72a6cf41, on thread 0x5a98. The error
code is 0xc0000005. This error may be a bug in the CLR or in the
unsafe or non-verifiable portions of user code. Common sources of this
bug include user marshaling errors for COM-interop or PInvoke, which
may corrupt the stack.
What am I doing wrong ?
The field:
VID_CHANNEL_GENERIC_EVENTS_STRUCT.GenericEventsList
is a pointer, declared as:
IPCAM_GENERIC_EVENT_INFO *GenericEventsList
But you are declaring it as an array, included in the struct:
[MarshalAs(UnmanagedType.LPArray, SizeConst = 100)]
public IPCAM_GENERIC_EVENT_INFO[] GenericEventsList;
You may try to use an IntPtr and Marshal.AllocHGlobal. Check this out: How use pinvoke for C struct array pointer to C#

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.

How to marshal nested structure?

I have three C++ structures that I need to send as parameters when calling a DLL method from C#.
I simplified them as the following:
typedef struct
{
int data1;
int data2;
} A;
typedef struct
{
int numStructA;
A *pStructA;
int moreData;
} B;
typedef struct
{
TCHAR *pStr;
B structB;
} C;
The following is the C++ function I need to call from C#:
int func(C *pStructC, int numStructC);
In my C# code, I define the structs as the following:
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct A
{
public int data1;
public int data2;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct B
{
public int numStuctA;
public IntPtr structA;
public int moreData;
}
[StructLayout(LayoutKind.Sequential, Pack = 1)]
private struct C
{
[MarshalAs(UnmanagedType.LPTStr)]
public string Str;
[MarshalAs(UnmanagedType.Struct)]
public B structB;
}
[DllImport("somedll.dll")]
private static extern int func(IntPtr pStructC, int size);
I tried to marshal the nested structures. However, the C++ function does not receive the data correctly. So far only the data that comes before the nested structure is sent correctly to the C++ function.
How can I marshal the nested structures so the C++ method receives the correct data? Thanks in advance.

Categories

Resources