【问题标题】:Pointer to a pointer C++/CLI wrapper指向指针 C++/CLI 包装器的指针
【发布时间】:2013-10-24 20:45:08
【问题描述】:

我正在尝试为非托管类编写托管 C++/CLI 包装器。类中的一个方法有一个类似的签名

audiodecoder::Decode(byte *pEncodedBuffer, unsigned int uiEncodedBufLen, byte **pDecodedAudio, unsigned int *uiDecodedAudioLen)

其中 *pEncodedBuffer 是指向编码音频样本的指针,而 **pDecodedAudio 是函数将初始化内存并存储解码音频的位置。实际上,数据是音频应该没有任何意义。

这个方法的包装器会是什么样子?任何建议都会有所帮助。

【问题讨论】:

  • 返回类型是什么?
  • 返回类型是bool

标签: c++ pointers c++-cli


【解决方案1】:

托管代码的一个优点是它使用垃圾收集器并具有强类型数组。因此,很好地支持将数组作为函数返回值返回。这使您理想的包装器功能如下:

  array<Byte>^ Wrapper::Decode(array<Byte>^ encodedBuffer) {
      pin_ptr<Byte> encodedPtr = &encodedBuffer[0];
      Byte* decoded = nullptr;
      unsigned int decodedLength
      int err = unmanagedDecoder->Decode(encodedPtr, encodedBuffer->Length, &decoded, &decodeLength);
      // Test err, throw an exception
      //...
      array<Byte>^ retval = gcnew array<Byte>(decodedLength);
      Marshal::Copy((IntPtr)decoded, retval, 0, decodedLength);
      free(decoded);   // WATCH OUT!!!
      return retval;
  }

请注意// WATCH OUT 评论。您需要销毁解码器分配的缓冲区。正确执行此操作需要了解解码器如何管理其内存,并且通常非常重要的是,您的 C++/CLI 代码与解码器模块共享相同的 CRT。如果没有好的协议或者无法编译解码器源代码,这往往会出错。

【讨论】:

    猜你喜欢
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 2016-01-26
    • 2021-08-14
    • 1970-01-01
    • 2011-04-24
    • 2021-04-07
    • 2019-09-10
    相关资源
    最近更新 更多