10/03/29 23:35:16
これで上手くいったと書こうと思ったら>>701に先越された。CallingConvention忘れないでね。
using System;
using System.Runtime.InteropServices;
[StructLayout(LayoutKind.Sequential, Pack=4)]
public struct ST_SAMPLEDLL
{
public byte cCount;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 256)]
public byte[] strBuff;
}
class Hoge
{
[DllImport("u.dll", EntryPoint = "test", CallingConvention=CallingConvention.Cdecl)]
unsafe public static extern int test_call([MarshalAs(UnmanagedType.LPArray), In, Out] IntPtr[] pstList);
static void Main()
{
IntPtr[] stSample = new IntPtr[10];
for (int i = 0; i < stSample.Length; ++i)
{
stSample[i] = Marshal.AllocCoTaskMem(Marshal.SizeOf(typeof(ST_SAMPLEDLL)));
}
// DLL呼び出し
test_call(stSample);
ST_SAMPLEDLL[] st = new ST_SAMPLEDLL[10];
for (int i = 0; i < st.Length; ++i)
{
st[i] = (ST_SAMPLEDLL)Marshal.PtrToStructure(stSample[i], typeof(ST_SAMPLEDLL));
Marshal.FreeCoTaskMem(stSample[i]);
}
Console.WriteLine((int)st[0].cCount + " " + (int)st[0].strBuff[0]);
Console.WriteLine((int)st[1].cCount + " " + (int)st[1].strBuff[0]);
}}