C#调用C++动态链接库dll-创新互联
在过程中发现两种方法解决问题:一种是非托管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();
}
}
}另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
文章题目:C#调用C++动态链接库dll-创新互联
本文来源:http://www.scyingshan.cn/article/dedjgi.html


咨询
建站咨询
