在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。
请注意绿色字体内容,祝你好运。
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。
请注意绿色字体内容,祝你好运。
相关文章推荐
- 在VS2008中编译纯c/c++程序并由c#调用过程
- 在VS2008中编译纯c/c++程序并由c#调用过程
- C++程序从编译到链接然后再到调用的整个过程
- (二)一个工作任务引起的乱战——C++程序编译为dll,让C#调用
- C#编译DLL文件供VScript或QTP调用过程
- Windows系统下用命令行编译C/C++程序过程总结
- 编译C++ 程序的过程
- C++控制台程序调用C#的.dll文件
- c++基础11:变量的命名约定 头文件的问题 程序的预处理编译链接过程
- C++程序编译过程
- C/C++程序编译的过程
- C/C++程序编译运行过程分析(转载)
- Windows系统下用命令行编译C/C++程序过程总结
- C/C++程序编译运行生成过程分析
- VS2008下编译C++程序,找不到 stdint.h,原因及解决方案
- linux下如何将c++程序编译成so,如何调用该so文件
- Windows系统下用命令行编译C/C++程序过程总结
- C++封装成托管模式供C#调用过程
- C++ 调用 C#写的COM (基于VS2008)
- c#程序调用c++编写dll需要注意问题