在过程中发现两种方法解决问题:一种是非托管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();
}
}
}
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。