Lv.的博客

C++/CLI入门系列 第二篇:封装C++ dll库,提供接口给C#调用

看了第一篇感觉没啥用对吧,来点稍微有用的。
1、先建个c#工程,依次 file -> new -> project,选择 visula c# -> console application,写工程名,点 ok。

 

 

2、再建个c++ dll工程。依次 file -> add -> new project。选择 visual -> win32 console application,点 ok -> next,选择 dll -> finish。

 

 

 

 


3、建立cli工程。依次 file -> add -> new project。选择 visula c++ -> clr -> class library,写工程名,点 ok。

 

 

4、创建结束,开始配种,啊呸!配置。
1)、c#工程默认平台any cpu,强迫症犯了,改成x86,编译下,生成目标debug路径:.\bin\x86\debug。
2)、修改c++工程输出目录、cli工程输出目录、cli工程库引入路径等为上方目录。
3)、修改cli引入头文件路径为包含CppDll.h的路径。
4)、依次编译c++工程,cli工程
5)、c#工程导入CliDll,依次选择 c#工程 -> references -> 点右键 -> add reference -> browse -> 选择debug路径下由cli工程生成的dll文件。
6)、编译c#工程,最后运行。

 

 

附源码:
github仓库项目地址:git@github.com:fx-odyssey/CS_Cli_Cpp.git(vs2008工程,打开可直接运行)

//CppDll.h

#pragma once

#include <stdio.h>

#include <stdlib.h>

 

#ifdef CPPDLL_EXPORTS

#define CPP_EXPORTS __declspec(dllexport)

#else

#define CPP_EXPORTS __declspec(dllimport)

#endif

 

extern "C" CPP_EXPORTS int Add(int a, int b);

 

extern "C" CPP_EXPORTS int Sub(int a, int b);

 

extern "C" CPP_EXPORTS int Mul(int a, int b);

 

extern "C" CPP_EXPORTS int Div(int a, int b);

 

***************************************

 

// CppDll.cpp

#include "stdafx.h"

#include "CppDll.h"

 

int Add(int a, int b)

{

return a + b;

}

 

int Sub(int a, int b)

{

return a - b;

}

 

int Mul(int a, int b)

{

return a * b;

}

 

int Div(int a, int b)

{

return a / b;

}

 

**************************************

 

// CliDll.h

#pragma once

#include <iostream>

#include "CppDll.h"

 

using namespace System;

using namespace System::Runtime::InteropServices;

using namespace System::Collections::Generic;

using namespace System::Collections;

using namespace std;

 

#pragma comment(lib, "CppDll.lib")

#pragma managed

namespace CliDll {

 

public ref class Arith

{

public:

Arith();

~Arith();

 

int AddCli(int a, int b);

int SubCli(int a, int b);

int MulCli(int a, int b);

int DivCli(int a, int b);

};

}

 

**************************************

 

// CliDll.cpp

#include "stdafx.h"

#include "CliDll.h"

 

using namespace CliDll;

 

CliDll::Arith::Arith(){}

 

CliDll::Arith::~Arith(){}

 

int CliDll::Arith::AddCli(int a, int b)

{

return Add(a, b);

}

 

int CliDll::Arith::SubCli(int a, int b)

{

return Sub(a, b);

}

 

int CliDll::Arith::MulCli(int a, int b)

{

return Mul(a, b);

}

 

int CliDll::Arith::DivCli(int a, int b)

{

return Div(a, b);

}

 

*************************************

 

//Program.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using CliDll;

 

namespace CSProject

{

class Program

{

static void Main(string[] args)

{

Arith arith = new Arith();

int back1 = arith.AddCli(1, 2);

Console.WriteLine(back1.ToString());

int back2 = arith.SubCli(3, 4);

Console.WriteLine(back2.ToString());

int back3 = arith.MulCli(4, 5);

Console.WriteLine(back3.ToString());

int back4 = arith.DivCli(8, 4);

Console.WriteLine(back4.ToString());

Console.ReadLine();

}

}

}

posted @   Avatarx  阅读(2221)  评论(0编辑  收藏  举报
(评论功能已被禁用)
编辑推荐:
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
历史上的今天:
2017-11-12 EL表达式
2017-11-12 WEB服务器、应用程序服务器、HTTP服务器区别
2015-11-12 oracle drop table and purge
2014-11-12 Qt之XML(一) DOM
点击右上角即可分享
微信分享提示