在C++ 中 如何调用 C# 写的DLL
2015-07-02 11:05
736 查看
对于很多和我一样的老C++程序员来讲,如果不能与时俱进,不但会倍感失落,还会真的自绝于天下。
比如C#有很多好的组件, 如NPOI ,如。。。 ,怎么在C++中也自如调用呢,这样不用去学习C#,不用换刀,也可以达到同样的目的。
网上看了半天,找到的资料要么太老,要么不对,干脆自己调试一遍。下面把体会说一说。
调试环境:visual studio 2010 .
时间:2012.12.20
一、C#写的类库:
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
public String Name
{
get;
set;
}
public void helloWorld()
{
Console.WriteLine("hello world!");
}
}
}
// C#程序配置,一定是类库
![](http://s14.sinaimg.cn/mw690/5920510atd144f734eb6d&690)
二、C++ 程序
共有三个程序文件
(1) 主程序
// test1.cpp : 定义控制台应用程序的入口点。
//
///
//
// 在C++ 项目属性 [配置]-[常规] 中,公共语言运行时支持,一定要选择“公共语言运行时支持”
//
#include "stdafx.h"
#include "yotopcompany.h"
#using "..\ClassLibrary1\bin\Debug\ClassLibrary1.dll" //引用C#类库
using namespace ClassLibrary1; // 声明命名空间,非必须
int _tmain(int argc, _TCHAR* argv[])
{
printf("hello world");
ClassLibrary1::Class1 ^c = gcnew ClassLibrary1::Class1(); //注意一定要用 ^ , 一定要用gcnew
c->Name = "\nxignxianghong";
printf("%s\n", c->Name);
c->helloWorld();
YotopCompany ^a = gcnew YotopCompany("a","b","c");
printf("%s,%s,%s",a->name,a->address,a->phoneNumber);
getchar();
return 0;
}
(2) c++ 中自己编写的一个类
// yotopCompany.h
#pragma once
ref class YotopCompany
{
public:
YotopCompany(void);
YotopCompany(char* name,char* address,char* phoneNumber);
char* name ;
char* address;
char* phoneNumber;
};
(3) c++编写的类的CPP文件
//yotopcompany.cpp
#include "StdAfx.h"
#include "YotopCompany.h"
YotopCompany::YotopCompany(void)
{
name = "yotop";
phoneNumber = "12345678" ;
address = "北京";
}
YotopCompany::YotopCompany(char*_name ,char* _address,char* _phoneNumber)
{
name = _name;
address = _address;
phoneNumber = _phoneNumber ;
}
(4) c++ 程序配置
![](http://s7.sinaimg.cn/mw690/5920510atd144f7a41076&690)
三、如果还有疑问,请参考 MSDN 文章:
// How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005
// 如何在 Visual Studio.NET 或 Visual Studio 2005 中的本机 Visual C++ 代码中调用托管的 DLL
// http://support.microsoft.com/kb/828736
四、总结:
1)用C#写任何的类库
2)C++ 中要引用此类库
3)创建C#对象时要用gcnew ;
4) C++ 编译设置一定设置为:支持公共语言运行时支持(/clr)
4) 自身的C++类要用 ref class 定义。
希望关心上述主题的朋友能够如愿以偿,GOOD LUCK!
比如C#有很多好的组件, 如NPOI ,如。。。 ,怎么在C++中也自如调用呢,这样不用去学习C#,不用换刀,也可以达到同样的目的。
网上看了半天,找到的资料要么太老,要么不对,干脆自己调试一遍。下面把体会说一说。
调试环境:visual studio 2010 .
时间:2012.12.20
一、C#写的类库:
using System;
using System.Collections.Generic;
using System.Text;
namespace ClassLibrary1
{
public class Class1
{
public String Name
{
get;
set;
}
public void helloWorld()
{
Console.WriteLine("hello world!");
}
}
}
// C#程序配置,一定是类库
二、C++ 程序
共有三个程序文件
(1) 主程序
// test1.cpp : 定义控制台应用程序的入口点。
//
///
//
// 在C++ 项目属性 [配置]-[常规] 中,公共语言运行时支持,一定要选择“公共语言运行时支持”
//
#include "stdafx.h"
#include "yotopcompany.h"
#using "..\ClassLibrary1\bin\Debug\ClassLibrary1.dll" //引用C#类库
using namespace ClassLibrary1; // 声明命名空间,非必须
int _tmain(int argc, _TCHAR* argv[])
{
printf("hello world");
ClassLibrary1::Class1 ^c = gcnew ClassLibrary1::Class1(); //注意一定要用 ^ , 一定要用gcnew
c->Name = "\nxignxianghong";
printf("%s\n", c->Name);
c->helloWorld();
YotopCompany ^a = gcnew YotopCompany("a","b","c");
printf("%s,%s,%s",a->name,a->address,a->phoneNumber);
getchar();
return 0;
}
(2) c++ 中自己编写的一个类
// yotopCompany.h
#pragma once
ref class YotopCompany
{
public:
YotopCompany(void);
YotopCompany(char* name,char* address,char* phoneNumber);
char* name ;
char* address;
char* phoneNumber;
};
(3) c++编写的类的CPP文件
//yotopcompany.cpp
#include "StdAfx.h"
#include "YotopCompany.h"
YotopCompany::YotopCompany(void)
{
name = "yotop";
phoneNumber = "12345678" ;
address = "北京";
}
YotopCompany::YotopCompany(char*_name ,char* _address,char* _phoneNumber)
{
name = _name;
address = _address;
phoneNumber = _phoneNumber ;
}
(4) c++ 程序配置
三、如果还有疑问,请参考 MSDN 文章:
// How to call a managed DLL from native Visual C++ code in Visual Studio.NET or in Visual Studio 2005
// 如何在 Visual Studio.NET 或 Visual Studio 2005 中的本机 Visual C++ 代码中调用托管的 DLL
// http://support.microsoft.com/kb/828736
四、总结:
1)用C#写任何的类库
2)C++ 中要引用此类库
3)创建C#对象时要用gcnew ;
4) C++ 编译设置一定设置为:支持公共语言运行时支持(/clr)
4) 自身的C++类要用 ref class 定义。
希望关心上述主题的朋友能够如愿以偿,GOOD LUCK!
相关文章推荐
- c语言结构体中保存函数指针的用法
- C++ STL中常见容器的时间复杂度
- 黑马程序员---C语言基础---语句概述
- C/C++中善用大括号
- C语言关键字auto与register的深入理解
- C++中的四种强制类型转换的区别
- 【c语言】模拟实现strchr函数,功能:在一个字符串中查找一个字符第一次出现的位置,如果没有出现返回NULL
- C++ 类继承与对象赋值 情况下 成员变量的覆盖 浅析
- 【c语言】编写程序,使字符串逆序,空间复杂度O(1)
- 【c语言】 模拟实现库函数strcat函数
- C语言中关于错误输出的函数
- 【c语言】模拟实现库函数strcpy函数
- 【c语言】实现一个函数,求字符串的长度,不允许创建第三方变量
- 【c语言】实现一个函数,求字符串的长度
- c++ explicit
- 重温《C++ Primer》笔记八 数组与指针的几个问题
- VisualStudio 使用Visual Leak Detector检查内存泄漏
- C++运行时错误集
- Kinect SDK C++ - 2. Kinect Depth Data
- 【C语言】模拟实现strchr函数.即在一个字符串中查找一个字符第一次出现的位置并返回