在过程中发现两种方法解决问题:一种是非托管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 的内容,更多相关内容请关注PHP中文网(www.php.cn)!
立即学习“C++免费学习笔记(深入)”;
.dll文件缺失怎么办?.dll文件在哪下载?不用担心,这里为大家提供了所有的.dll文件下载,无论用户丢失的是什么.dll文件,在这里都能找到。用户保存后,在网盘搜索dll文件全称即可查找下载!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号