방법: 관리되지 않는 리소스를 바이트 배열로 로드

이 항목에서는 관리되지 않는 리소스를 배열에 로드하는 여러 가지 방법에 대해 Byte 설명합니다.

예제

관리되지 않는 리소스의 크기를 알고 있는 경우 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();
}
0123456789

이 샘플에서는 관리되지 않는 메모리 블록에서 관리되는 배열로 데이터를 복사하는 방법을 보여줍니다.

// 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)