zoukankan      html  css  js  c++  java
  • 使用 __declspec(dllimport) 能够优化对DLL导出函数的调用.

      使用 __declspec(dllimport) 能够优化对DLL导出函数的调用。

    不使用时:

          [DLL]
            #ifdef THEDLL_EXPORTS
            #define THEDLL_API __declspec(dllexport)
            #else
            #define THEDLL_API __declspec(dllimport)
            #endif
            
            // THEDLL_API 
            int fntheDll(void);
            
            [EXE]
            #include "..\theDll\theDll.h"
            #pragma  comment(lib, "..\Release\theDll.lib")
    
            #include <Windows.h>
            #include <iostream>
            using namespace std;
    
            int _tmain(int argc, _TCHAR* argv[])
            {
                cout << fntheDll() << endl;
                return 0;
            }
            
            问题是: 链接器生成不必要的 thunk:
            
            cout << fntheDll() << endl;
            00BC1000 A1 3C 20 BC 00   mov         eax,dword ptr [__imp_std::endl (0BC203Ch)]
            00BC1005 50               push        eax  
            00BC1006 E8 13 08 00 00   call        fntheDll (0BC181Eh) 
                                                                ^
            // 链接器生成的 thunk                                 |
            fntheDll:                                           |
                 |----------------------------------------------/
                 v
            00BC181E FF 25 B8 20 BC 00 jmp         dword ptr [__imp_fntheDll (0BC20B8h)]
                                                                         |
                                                                         |
                                                                         --------> EXE IAT(导入表)中的地址, 即, 函数 fntheDll() 在EXE地址空间的位置.

    使用时:

         [DLL]
            #ifdef THEDLL_EXPORTS
            #define THEDLL_API __declspec(dllexport)
            #else
            #define THEDLL_API __declspec(dllimport)
            #endif
    
            THEDLL_API int fntheDll(void);
    
            [EXE]
            #include "..\theDll\theDll.h"
            #pragma  comment(lib, "..\Release\theDll.lib")
    
            #include <Windows.h>
            #include <iostream>
            using namespace std;
    
            int _tmain(int argc, _TCHAR* argv[])
            {
                cout << fntheDll() << endl;
                return 0;
            }
            
            链接器没有生成 thunk:
    
            cout << fntheDll() << endl;
            00E11000 A1 3C 20 E1 00   mov         eax,dword ptr [__imp_std::endl (0E1203Ch)]
            00E11005 50               push        eax  
            00E11006 FF 15 B8 20 E1 00 call       dword ptr [__imp_fntheDll (0E120B8h)]
                                                                       |
                                                                       |
                                                                       --------> EXE IAT(导入表)中的地址, 即, 函数 fntheDll() 在EXE地址空间的位置.

      结论: 所以通过 __declspec(dllimport) 指示导入函数, 链接器不会生成不必要的中间thunk, 而该thunk使生成的EXE更大, 执行时多了一次跳转。

  • 相关阅读:
    HTTP/HLS/RTMP超级负载测试工具(转)
    Jmeter监控Linux服务器性能
    装饰器做缓存
    内置装饰器
    Python装饰器 计时器记录方法执行性能
    【Python】装饰器实现日志记录
    Java对关于两个地点的根据经纬度算出后排序
    JS获得当前位置信息
    百度地图插件开发使用三 及jquery function(a.b)排序等
    css用clearfix清除浮动
  • 原文地址:https://www.cnblogs.com/happylong/p/4501538.html
Copyright © 2011-2022 走看看