using
System;
using
System.Runtime.InteropServices;
namespace
test1
{
internal
class
Program
{
[DllImport(
"Dll1.dll"
)]
static
extern
Int32 dllInt32ArrayPointer(
ref
IntPtr arr,
ref
Int32 size);
static
void
Main(
string
[] args)
{
Int32[] arr =
new
Int32[] { 1, 2, 3, 10, 22 };
Int32 size = arr.Length;
IntPtr buffer = Marshal.AllocCoTaskMem(Marshal.SizeOf(arr[0] * size));
Marshal.Copy(arr, 0, buffer, size);
Int32 newArraySize = dllInt32ArrayPointer(
ref
buffer,
ref
size);
if
(size > 0)
{
Int32[] newArray =
new
int
[newArraySize];
Marshal.Copy(buffer, newArray, 0, newArraySize);
Marshal.FreeCoTaskMem(buffer);
foreach
(var e
in
newArray)
{
Console.WriteLine(e);
}
}
else
{
Console.WriteLine(
"C++側で新たに設定された配列は空っぽです。"
);
}
Console.ReadKey();
}
}
}