栏目分类:
子分类:
返回
终身学习网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
终身学习网 > IT > 软件开发 > 后端开发 > .Net

通过C++/CLI将C++与C#进行联合编程

.Net 更新时间:发布时间: 百科书网 趣学号
通过C++/CLI将C++与C#进行联合编程
官网

可以使用C++/CLI在C++和.Net之间联合编程,C++/CLI是一个两栖模块,它具有如下特点

  1. 既可以访问.Net类库,也可以访问C++原生类库
  2. 既可以被.Net程序引用,也可以被C++原生程序引用

使用C++/CLI,可以使用C++编写算法,用C#编写界面


C++/CLI(Common Language Infrastructure,通用语言框架)可以简单的认为就是.NET运行的托管C++


C#可以调用Native C++类,也可以调用C++/CLI类,但是区别非常大。

  1. C#可以调用Native C++类类型,无论使用Pinvoke还是其他方法,Native类类型作为参数都很难处理
  2. C#可以直接调用ref类,和.net类库内其他类一样
    1.ref类的变量(字段)不能使Native C++类类型,可以是指向Native C++类类型对象的指针
    2.ref类内部可以直接使用Native C++类类型,只需要导入相关头文件即可
    3.ref类的函数(方法)参数可以直接使用Native C++类类型,但是C#无法理解这些参数。也就是说,public函数不要用Native C++类类型作为参数类型

C++/CLI声明的类必须是ref类,即声明方式只能是:

ref class major{};
ref struct major{};

C++/CLI调用C#,在C++/CLI文件内引用C#类库文件和命名空间即可

#using "LMath.dll"
using namespace My::LMath;

一 在VS2017/2019 安装 C++/CLI的模块支持



二 新建C++/CLI项目

三 添加源文件

String^ fileName = "textfile.txt";
StreamWriter^ sw = gcnew StreamWriter(fileName);

If you use the sample program, notice thatyou use the gcnew keyword instead of new when creating a .NET object,and that gcnew returns a handle( ^ ) rather than a pointer ( * ):


using namespace System;
using namespace System::IO;

int main()
{
	String^ fileName = "textfile.txt";
	StreamWriter^ sw = gcnew StreamWriter(fileName);
	sw->WriteLine("Hi Major");
	sw->Close();
	Console::WriteLine("a new file ('{0}') has been written", fileName); 
	return 0;
}



C++/CLI Tasks
C++/CLI Wrapper
public ref class Point;
  int x,y;
public ref class ContourFinder
   void load_picture(System::String^ file_name);
   List^>^ find_contour(int threshold);
   property int Rows {int get();}
   property int cols {int get();}
参考文档
  1. https://www.cnblogs.com/TianFang/p/4931879.html
「C++学习笔记」面向.Net Core的(C++)CLR类库非专业入门(+使用Opencv)
转载请注明:文章转载自 www.051e.com
本文地址:http://www.051e.com/it/985076.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 ©2023-2025 051e.com

ICP备案号:京ICP备12030808号