Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[DllImport("TestDll.dll", CallingConvention = CallingConvention.Cdecl)]
static extern void TestIntArray(System.IntPtr array, int length);
static void Main()
{
// 配列要素数
const int Length = 5;
var array = new int[Length] { 0, 1, 2, 3, 4 };
// アンマネージド配列のメモリ確保
System.IntPtr ptr = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(int)) * Length);
// マネージド配列をアンマネージドにコピーする
Marshal.Copy(array, 0, ptr, Length);
// C++に配列を渡す(ポインタを渡す)
TestIntArray(ptr, Length);
// アンマネージドのメモリを解放
Marshal.FreeCoTaskMem(ptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment