C # to call C ++ dynamic link library dll

We found that in the process are two ways to solve the problem: dll unmanaged C ++ library one kind created, you need to call a static method. This method can not be directly referenced in the C # reference, but rather to use a static method call, the other blog has been described in great detail, the only need to add that, C # file you need to:

using System.Runtime.InteropServices;

Before you can call [DllImport] method.

Another method is to directly using CLR, managed to generate C ++ dll library.

Create a process

routine as
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 # Language:

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();

        }

    }

}

These are the contents of C # to call C ++ dynamic link library dll's, please pay attention to more details PHP Chinese network (www.php.cn)!

 

Guess you like

Origin www.cnblogs.com/ximi07/p/12371260.html