Created
March 19, 2019 14:04
-
-
Save baobao/90ef016750d439ebcbc5a2447ee70f1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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