2017年6月1日 星期四

Passing struct pointer from C++ dll to C#


#include <stdio.h>
#include <Windows.h>
struct Name
{
char FirstName[100];
char LastName[100];
char *Array[3];
};
extern "C" __declspec(dllexport) void __cdecl GetName(struct Name *name)
{
strncpy_s(name->FirstName, "FirstName", sizeof(name->FirstName));
name->Array[0] = "Foo 0";
name->Array[1] = "Foo 1";
name->Array[2] = "Foo 2";
}
extern "C" __declspec(dllexport) void __cdecl Hello()
{
printf("Hello\n");
}
using System;
using System.Runtime.InteropServices;
namespace TestApp
{
class Program
{
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Name
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string FirstName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 100)]
public string LastName;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public string[] Array;
};
[DllImport("TestDll.dll")]
public static extern void GetName(ref Name name);
[DllImport("TestDll.dll")]
public static extern void Hello();
static void Main(string[] args)
{
Hello();
var name = new Name();
GetName(ref name);
Console.WriteLine(name.FirstName);
foreach (var s in name.Array)
Console.WriteLine(s);
}
}
}

沒有留言:

張貼留言