Hi,
I have a native c++ code with structure as follows:
typedef struct
{
unsigned long ProtocolID;
unsigned long RxStatus;
unsigned long TxFlags;
unsigned long Timestamp;
unsigned long DataSize;
unsigned long ExtraDataIndex;
BYTE Data[4128];
} PASSTHRU_MSG;
I 'm trying to write a wrapper in c++/CLI , so i have defined structure as follows:
[StructLayout(LayoutKind::Sequential)]
ref struct PASSTHRU_MSG_X
{
PASSTHRU_MSG_X()
{
this->data = gcnew array<byte>(4128);
}
UInt32 ProtocolID;
UInt32 RxStatus;
UInt32 TxFlags;
UInt32 Timestamp;
UInt32 DataSize;
UInt32 ExtraDataIndex;
array<BYTE> ^data;
} ;
Now I have a function like this:
long CPassThruWrapper::PassThruReadMsgs(UInt32 channelId, array<PASSTHRU_MSG_X^> ^ respMsgs, UInt32 &numResponses, UInt32 timeout)
{
PASSTHRU_MSG* responses = new PASSTHRU_MSG[respMsgs->Length];
int n = respMsgs->Length;
unsigned long numResp;
long val = NativePassThruReadMsgs(channelId, responses, &numResp, timeout);
for (int i=0;i<1;i++)
{
PASSTHRU_MSG_X ^msg = static_cast<PASSTHRU_MSG_X^>(Marshal::PtrToStructure(IntPtr(&responses[i]), PASSTHRU_MSG_X::typeid));
}
return val;
}
Here I'm trying to convert responses which is an array of native structure PASSTHRU_MSG and just tried to marshal first structure element to managed strcture PASSTHRU_MSG_X.
When i tried this: It threw exception:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Please note I tried same code by commenting byte array in both structures and it worked without any problem.
Can somebody help me in pointing mistake I'm doing in declaring byte array inside that structure so that this code works properly.
Thanks in advance