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[] { });
        }
    }
    
}

结果:

 

参考:http://www.noobyard.com/article/p-dkxknoyy-nt.html

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