Skip to content

Latest commit

 

History

History
70 lines (57 loc) · 1.91 KB

how-to-load-unmanaged-resources-into-a-byte-array.md

File metadata and controls

70 lines (57 loc) · 1.91 KB
descriptiontitlems.customms.datehelpviewer_keywordsms.assetid
Learn more about: How to: Load Unmanaged Resources into a Byte Array
How to: Load Unmanaged Resources into a Byte Array
get-started-article
11/04/2016
native resources, loading into Byte array
unmanaged resources, loading into Byte array
native resources
cdada6cd-6d42-437a-a90f-44a0b18d6a93

How to: Load Unmanaged Resources into a Byte Array

This topic discusses several ways to load unmanaged resources into a xref:System.Byte array.

Examples

If you know the size of your unmanaged resource, you can preallocate a CLR array and then load the resource into the array using a pointer to the array block of the CLR array.

// load_unmanaged_resources_into_Byte_array.cpp// compile with: /clrusingnamespaceSystem;voidunmanaged_func( unsignedchar * p ) { for ( int i = 0; i < 10; i++ ) p[ i ] = i; } public ref classA { public:voidfunc() { 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(); } }; intmain() { A^ g = gcnew A; g->func(); }
0123456789 

This sample shows how to copy data from an unmanaged memory block to a managed array.

// load_unmanaged_resources_into_Byte_array_2.cpp// compile with: /clrusingnamespaceSystem;usingnamespaceSystem::Runtime::InteropServices; #include<string.h>intmain() { 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 castMarshal::Copy( (IntPtr)buf, byteArray, 0, len ); }

See also

Using C++ Interop (Implicit PInvoke)

close