编组Ptr结构抛出异常 [英] Marshalling Ptr to structure throws exception

查看:65
本文介绍了编组Ptr结构抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我有一个原生的c ++代码,其结构如下:

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;





我正在尝试用c ++ / CLI编写包装器,所以我将结构定义为如下:



[StructLayout(LayoutKind :: Sequential)]



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;

    } ;







现在我有这样的功能:< br $>





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;

    //This is native call>> where I get array of structure PASSTHRU_MSG in responses
    long val = NativePassThruReadMsgs(channelId, responses, &numResp, timeout);    

    for (int i=0;i<1;i++)
    {
        // Here my function throws exception saying attempted to read /write memory which is    write protected 
        PASSTHRU_MSG_X ^msg = static_cast<PASSTHRU_MSG_X^>(Marshal::PtrToStructure(IntPtr(&responses[i]), PASSTHRU_MSG_X::typeid));
    }    

    return val;

}





这里我试图转换响应,这是一个原生结构PASSTHRU_MSG和只是试图将第一个结构元素编组到托管结构PASSTHRU_MSG_X。

当我尝试这个:它抛出异常:



试图读或写受保护的记忆这通常表明其他内存已损坏。



请注意我通过在两个结构中注释字节数组来尝试相同的代码,并且它没有任何问题。

有人可以帮助我指出错误我正在声明该结构中的字节数组,以便此代码正常工作。

提前谢谢



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

推荐答案

我为此找到了解决方案:



我改变了这样的托管结构并且它有效!!



I found solution for this:

I changed managed structure like this and it worked!!

[StructLayout(LayoutKind::Sequential)]
    ref struct PASSTHRU_MSG_X
        {
          UInt32 ProtocolID;
          UInt32 RxStatus;
          UInt32 TxFlags;
          UInt32 Timestamp;
          UInt32 DataSize;
          UInt32 ExtraDataIndex;
          [MarshalAs(UnmanagedType::ByValArray, SizeConst = 4128)]
          array<BYTE>^ data;
        } ;


这篇关于编组Ptr结构抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆