Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Native C++, the structure looks like below.
C++
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.
C++
[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)
C++
	EmployeeDB_CLR ^hclrDetails; //assume data is present here

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++)
		{
			// Copy the managed struct to unmanaged memory.
			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.
C++
        EmployeeDB_CLR ^hclrDetails; //assume data is present here
        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.
Posted
Comments
Philippe Mori 14-May-15 12:45pm    
Are you sure that your native BOOL are 1 byte and not. 4? Better to verify sizes and offsets.
eswarpabolu 14-May-15 12:50pm    
Thanks for the reply. Actually i was using typedef BYTE BOOLEAN; and pack(1) so, it is allocated to 1byte.
eswarpabolu 15-May-15 0:22am    
any help if possible? @SaschaLefevre
eswarpabolu 15-May-15 0:24am    
Any better solution than this? @Nish-Nishant

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900