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. 【Python3之多线程】

    一.threading模块 multiprocess模块的完全模仿了threading模块的接口,二者在使用层面,有很大的相似性. 1.开启线程的两种方式(同Process) 方法一 from thr ...

  2. 设计模式 - 代理模式(jdk)

    定义:为另一个对象提供一个替身或占位符以控制对这个对象的访问. 一.静态代理 静态代理说白了就是把原先直接调用被代理类的方法放到代理类来调用,同时 我们可以在代理类额外的添加一些操作. 接口: pac ...

  3. Python-Flask:利用flask_sqlalchemy实现分页效果

    Flask-sqlalchemy是关于flask一个针对数据库管理的.文中我们采用一个关于员工显示例子. 首先,我们创建SQLALCHEMY对像db. from flask import Flask, ...

  4. jsoncpp动态解析节点类型

    在互联网无处不在的今天,JSON作为轻量级数据存储格式,被广泛应用到互联网数据传输中.众所周知,JSON由键/值对.对象.数组组成,其中键/值对的值包括以下几种类型: enum ValueType { ...

  5. PCI_Making Recommendations

    协作性过滤 简单理解从众多用户中先搜索出与目标用户'品味'相似的部分人,然后考察这部分人的偏爱,根据偏爱结果为用户做推荐.这个过程也成为基于用户的协作性过滤(user_based collaborat ...

  6. async 函数

    同步 console.log(1); console.log(2); console.log(3); console.log(4); //异步 ajax 文件读取io操作 console.log(1) ...

  7. 计算BMI

    用一个小程序计算BMI 代码如下: package Day06; public class BMI { private String name; private int age; private do ...

  8. Miller-Rabin 素性测试

    根据费马小定理,若p为素数,则必有a^(p-1) mod p=1 对和p互质的a成立. 根据二次探测定理:如果p是素数,且0<x<p,则方程x^2 mod p=1的解为1或p-1. 所以若 ...

  9. Struts2简诉

    Struts2框架是基于MVC模式的开源,MVC模式是一种开发方式,主要作用是对组件之间进行隔离,M代表业务逻辑层,V代表视图层,C代表控制层.有利于代码的后期维:Struts2框架的源码主要来于We ...

  10. C#中的Infinity和NaN

    C#中double和float类型有两个特殊值: Infinity(无穷大):5.0 / 0.0 = Infinity NaN(not a number):0.0 / 0.0 = NaN 计算表达式 ...