This topic demonstrates one facet of C++ interoperability. For more information, see Using C++ Interop (Implicit PInvoke).
The following code examples use the
Example
The following example demonstrates how to pass a managed array to an unmanaged function. The managed function uses
| В | Copy Code |
|---|---|
// PassArray1.cpp
// compile with: /clr
#include <iostream>
using namespace std;
using namespace System;
#pragma unmanaged
void TakesAnArray(int* a, int c) {
cout << "(unmanaged) array recieved:\n";
for (int i=0; i<c; i++)
cout << "a[" << i << "] = " << a[i] << "\n";
cout << "(unmanaged) modifying array contents...\n";
for (int i=0; i<c; i++)
a[i] = rand() % 100;
}
#pragma managed
int main() {
array<int>^ nums = gcnew array<int>(5);
nums[0] = 0;
nums[1] = 1;
nums[2] = 2;
nums[3] = 3;
nums[4] = 4;
Console::WriteLine("(managed) array created:");
for (int i=0; i<5; i++)
Console::WriteLine("a[{0}] = {1}", i, nums[i]);
pin_ptr<int> pp = &nums[0];
TakesAnArray(pp, 5);
Console::WriteLine("(managed) contents:");
for (int i=0; i<5; i++)
Console::WriteLine("a[{0}] = {1}", i, nums[i]);
} | |
The following example demonstrates passing an unmanaged array to a managed function. The managed function accesses the array memory directly (as opposed to creating a managed array and copying the array content), which allows changes made by the managed function to be reflected in the unmanaged function when it regains control.
| В | Copy Code |
|---|---|
// PassArray2.cpp
// compile with: /clr
#include <iostream>
using namespace std;
using namespace System;
#pragma managed
void ManagedTakesAnArray(int* a, int c) {
Console::WriteLine("(managed) array recieved:");
for (int i=0; i<c; i++)
Console::WriteLine("a[{0}] = {1}", i, a[i]);
cout << "(managed) modifying array contents...\n";
Random^ r = gcnew Random(DateTime::Now.Second);
for (int i=0; i<c; i++)
a[i] = r->Next(100);
}
#pragma unmanaged
void NativeFunc() {
int nums[5] = { 0, 1, 2, 3, 4 };
printf_s("(unmanaged) array created:\n");
for (int i=0; i<5; i++)
printf_s("a[%d] = %d\n", i, nums[i]);
ManagedTakesAnArray(nums, 5);
printf_s("(ummanaged) contents:\n");
for (int i=0; i<5; i++)
printf_s("a[%d] = %d\n", i, nums[i]);
}
#pragma managed
int main() {
NativeFunc();
} | |
Dhtml editor
Free javascript download