一、非托管C++建立的dll库,须要用静态方法调用;php
二、直接使用CLR,生成托管C++dll库。web
不少时候在项目中须要经过C++调用C#的dll,或者反过来调用。首先明白一个前提:C#是托管型代码。C++是非托管型代码。
托管型代码的对象在托管堆上分配内存,建立的对象由虚拟机托管。(C# )
非托管型代码对象有实际的内存地址,建立的对象必须本身来管理和释放。(C++)app
一、打开VS建立C++项目”C++_CScharp_DLL”svg
点击肯定以后接着点击下一步:函数
而后选择应用程序和附加选项:oop
点击完成,C++的项目就新建好了。测试
二、添加代码文件ui
右键项目,添加类,以下图所示:this
添加类以后会打开添加文件对话框,点击添加便可,以下图所示:spa
点击肯定以后进去下一个对话框,填写文件名Function,以下图所示:
添加好后会生成h文件和cpp文件,以下图所示:
Function.h文件代码以下:
#pragma once
#include <string>
public ref class Function {
public:
Function(void);
~Function(void);
int menber;
int menberFuncAdd(int a,int b);
System::String^ say(System::String^ str);
};
//.cpp
#include "Function.h"
Function::Function(void) {
}
Function::~Function(void) {
}
int Function::menberFuncAdd(int a,int b) {
return a+b;
}
System::String^ Function::say(System::String^ str) {
return str;
}
填写完后Function.h文件会报错,错误类型以下:
这里须要在C++项目里面设置,让动态库受到公共语言运行时的支持。以下图所示:
打开项目属性
修改完成后点击项目右键生成DLL,看是否报错,成功结果以下图:
三、添加测试程序:
在该解决方案中添加测试程序:
添加一个C#控制台测试程序:
添加完后设为启动项:
添加引用:
将C++项目添加到C#的项目中:
四、编写测试代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Test {
class Program {
static void Main(string[] args)
{
Function fun = new Function();
Console.WriteLine(fun.menberFuncAdd(1, 2));
Console.WriteLine(fun.say("Hello World"));
Console.ReadKey();
}
}
}
非托管模式从功能上来讲,只支持函数调用,直接调用C++类库中的公共方法,在被导出的函数前面必定要添加额extern “C来指明导出函数的时候使用C语言方式编译和连接的,这样保证函数定义的名字相同,不然若是默认按C++方式导出,那个函数名字就会变得乱七八糟,咱们的程序就没法找到入口点了。
“__declspec(dllexport)”意思是将后面修饰的内容定义为DLL中要导出的内容。固然你也能够不使用这个宏,能够直接将”__declspec(dllexport)”写在要导出的函数前面。C++中定义以下:
extern "C" __declspec(dllexport) int __stdcall Add(int n1, int n2);
在C#中,调用以下:
[DllImport(“SampleCppWrapper.dll”)]
private static extern int Add(int n1, int n2);
注意:
在编译C++DLL以前,须要作如下配置,在项目属性对话框中选择”C/C++”|”Advanced”,将Compile AS 选项的值改成”C++”。而后肯定,并编译。
如下为摘录例子:
一、 调用C++类库中的类的方法
C#不能直接调用C++类库中的类,须要一种变通的解决方式,经过再作一个C++类库把要调用的类成员方法暴露出来,好比下面这个C++类:
//SampleCppClass.h
#pragma once
class __declspec(dllexport) SampleCppClass
{
public:
SampleCppClass(void);
~SampleCppClass(void);
int Add(int n1, int n2);
int Sub(int n1, int n2);
};
//SampleCppClass.cpp
#include "SampleCppClass.h"
SampleCppClass::SampleCppClass(void)
{
}
SampleCppClass::~SampleCppClass(void)
{
}
int SampleCppClass::Add(int n1, int n2)
{
return n1 + n2;
}
int SampleCppClass::Sub(int n1, int n2)
{
return n1 - n2;
}
咱们要调用SampleCppClass中的Add和Sub两个方法,因此咱们再写一个C++类库,经过公共方法间接调用类成员方法:
//SampleCppWrapper.h
#pragma once
#include "..\SampleCppClass\SampleCppClass.h"
namespace SampleCppWrapper {
extern "C" __declspec(dllexport) int __stdcall Add(int n1, int n2);
extern "C" __declspec(dllexport) int __stdcall Sub(int n1, int n2);
}
//SampleCppWrapper.cpp
#include "SampleCppWrapper.h"
namespace SampleCppWrapper {
SampleCppClass* g_pObj = new SampleCppClass();
int __stdcall Add(int n1, int n2)
{
return g_pObj->Add(n1, n2);
}
int __stdcall Sub(int n1, int n2)
{
return g_pObj->Sub(n1, n2);
}
}
在C#中,再调用SampleCppWrapper.dll中的公共方法:
[DllImport("SampleCppWrapper.dll")]
private static extern int Add(int n1, int n2);
[DllImport("SampleCppWrapper.dll")]
private static extern int Sub(int n1, int n2);
三、 使用C++类库中的回调函数
C++的回调函数是一种事件响应机制,和C#的委托类似,好比一个C++类中的回调函数:
// SampleCppClass.h
#pragma once
typedef void (*LoopCallback)(void* pContext);
class __declspec(dllexport) SampleCppClass
{
public:
SampleCppClass(void);
~SampleCppClass(void);
void SetCallbackFunc(LoopCallback callback);
void SetCallbackContext(void* pContext);
void Loop();
private:
LoopCallback m_callback;
void* m_pContext;
};
// SampleCppClass.cpp
#include "SampleCppClass.h"
SampleCppClass::SampleCppClass(void)
{
}
SampleCppClass::~SampleCppClass(void)
{
}
void SampleCppClass::SetCallbackFunc(LoopCallback callback)
{
m_callback = callback;
}
void SampleCppClass::SetCallbackContext(void* pContext)
{
m_pContext = pContext;
}
void SampleCppClass::Loop()
{
for (int i=0; i<10; i++)
{
if (m_callback != NULL)
{
m_callback(m_pContext);
}
}
}
导出方法文件中添加:
//.h
#pragma once
#include "..\SampleCppClass\SampleCppClass.h"
namespace SampleCppWrapper
{
typedef void (__stdcall *LoopCallbackWrapper)(void* pContext);
extern "C" __declspec(dllexport) void __stdcall SetCallbackFunc(LoopCallbackWrapper callback);
extern "C" __declspec(dllexport) void __stdcall SetCallbackContext(void* pContext);
extern "C" __declspec(dllexport) void __stdcall Loop();
}
// .CPP
#include "SampleCppWrapper.h"
namespace SampleCppWrapper
{
LoopCallbackWrapper g_callbackWrapper;
SampleCppClass* g_pObj = new SampleCppClass();
void LoopCallbackFunc(void* pContext);
void __stdcall SetCallbackFunc(LoopCallbackWrapper callback)
{
g_callbackWrapper = callback;
g_pObj->SetCallbackFunc(LoopCallbackFunc);
}
void __stdcall SetCallbackContext(void* pContext)
{
g_pObj->SetCallbackContext(pContext);
}
void __stdcall Loop()
{
g_pObj->Loop();
}
void LoopCallbackFunc(void* pContext)
{
if (g_callbackWrapper != NULL)
{
g_callbackWrapper(pContext);
}
}
}
C#中调用:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SampleCsTest
{
public partial class Form1 : Form
{
[StructLayout(LayoutKind.Sequential)]
private class Context
{
public Form1 Form { get; set; }
}
private delegate void LoopCallbackHandler(IntPtr pContext);
private static LoopCallbackHandler callback = LoopCallback;
[DllImport("SampleCppWrapper.dll")]
private static extern void SetCallbackFunc(LoopCallbackHandler callback);
[DllImport("SampleCppWrapper.dll")]
private static extern void SetCallbackContext(IntPtr pContext);
[DllImport("SampleCppWrapper.dll")]
private static extern void Loop();
private Context ctx = new Context();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SetCallbackFunc(callback);
ctx.Form = this;
IntPtr ptr = Marshal.AllocHGlobal(Marshal.SizeOf(ctx));
Marshal.StructureToPtr(ctx, ptr, false);
SetCallbackContext(ptr);
}
private void button1_Click(object sender, EventArgs e)
{
Loop();
}
private static void LoopCallback(IntPtr pContext)
{
Context ctx = (Context)Marshal.PtrToStructure(pContext, typeof(Context));
ctx.Form.textBox1.Text += "callback" + Environment.NewLine;
}
}
}
以上为非托管方式简单参数的传递,实际使用过程当中,可能参数类型会复杂不少,这牵涉到C# C++之间的参数转换及C#语法对托管代码的编写,具体作一些项目时,确定会比例子状况复杂的多,那就须要对各类参数传递及转换好好了解一番,若是解决了各类状况参数传递问题,基本C#调用非托管C++dll没有其它复杂问题。 管廊程序中有相关C#调用C++的DLL 能够参考使用,不过管廊中生成的都是托管DLL.