当前位置: 首页 > news >正文

C# invoke C++ method

1. Platform Invocation (P/Invoke)

Set C++ project as dll, compile, copy compiled c++ dll to C# bin/debug matched location

image

 

 

 

//C++
// ConsoleApplication1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#include <iostream>extern "C"
{__declspec(dllexport) int AddNumbers(int a, int b){return a + b;}__declspec(dllexport) double CalculateAverage(double* numbers, int count){double sum = 0.0;for (int i = 0; i < count; i++){sum += numbers[i];}return sum / count;}
}
// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

 

 

using System;
using System.Runtime.InteropServices;namespace ConsoleApp3
{internal class Program{[DllImport("ConsoleApplication1.dll")]public static extern int AddNumbers(int a, int b);[DllImport("ConsoleApplication1.dll")]public static extern double CalculateAverage(double[] numbers, int count);static void Main(string[] args){int result = AddNumbers(5, 100);Console.WriteLine($"Result:{result}");double[] numbers = { 4.5, 5346.5, 2453465.4, 675467.7 };double average=CalculateAverage(numbers,numbers.Length);Console.WriteLine($"Average:{average}");}}
}

 

image

 

 

 

 

image

 

 

2.

 

http://www.jsqmd.com/news/6310/

相关文章:

  • 本土化优势凸显:Gitee如何成为中国开发团队的效率引擎
  • Linux系统OOM终止Oracle进程
  • 实用指南:《C++进阶之C++11》【可变参数模板 + emplace接口 + 新的类功能】
  • Filebeat写ElasticSearch故障排查思路(上) - 教程
  • 告别 “一刀切” 管理!MyEMS 为不同行业定制专属能源优化方案
  • 「突发奇想,灵光乍现」 - hello
  • BST(self saved)
  • jenkins 用户权限 管理配置
  • DirectX- DLL修复工具 免费下载!绿色单文件版!安装使用教程
  • 测试集成CI/CD的五大实践:构建高效质量保障体系
  • Windows系统Web UI自动化测试学习系列4--开源体系平台测试项目环境部署搭建
  • DirectX修复工具官方中文增强版下载!下载安装教程(附安装包),0xc000007b错误解决办法
  • kuboard使用的etcd空间清理(3个etcd)
  • Node生态中最优雅的数据库事务处理机制
  • 详细介绍:扒透 STL 底层!map/set 如何封装红黑树?迭代器逻辑 + 键值限制全手撕----《Hello C++ Wrold!》(23)--(C/C++)
  • 死锁的处理策略-预防死锁
  • 跨网文件安全交换系统:提升数据传输安全性和合规性
  • ArcGIS 公众号推荐
  • 跨网文件交换系统:数字化时代企业与机构的数据安全传输利器
  • 缩放 div
  • Redis从零讲解 - 详解
  • 【2025-09-29】团队合作
  • 杂凑算法学习笔记
  • pg库支持扩展postgis
  • kuboard部署启用3个etcd(k8s单个master)
  • odoo18应用、队列服务器分离(SSHFS) - 详解
  • 数据库服务分布架构(MyCAT)
  • 题解:P14038 [PAIO 2025] Adventure Plan
  • 20231414_王仕琪_密码技术密码杂凑算法学习笔记
  • 调度算法易错概念总结