在过程中发现两种方法解决问题:一种是非托管c++创建的dll库,需要用静态方法调用。这种方法无法在c#的reference中直接引用,而是要用静态调用的方法,其他博客已经介绍的很详尽,唯一需要补充的是,c#文件需要先:
using system.runtime.interopservices;
之后才可以调用[dllimport]方法。
另一种方法是直接使用clr,生成托管c++dll库。
创建流程
例程如下
c++ dll:
// cpplibdemo.h
#pragma once
using namespace system;
namespace cpplibdemo {
public ref class class1
{
// todo: add your methods for this class here.
public:
string ^getgreating(){
return "hello world";
}
};
}
c#语言:
using system;
using system.collections.generic;
using system.linq;
using system.text;
using system.threading.tasks;
using cpplibdemo;
namespace consoleapplication5
{
class program
{
static void main(string[] args)
{
class1 clrdemo = new class1();
console.write(clrdemo.getgreating());
console.readline();
}
}
}
以上就是c#调用c++ 动态链接库dll 的内容。