HOW TO:將 Unmanaged 資源載入至位元組陣列

本主題會討論數種將 Unmanaged 資源載入至 Byte 陣列的方式。

範例

如果您知道 Unmanaged 資源的大小,則您可以預先配置 CLR 陣列,然後使用指向 CLR 陣列之陣列區塊的指標,將資源載入至陣列。

// load_unmanaged_resources_into_Byte_array.cpp
// compile with: /clr
using namespace System;
void unmanaged_func( unsigned char * p ) {
   for ( int i = 0; i < 10; i++ )
      p[ i ] = i;
}

public ref class A {
public:
   void func() {
      array<Byte> ^b = gcnew array<Byte>(10);
      pin_ptr<Byte> p =  &b[ 0 ];
      Byte * np = p;
      unmanaged_func( np );   // pass pointer to the block of CLR array.
      for ( int i = 0; i < 10; i++ )
         Console::Write( b[ i ] );
      Console::WriteLine();
   }
};

int main() {
   A^ g = gcnew A;
   g->func();
}
  

這個範例會顯示如何將資料從 Unmanaged 記憶體區塊複製到 Managed 陣列。

// load_unmanaged_resources_into_Byte_array_2.cpp
// compile with: /clr
using namespace System;
using namespace System::Runtime::InteropServices;

#include <string.h>
int main() {
   char buf[] = "Native String";
   int len = strlen(buf);
   array<Byte> ^byteArray = gcnew array<Byte>(len + 2);
   
   // convert any native pointer to IntPtr by doing C-Style cast
   Marshal::Copy( (IntPtr)buf, byteArray, 0, len );
}

請參閱

參考

使用 C++ Interop (隱含 PInvoke)