c++调用dll,怎么换成c#的

 我来答
折柳成萌
高粉答主

2017-09-30 · 繁杂信息太多,你要学会辨别
知道顶级答主
回答量:4.4万
采纳率:96%
帮助的人:5851万
展开全部

一、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 onceref 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++ 程序配置

三、 如何在 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 定义。

bernard
2017-09-30 · 超过12用户采纳过TA的回答
知道答主
回答量:121
采纳率:0%
帮助的人:22.4万
展开全部
c++可以调用的dll,c#也可以直接调用
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式