本文摘自PHP中文网,作者黄舟,侵删。
在过程中发现两种方法解决问题:一种是非托管C++创建的dll库,需要用静态方法调用。这种方法无法在C#的reference中直接引用,而是要用静态调用的方法,其他博客已经介绍的很详尽,唯一需要补充的是,C#文件需要先:
1 | using System.Runtime.InteropServices;
|
之后才可以调用[DllImport]方法。
另一种方法是直接使用CLR,生成托管C++dll库。
创建流程
例程如下
C++ dll:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #pragma once
using namespace System;
namespace CPPlibdemo {
public ref class Class1
{
public :
String ^getgreating(){
return "hello world" ;
}
};
}
|
C#语言:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 的内容!
相关阅读 >>
c++学习基础知识--this指针、静态成员、常量成员函数
c++ 布尔类型和引用的用法详解
c++输出二维字符矩阵对齐
精选的这19道c/c+面试题,你能答对多少呢? - 个人文章 思否
c++隐式类型转换是什么?
新手程序员应该知道的c语言和c++的区别
【c++趣味程序】之开心消消乐
const在c++中的意思
第一章c++:函数返回值、gnu编译器命令
c源程序中不允许出现空语句吗?
更多相关阅读请进入《C#》频道 >>
转载请注明出处:木庄网络博客 » C#调用C++ 动态链接库dll