C++调用C#的DLL详解
来源:互联网 发布:动感相册制作软件 编辑:程序博客网 时间:2024/06/03 19:58
C++调用C#.Net的类库DLL步骤如下所示:
一、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++ 程序配置
三、总结:
1)用C#写任何的类库
2)C++中要引用此类库
3)创建C#对象时要用gcnew ;
4)C++ 编译设置一定设置为:支持公共语言运行时支持(/clr)
5)自身的C++类要用 ref class定义。
可参考微软官网:
https://support.microsoft.com/zh-cn/kb/828736
- C#调用C的DLL
- c#调用dll详解
- C#调用C++DLL
- C#调用C++dll
- C#调用C++dll
- C#调用C++DLL
- c#调用c++DLL
- C# 调用C++dll
- C#调用C++DLL
- C#调用C++DLL
- C#调用C++DLL
- C#调用C++Dll
- C#调用C++DLL
- C#调用Delphi的dll之详解
- C++调用C#的DLL详解
- C#调用C++DLL的方法
- C#动态调用c++DLL的方法
- 编写C#调用的C++DLL
- 【Scala】使用Option、Some、None,避免使用null
- ElasticSearch安装部署,基本配置(Ubuntu14.04)
- ubuntu中遇到在执行可执行文件时报错不存在该文件或目录(No such file)
- 隧道协议
- js获取url传递参数,js获取url?号后面的参数window.location
- C++调用C#的DLL详解
- Linux 基础命令 文件查找
- UVA 750 八皇后·(2)+ 预处理+递归+棋盘问题
- 移动端直播应用的开发流程
- Protocol Buffer技术详解(3)-数据编码
- CentOS 6.4下编译安装MySQL 5.6.14
- PPTP
- osgPhysX编译错误:Debug Assertion Failed _pFirstBlock == pHead
- flume通过tcp/udp采集数据并存到kafka配置及操作方式