How to call C/C++ sytle function from C# solution?

1. Write native(unmanaged) code with C/C++, and make sure compile it as a DLL, the sample is as below

#include <iostream>
using namespace std;

extern "C"
{
  _declspec(dllexport) int AddTwoNumber(int x, int y);
}
int AddTwoNumber(int x, int y)
{
  return x+y;
}

2. Write managed code with C#, we can put it in a console application, like below:

static void Main(string[] args)
{
  int result = AddTwoNumber(2, 77);
  Console.WriteLine(result);
  Console.Read();
}

[DllImport("E:\\TestTools\\UnManagedSolution\\Debug\\UnManagedSolution.dll", CallingConvention = CallingConvention.Cdecl)] // full path to reference the dll file
//[DllImport("E:\\TestTools\\UnManagedSolution\\Debug\\UnManagedSolution.dll", CallingConvention=CallingConvention.StdCall)] // just different CallingConvention
//[DllImport("UnManagedSolution.dll"), CallingConvention] // make sure copy the dll to the same folder with exe file
public extern static int AddTwoNumber(int x, int y);

Now it just works, try it! ^_^

时间: 2024-10-14 09:52:46

How to call C/C++ sytle function from C# solution?的相关文章

mysql create function error and solution way.

Error Code: 1418. This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable reason : mysql的设置默认是不允许创建函数 解决办法1: 执

Python:staticmethod vs classmethod

Being educated under Java background, static method and class method are the same thing. But not so in Python, there is subtle difference: Say function a() is defined in Parent Class, while Sub Class extends Parent Class If function a() has @staticme

GenomicRangeQuery /codility/ preFix sums

首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which correspond to the types of successive nucleotides in the sequence. Each nucleotide has an impact factor, which is an integer. Nucleotides of types A,

Colidity--GenomicRangeQuery

思路:统计每一个字符前面的四个字符的个数(利用前缀和数组),这样就能在O(1)时间得到某个区间某个字符的个数 开始想到的是O(n^2)的空间,这个思路比较好 1 // you can use includes, for example: 2 // #include <algorithm> 3 4 // you can write to stdout for debugging purposes, e.g. 5 // cout << "this is a debug mes

《Walking the callstack(转载)》

本文转载自:https://www.codeproject.com/articles/11132/walking-the-callstack Download demo project with source - 64.2 Kb Introduction In some cases you need to display the callstack of the current thread or your are just interested in the callstack of othe

通过百度echarts实现数据图表展示功能

现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解echarts是个怎样技术的开发者来说,可以到echarts官网进行学习了解,官网有详细的API文档和实例供大家参考学习. 2.以下是我在工作中实现整理出来的实例源码: 公用的支持js文件 echarts.js.echarts.min.js,还有其他的图表需要支持的js文件也可以到官网下载 echa

Python中如何使用最小二乘法

之所以说"使用"而不是"实现",是因为python的相关类库已经帮我们实现了具体算法,而我们只要学会使用就可以了.随着对技术的逐渐掌握及积累,当类库中的算法已经无法满足自身需求的时候,我们也可以尝试通过自己的方式实现各种算法. 言归正传,什么是"最小二乘法"呢? 定义:最小二乘法(又称最小平方法)是一种数学优化技术,它通过最小化误差的平方和寻找数据的最佳函数匹配. 作用:利用最小二乘法可以简便地求得未知的数据,并使得这些求得的数据与实际数据之间误

lua解上阶梯问题(按指定格式输出具体走法、迭代、递归)

问题描述 楼梯有n阶台阶,上楼可以一步上1阶,也可以一步上2阶,编一程序列出每一种走法. 思路 注释均在代码中 其它 第一次接触lua,很好的脚本语言 源代码 --[[-------------------------------------------------------------------------- 表复制:深复制 --]]-------------------------------------------------------------------------- func

从稀疏表示到低秩表示(二)

从稀疏表示到低秩表示(二) 确定研究方向后一直在狂补理论,最近看了一些文章,有了些想法,顺便也总结了representation系列的文章,由于我刚接触,可能会有些不足,愿大家共同指正. 从稀疏表示到低秩表示系列文章包括如下内容: 一. sparse representation 二.NCSR(NonlocallyCentralized Sparse Representation) 三.GHP(GradientHistogram Preservation) 四.Group sparsity 五.