Skip to content

Instantly share code, notes, and snippets.

@baobao
Created March 19, 2019 14:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save baobao/90ef016750d439ebcbc5a2447ee70f1f to your computer and use it in GitHub Desktop.
Save baobao/90ef016750d439ebcbc5a2447ee70f1f to your computer and use it in GitHub Desktop.
[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