c#调用c++的dll

系统:64位win10

工具:vs2015

1.非托管

(1)先生成c++的dll:

文件>>新建>>项目>>Visual C++>>Win32项目>>确定;

下一步>>Dll>>空项目>>完成;

添加源文件,添加>>新建项>>mydll.cpp;

#include<stdio.h>
#include<iostream>

using namespace std;

int x = 20;

extern "C" __declspec(dllexport) void testhello()
{
	cout << "hello:" << x << endl;
	x++;
}

生成c++的dll,生成>>生成解决方案;

(2)创建c#程序使用dll:

文件>>新建>>项目>>Visual C#>>控制台应用程序;

添加一个类, Usedll.cs,使用DllImport;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace UseDlllllllllllll
{
    class Usedll
    {
        [DllImport(@"c:\users\administrator\documents\visual studio 2015\Projects\Win32Project2\x64\Release\Win32Project2.dll", EntryPoint = "testhello")]
        public static extern void testhello();
    }
}

在自动生成的Program.cs里Main函数使用;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseDlllllllllllll
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("start test");
            Usedll.testhello();
            Usedll.testhello();
            Usedll.testhello();
        }
    }
    
}

结果:

2.托管

修改设置:项目右键>>属性>>常规>>公共语言运行时支持选“公共语言运行时支持(/clr)”>>应用>>确定;

(1)修改c++代码(使用委托类封装或者直接改成委托类都可)

添加mydll.h头文件

#pragma once
public ref class DllClass
{
private:
	int x = 100;
public:
	DllClass();
	~DllClass();
	void testhello();
};

修改mydll.cpp

#include<stdio.h>
#include<iostream>

#include"mydll.h"

using namespace std;

DllClass::DllClass() {}
DllClass::~DllClass(){}
void DllClass::testhello()
{
	cout << "hello:" << x << endl;
	x++;
}

生成解决方案;

(2)c#修改

添加dll的引用,右键引用>>添加引用>>浏览>>勾选刚刚的dll>>确定(代替Usedll.cs的DllImport)

这样就添加了刚刚的dll了

修改Program.cs,Main函数可以直接使用dll的类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UseDlllllllllllll
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("start test");
            //Usedll.testhello();
            //Usedll.testhello();
            //Usedll.testhello();
            DllClass dc = new DllClass();
            dc.testhello();
            dc.testhello();
            dc.testhello();
        }
    }
    
}

结果:

3.动态加载

不使用DllImport也不用引入dll,动态加载反射调用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace UseDlllllllllllll
{
    class Program
    {
        static void Main(string[] args)
        {
            //Console.WriteLine("start test");
            //Usedll.testhello();
            //Usedll.testhello();
            //Usedll.testhello();

            // 按照路径获取dll
            Assembly assembly = Assembly.LoadFrom(@"c:\users\administrator\documents\visual studio 2015\Projects\Win32Project2\x64\Release\Win32Project2.dll");
            // 获取dll中的DllClass类
            Type dc = assembly.GetType("DllClass");
            // 实例化该类
            object obj = Activator.CreateInstance(dc);
            // 获取该类中的testhello
            MethodInfo testhello = dc.GetMethod("testhello");
            // 反射调用testhello
            testhello.Invoke(obj, new object[] { });
            testhello.Invoke(obj, new object[] { });
            testhello.Invoke(obj, new object[] { });
        }
    }
    
}

结果:

 

参考:https://blog.csdn.net/liyuqian199695/article/details/53525178

https://blog.csdn.net/zhengjiafa/article/details/53726355?utm_source=blogxgwz2

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值