zoukankan      html  css  js  c++  java
  • 【转】.def

               DLL中导出函数的声明有两种方式:一种为在函数声明中加上__declspec(dllexport),这里不再举例说明;另外一种方式是采用模块定义(.def) 文件声明,.def文件为链接器提供了有关被链接程序的导出、属性及其他方面的信息。


                首先创建 一个DLL程序,.cpp中
    int __stdcall Add(int numa, int numb)
    {
           return (numa + numb);
    }

    int __stdcall Sub(int numa, int numb)
    {
            return (numa - numb);
    }

                 然后创建一个.def的文件,在里面加上

    ;DllTestDef.lib : 导出DLL函数
    ;作者:----
    LIBRARY DllTestDef
    EXPORTS 
    Add @ 1
    Sub @ 2

               最后创建一个测试程序:.cpp文件如下:
    #include <iostream>
    #include <windows.h>

    using namespace std;

    typedef int (__stdcall *FUN)(int, int);
    HINSTANCE hInstance;
    FUN   fun;

    int main()
    {
           hInstance = LoadLibrary("DLLTestDef.dll");
           if(!hInstance)
               cout << "Not Find this Dll" << endl;
           fun = (FUN)GetProcAddress(hInstance, MAKEINTRESOURCE(1));
           if (!fun)
           {
                  cout << "not find this fun" << endl;
           }
           cout << fun(1, 2) << endl;
           FreeLibrary(hInstance);
           return 0;
    }



    说明:
    .def文件的规则为:

      (1)LIBRARY语句说明.def文件相应的DLL;

      (2)EXPORTS语句后列出要导出函数的名称。可以在.def文件中的导出函数名后加@n,表示要导出函数的序号为n(在进行函数调用时,这个序号将发挥其作用);

      (3).def 文件中的注释由每个注释行开始处的分号 (;) 指定,且注释不能与语句共享一行。

  • 相关阅读:
    MySQL之汇总数据(AVG,COUNT,MAX,MIN,SUM)
    查询两门及两门以上不及格同学的平均分
    百度乐播音乐真实地址查找api接口
    腾讯云服务器无法ssh登陆问题
    ubuntu搭建php运行环境
    阿里云腾讯云服务器ubuntu多域名配置
    PHP 快速排序算法详解
    PHP7.x新特性
    PHP的钩子实现解析
    PHP 二维数组根据某个字段排序 复制代码 array_multisort
  • 原文地址:https://www.cnblogs.com/beipiaoboy/p/3261775.html
Copyright © 2011-2022 走看看