C#调用C++ 动态链接库dll


本文摘自PHP中文网,作者黄舟,侵删。

在过程中发现两种方法解决问题:一种是非托管C++创建的dll库,需要用静态方法调用。这种方法无法在C#的reference中直接引用,而是要用静态调用的方法,其他博客已经介绍的很详尽,唯一需要补充的是,C#文件需要先:

1

using System.Runtime.InteropServices;

之后才可以调用[DllImport]方法。

另一种方法是直接使用CLR,生成托管C++dll库。

创建流程

例程如下
C++ dll:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// CPPlibdemo.h

#pragma once

 

using namespace System;

 

namespace CPPlibdemo {

 

    public ref class Class1

    {

        // TODO: Add your methods for this class here.

    public:

            String ^getgreating(){

 

            return "hello world";

        }

    };

}

C#语言:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using CPPlibdemo;

 

namespace ConsoleApplication5

{

    class Program

    {

        static void Main(string[] args)

        {

            Class1 clrdemo = new Class1();

 

            Console.Write(clrdemo.getgreating());

            Console.ReadLine();

        }

    }

}

以上就是C#调用C++ 动态链接库dll 的内容!

相关阅读 >>

如何用c++读取ini文件中的section节名

c++类型转换

c++中new的用法详解

c++中头文件和源文件的区别是什么

c++清屏函数是什么

如何安装visual c++ 6.0

c++怎么运行

c++中的四种强制类型转换_基本用法及使用场景

c++标识符命名规则

c++如何简单输出当前日期时间

更多相关阅读请进入《C#》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...