C#调用C++DLL

  


struct YY
{
 int a;
 char* name;
};


extern WIN32PROJECT1_API int nWin32Project1;

WIN32PROJECT1_API int fnWin32Project1(YY* y);

 

 

 

 


    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {


            YY y = new YY();
            StringBuilder buffer = new StringBuilder("", 4);
            buffer.Append((char)0);


            y.name = buffer.ToString();

            MessageBox.Show(fnWin32Project1(ref y).ToString());
            MessageBox.Show(y.name);
            MessageBox.Show(y.a.ToString());
        }

        [DllImport("Win32Project1.dll", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
        private static extern int fnWin32Project1(ref YY y);

        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
        public struct YY
        {
            public int a;
            public string name;
        }
    }

你可能感兴趣的:(C#调用C++DLL)