zoukankan      html  css  js  c++  java
  • 简单了解 DLL中, .def 文件及C#调用C++方法

    DLL中导出函数的声明有两种方式:

    1、在函数声明中加上__declspec(dllexport)

     1 //以下内容为 .h  文件中的内容
     2 //向外界提供的端口
     3 extern"C" _declspec(dllexport) int _stdcall Test1(int M, int N);
     4 extern"C" _declspec(dllexport) int _stdcall Test2(int M, int N);
     5 
     6 //端口函数的声明
     7 int _stdcall Test1(int M, int N);
     8 int _stdcall Test2(int M, int N);
     9 
    10 class MyClass
    11 {
    12 public:
    13     MyClass();
    14     ~MyClass();
    15 public:
    16     int TestFunction1(int M, int N) const;
    17 
    18 private:
    19     double tmp = 3.1415926;
    20 };
    21 
    22 MyClass::MyClass()
    23 {
    24 }
    25 
    26 MyClass::~MyClass()
    27 {
    28 }
    29 
    30 int MyClass::TestFunction1(int M, int N) const
    31 {
    32     return M + N;
    33 }

    2、采用模块定义(.def) 文件声明,.def文件为链接器提供了有关被链接程序的导出、属性及其他方面的信息。

     1 ;以下内容为 .def 文件中的内容
     2 ;nativeCPP : 导出 DLL 函数   这句是注释,分号为标志
     3 ;LIBRARY语句说明.def文件相应的DLL;
     4 ;EXPORTS语句后列出要导出函数的名称。可以在.def文件中的导出函数名后加@n,表示要导出函数的序号为n(在进行函数调用时,这个序号将发挥其作用)
     5 
     6 LIBRARY "nativeCPP"  
     7 
     8 EXPORT
     9     Test1 @ 1
    10     Test2 @ 2

    使用P/Invoke直接调用native C++ Dll里面的函数。(注:此方法只能调用函数,不能调用class)。

    C#调用C++导出的方法:

     1 using System;
     2 using System.Runtime.InteropServices;
     3 
     4 namespace ConsoleApplication_test
     5 {
     6     class Program
     7     {
     8         [DllImport(@"E:LgsTestC#_C++Test_CSharpInvokeCPP_nativeDebug
    ativeCpp.dll", EntryPoint = "Test1",
     9             SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
    10         static extern int Test1(int M, int N);
    11 
    12         [DllImport(@"E:LgsTestC#_C++Test_CSharpInvokeCPP_nativeDebug
    ativeCpp.dll", EntryPoint = "Test2",
    13           SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = false, CallingConvention = CallingConvention.StdCall)]
    14         static extern int Test2(int M, int N);
    15 
    16         static void Main(string[] args)
    17         {
    18             int start = Environment.TickCount;    //计时开始
    19             int m = 50000;
    20             int n = 50000;
    21             int flag = Test1(m, n);
    22             Console.WriteLine("END flag = {0}", flag);
    23             Console.WriteLine(" m = n = {0}", m);
    24             int stop = Environment.TickCount;    //计时结束
    25             Console.WriteLine("Seconds1 = {0,10}
    ", (double)(stop - start) / 1000);
    26 
    27 
    28             int start_ = Environment.TickCount;    //计时开始
    29             int m_ = 50000;
    30             int n_ = 50000;
    31             int flag_ = Test2(m_, n_);
    32             Console.WriteLine("END flag = {0}", flag_);
    33             Console.WriteLine(" m = n = {0}", m_);
    34             int stop_ = Environment.TickCount;    //计时结束
    35             Console.WriteLine("Seconds2 = {0,10}", (double)(stop_ - start_) / 1000);
    36 
    37             Console.ReadKey();
    38         }
    39     }
    40 }
  • 相关阅读:
    大话串口:我和它的恩恩怨怨
    分布式网游server的一些想法(一) 语言和平台的选择
    C++: C没有闭包真的很痛苦……
    C++不是C/C++
    最美树算法
    类魔兽世界 技能 天赋 成就 log 系统设计
    C++网游服务端开发(一):又无奈的重复造了个轮子,一个底层网络库
    C++ protobuf 不仅仅是序列化……
    深入WPFStyle
    Illusion = Caliburn.Micro + MEF
  • 原文地址:https://www.cnblogs.com/luguoshuai/p/10762539.html
Copyright © 2011-2022 走看看