In Native C++, the structure looks like below.
typedef struct _DB_EMPLOYEE_DETAILS_N
{
OUT ULONG ulNumber;
OUT BOOLEAN bActive;
OUT BOOLEAN bPermanent;
} DB_EMPLOYEE_DETAILS_N, *PDB_EMPLOYEE_DETAILS_N;
typedef struct _Final_EmployeeDB_N {
OUT ULONG ulcount;
OUT DB_EMPLOYEE_DETAILS_N stEmployeeArgs[30];
} Final_EmployeeDB_N, *PFinal_EmployeeDB_N;
In Managed C++, the structure looks like below.
[StructLayoutAttribute(LayoutKind::Sequential, Pack = 1)]
public value struct EMPLOYEE_DETAILS_CLR
{
public:
UInt32 ulNumber;
[MarshalAsAttribute(UnmanagedType::I1)]
bool bActive;
[MarshalAsAttribute(UnmanagedType::I1)]
bool bPermanent;
};
[StructLayoutAttribute(LayoutKind::Sequential, Pack = 1)]
public ref class EmployeeDB_CLR {
public:
UInt32 ulcount;
array<EMPLOYEE_DETAILS_CLR> ^stEmployeeArgs = gcnew array<EMPLOYEE_DETAILS_CLR>(30);
};
I need help in two aspects.
1) How to declare the fixed size array in Managed C++? I got some info from the
Link to declare as inline_array. But is there any other way to perform this operation?
2) I want to convert the structures from managed C++ to native C++ and viceversa.
The below code converts the managed code to unmanaged.
I could achieve this in two ways:
1)
EmployeeDB_CLR ^hclrDetails;
IntPtr UnmanagedMemAddr = Marshal::AllocHGlobal(Marshal::SizeOf(hclrDetails->stEmployeeArgs[0]));
Final_EmployeeDB_N stDetails = { 0 };
PFinal_EmployeeDB_N pDetails = &stDetails;
stDetails.ulcount = hclrDetails->ulcount;
for (int i = 0; i < hEnumDeviceArgs->stEmployeeArgs->Length; i++)
{
Marshal::StructureToPtr(hclrDetails->stEmployeeArgs[i],
UnmanagedMemAddr,
false);
stDetails.stEmployeeArgs[i] = *static_cast<PDB_EMPLOYEE_DETAILS_N>(UnmanagedMemAddr.ToPointer());
}
2) I tried by using pin_Ptr<> and memcpy and it worked.
EmployeeDB_CLR ^hclrDetails; Final_EmployeeDB_N stDetails = { 0 };
pin_ptr<EMPLOYEE_DETAILS_CLR> tempPtr = &(hclrDetails->stEmployeeArgs[0]);
memcpy(stDetails.stEmployeeArgs, tempPtr, hclrDetails->stEmployeeArgs->Length);
Any suggestions are welcome. Thanks in advance.