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! ^_^

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

  1. 《Walking the callstack(转载)》

    本文转载自:https://www.codeproject.com/articles/11132/walking-the-callstack Download demo project with so ...

  2. GenomicRangeQuery /codility/ preFix sums

    首先上题目: A DNA sequence can be represented as a string consisting of the letters A, C, G and T, which ...

  3. Private-code MaxCounter

    No need for a double cycle: : You are given N counters, initially set to 0, and you have two possibl ...

  4. js常用功能汇总

    var Utils = function() { this.Tools; this.ui; }; Utils = new Utils(); Utils.prototype.Tools = { year ...

  5. On Caching and Evangelizing SQL

    http://www.oracle.com/technetwork/issue-archive/2011/11-sep/o51asktom-453438.html   Our technologist ...

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

    现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...

  7. Python:staticmethod vs classmethod

    Being educated under Java background, static method and class method are the same thing. But not so ...

  8. SharePoint Solutions Deployment-PowerShell

    之前群里有人问到了这个,项目一期开发结束后正好整理了一下,发上来分享一下,也是从谷歌搜索里面学来的,大家要用好搜索哈 $ver = $host | select version if ($ver.Ve ...

  9. 机器学习:形如抛物线的散点图在python和R中的非线性回归拟合方法

    对于样本数据的散点图形如函数y=ax2+bx+c的图像的数据, 在python中的拟合过程为: ##最小二乘法 import numpy as np import scipy as sp import ...

随机推荐

  1. 面向对象设计模式——观察者(OBSERVER)模式

    定义 定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新.  Observer模式描述了如何建立这种关系.这一模式中的关键对象是目标(subject ...

  2. (转)Sublime Text中文乱码问题

    Sublime Text 2是一个非常不错的源代码及文本编辑器,但是不支持GB2312和GBK编码在很多情况下会非常麻烦.不过Sublime Package Control所以供的插件可以让Subli ...

  3. HDU1124 Factorial

    Problem Description The most important part of a GSM network is so called Base Transceiver Station ( ...

  4. PHP环境搭建——Apache

    1.PHP环境安装前我们需要知道PHP给我们带来的功能是什么? Php主要用在三个领域 (1)       网站和web应用程序 (2)       命令行脚本 (3)       桌面(GUI)应用 ...

  5. Android Handler 机制 - Looper,Message,MessageQueue

    Android Studio 2.3 API 25 从源码角度分析Handler机制.有利于使用Handler和分析Handler的相关问题. Handler 简介 一个Handler允许发送和处理M ...

  6. 一种轻便且灵活的js模板的思路

    一种轻便且灵活的js模板的思路 项目地址:https://github.com/j20041426/template 思路背景 在Vue.React.Angular等大前端框架异军突起的今天,写前端时 ...

  7. Java 字符排序问题

    Java 字符排序问题 未专注于排序算法,而是写了一个MyString类,实现了comparable的接口,然后用Arrays的sort方法来实现排序.我觉得这道题的难度在于如果比较两个.因为大小写的 ...

  8. Linux 下 安装jdk 1.7

    Linux 下 安装jdk 1.7 参考百度经验 http://jingyan.baidu.com/album/ce09321b7c111f2bff858fea.html?picindex=6 第一步 ...

  9. apt-get命令失效

    1.linux体系区分 a.ded体系,例如debian系统.ubuntu系统.使用apt-get命令: 例如:sudo apt-get install apache2 b.rpm体系,例如redha ...

  10. factorOne cannot be&nb…

    factorOne cannot be resolved or is not a field 现象描述: Eclipse的使用时会在代码处出现警告factorOne cannot be resolve ...