C# 调用C++的dll,通过DllImport方式。
以下例子包含以下几种参数传递方式:
传递string、Int、Int数组、结构体、结构体数组。
比较懒,没写注释,呵呵,各位辛苦了。
C++ Dll 如下:
// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"
#include <stdio.h>
#include <tchar.h>
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
struct PersonInfo
{
int id;
wchar_t name;
};
struct StudentInfo
{
int id;
wchar_t name[100];
};
int Add(int para1,int para2)
{
return para1+para2;
};
int TestStruct(struct PersonInfo* person)
{
person->id=10;
return 0;
};
int TestStructArray(struct PersonInfo persons[5] )
{
persons[0].id=11;
persons[0].name = L'a';
persons[1].id=22;
persons[1].name = L'b';
persons[2].id=33;
persons[2].name = L'c';
persons[3].id=44;
persons[3].name = L'd';
persons[4].id=55;
persons[4].name = L'e';
return 0;
};
int TestStructArray2(struct StudentInfo students[3] )
{
students[0].id=11;
wcscpy(students[0].name,L"aaaaaaa");
TCHAR szMsg[200] = {0};
_stprintf_s(szMsg, 200, _T("size of PersonInfo2:%d"), sizeof(StudentInfo));
::OutputDebugString(szMsg);
students[1].id=22;
wcscpy(students[1].name,L"bbbb");
students[2].id=33;
wcscpy(students[2].name,L"ccc");
return 0;
};
int TestIntArr(int tt[5])
{
tt[0]=0;
tt[1]=1;
tt[2]=2;
tt[3]=3;
tt[4]=4;
return 0;
}
int TestString(LPTSTR buffer, DWORD size)
{
wcscpy_s(buffer,size,L"eststsetwet");
return 0;
}
C++ def文件如下:
LIBRARY "Win32Dll"
EXPORTS
Add
TestStruct
TestStructArray
TestStructArray2
TestIntArr
TestString
C# 调用代码如下:
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
Loaded += new RoutedEventHandler(button1_Click);
}
private void button1_Click(object sender, RoutedEventArgs e)
{
int num1 = 0, num2 = 0;
if (!string.IsNullOrEmpty(textBox1.Text) && !string.IsNullOrEmpty(textBox2.Text))
{
num1 = Convert.ToInt32(textBox1.Text);
num2 = Convert.ToInt32(textBox2.Text);
int ret = DllTest.Add(Convert.ToInt32(textBox1.Text), Convert.ToInt32(textBox2.Text));
textBlock3.Text = ret.ToString();
}
PersonInfo p = new PersonInfo();
DllTest.TestStruct(ref p);
//int dd = p.id;
int[] intArray = new int[5];
DllTest.TestIntArr(intArray);
Debug.WriteLine(intArray);
PersonInfo[] ps = new PersonInfo[5];
DllTest.TestStructArray(ps);
int t = Marshal.SizeOf(new StudentInfo());
// var t = sizeof(PersonInfo2);
StudentInfo[] ps2 = new StudentInfo[3];
DllTest.TestStructArray2(ps2);
short[] name = ps2[0].name;
string ret13 = new string(name.Where(o => o != 0).Select(o => (char)o).ToArray());
char[] chars = new char[100];
StringBuilder builder = new StringBuilder(256);
DllTest.TestString(builder, 256);
}
}
public class DllTest
{
[DllImport("Win32Dll.dll", EntryPoint = "Add")]
public static extern Int32 Add(int a, int b);
[DllImport("Win32Dll.dll", EntryPoint = "TestStruct")]
public static extern Int32 TestStruct(ref PersonInfo person);
[DllImport("Win32Dll.dll", EntryPoint = "TestStructArray")]
public static extern Int32 TestStructArray([In, Out] PersonInfo[] persons);
[DllImport("Win32Dll.dll", EntryPoint = "TestStructArray2")]
public static extern Int32 TestStructArray2([In, Out] StudentInfo[] students);
[DllImport("Win32Dll.dll", EntryPoint = "TestIntArr")]
public static extern Int32 TestIntArr([In, Out] int[] intArray);
[DllImport("Win32Dll.dll", EntryPoint = "TestString")]
public static extern Int32 TestString([MarshalAs(UnmanagedType.LPTStr)]StringBuilder buffer, Int32 length);
}
[ComVisible(false), StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct PersonInfo
{
public int id;
public char name;
}
[StructLayout(LayoutKind.Sequential)]
public struct StudentInfo
{
public int id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
public short[] name;
}