C#调用C++ 动态链接库dll
在过程中发现两种方法解决问题:一种是非托管C++创建的dll库,需要用静态方法调用。这种方法无法在C#的reference中直接引用,而是要用静态调用的方法,其他博客已经介绍的很详尽,唯一需要补充的是,C#文件需要先:
usingSystem.Runtime.InteropServices;
之后才可以调用[DllImport]方法。
另一种方法是直接使用CLR,生成托管C++dll库。
创建流程
例程如下
C++ dll:
//CPPlibdemo.h#pragmaonceusingnamespaceSystem;namespaceCPPlibdemo{publicrefclassClass1{//TODO:Addyourmethodsforthisclasshere.public:String^getgreating(){return"helloworld";}};}
C#语言:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingCPPlibdemo;namespaceConsoleApplication5{classProgram{staticvoidMain(string[]args){Class1clrdemo=newClass1();Console.Write(clrdemo.getgreating());Console.ReadLine();}}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。