您的位置:首页 > 编程语言 > C语言/C++

在VS2008中编译纯c/c++程序并由c#调用过程

2008-02-29 23:50 519 查看
这篇文章没有什么新意,在2004年的时候就有人写过那时候VS还只是2003版,而且网络上也有很多转来转去的文章,其实一共那么两三篇。其实我也是从那里学来的,只不过中间经历的一些郁闷,因为那些文章没有提到一些注意的事项确是很烦人。
1. 建立一个C#控制台工程,主要用于调试。
2. 在解决方案中添加一个新的空工程(VC++的)。
3. 添加一个源文件到Source Files文件夹(xxx.c or xxx.cpp)。
4. 加入这行代码

#include <string.h>

extern "C" __declspec(dllexport) int mySum(int a,int b,int *c)
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace TestEmbedCCalling
class Program
[DllImport("CCodeDll.dll", EntryPoint = "mySum", CharSet = CharSet.Ansi,
CallingConvention = CallingConvention.StdCall)]
public static extern int mySum(int a, int b, ref int c);

static void Main(string[] args)
int c = 0;
Console.WriteLine(mySum(2, 3, ref c));
Console.Read();
}
}
}最后你会看到运行结果:5。
请注意绿色字体内容,祝你好运。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: