В файле Win32Project1.cpp напишем код
// Win32Project1.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
struct Book
{
int Price;
char* Name;
};
extern "C" __declspec(dllexport) void MyText(LPCTSTR text)
{
::MessageBox(NULL, text, "My Message1", MB_OK);
}
extern "C" __declspec(dllexport) void MyText2(Book* pBook)
{
::MessageBox(NULL, pBook->Name, "My Message2", MB_OK);
}
C#
В файле Program.cs напишем код
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
[StructLayout(LayoutKind.Sequential)]
struct Book
{
public int Price;
public string Name;
}
class Program
{
[DllImport("Win32Project1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void MyText(string text);
[DllImport("Win32Project1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void MyText2(ref Book book);
static void Main(string[] args)
{
MyText("Hello");
Book book = new Book();
book.Price = 62;
book.Name = "Властелин Колец";
MyText2(ref book);
}
}
}
Чтобы структура передалась из
C++ в
C# нужно использовать
атрибут StructLayout