VC中无LIB时的DLL隐式链接,制作与VC++相符合的LIB函数符号输入库

类别:VC语言 点击:0 评论:0 推荐:

请大家注意!这种方法只能应用于输出为C格式的__stdcall调用方式!

1. 使用VC++的工具DUMPBIN将DLL中的导出函数表导出到一定义(.DEF)文件
 EXAMPLE:
  DUMPBIN VideoDeCoder.dll /EXPROTS /OUT:VideoDeCoder.def
 
2. 将导出的.DEF文件整理为一符合.DEF个数的函数导出文件
 EXAMPLE:
  VideoDeCoder.DEF 文件内容如下
                                                                
        Dump of file VideoDeCoder.dll                                 
                                                                      
        File Type: DLL                                                
                                                                      
          Section contains the following exports for VideoDeCoder.dll 
                                                                      
                   0 characteristics                                  
            3D49E48F time date stamp Fri Aug 02 09:46:55 2002         
                0.00 version                                          
                   1 ordinal base                                     
                  11 number of functions                              
                  11 number of names                                  
                                                                      
            ordinal hint RVA      name                                
                                                                      
                  1    0 00010F60 _TM_ClearDecoderBuff@4              
                  2    1 00010E80 _TM_CloseDecoder@4                  
                  3    2 00010F00 _TM_DecodePicture@4                 
                  4    3 00010ED0 _TM_DecodePictureHeader@4           
                  5    4 00010FD0 _TM_GetFileEnd@4                    
                  6    5 00011030 _TM_GetUValue@4                     
                  7    6 00011060 _TM_GetVValue@4                     
                  8    7 00011000 _TM_GetYValue@4                     
                  9    8 00010E10 _TM_OpenDecoder@8                   
                 10    9 00010F30 _TM_ReturnType@4                    
                 11    A 00010F90 _TM_SetFileEnd@8                    
                                                                      
          Summary                                                     
                                                                      
                2000 .data                                            
                1000 .rdata                                           
                1000 .reloc                                           
               15000 .text                                            
  按照以下方法整理:
  1>添加LIB说明
   LIBRARY "VideoDeCoder"            ;"xx"为DLL名称
            DESCRIPTION "VideoDeCoder library" 
        2>去掉导出函数说明端以外的内容,在LIB说明下添加 "EXPROTS" 说明导出函数
         LIBRARY "VideoDeCoder"
   DESCRIPTION "VideoDeCoder library"

   EXPORTS
            ordinal hint RVA      name                         
                                                               
                  1    0 00010F60 _TM_ClearDecoderBuff@4           
                  2    1 00010E80 _TM_CloseDecoder@4           
                  3    2 00010F00 _TM_DecodePicture@           
                  4    3 00010ED0 _TM_DecodePictureH           
                  5    4 00010FD0 _TM_GetFileEnd@4             
                  6    5 00011030 _TM_GetUValue@4              
                  7    6 00011060 _TM_GetVValue@4              
                  8    7 00011000 _TM_GetYValue@4              
                  9    8 00010E10 _TM_OpenDecoder@8            
                 10    9 00010F30 _TM_ReturnType@4             
                 11    A 00010F90 _TM_SetFileEnd@8    
        3>将所有的函数放至行首,去掉 "hint" 和 "RVA" 数据,留下函数的序号 "ordinal" ,在序号前加上 "@" 符号
        形成 "_导出函数名@参数字节和 @序号" 此种格式(__stdcall 方式调用导出的函数符号是 "函数名称@参数字节和").
        最后形成.DEF文件如下:
        LIBRARY "VideoDeCoder"
  DESCRIPTION "VideoDeCoder library"

  EXPORTS
     
  TM_ClearDecoderBuff@4             @1   
  TM_CloseDecoder@4                 @2   
  TM_DecodePicture@4                @3   
  TM_DecodePictureHeader@4          @4   
  TM_GetFileEnd@4                   @5   
  TM_GetUValue@4                    @6   
  TM_GetVValue@4                    @7   
  TM_GetYValue@4                    @8   
  TM_OpenDecoder@8                  @9   
  TM_ReturnType@4                  @10   
  TM_SetFileEnd@8                  @11   
3. 使用VC++的LIB工具,带/DEF:(.def文件名) /MACHINE:IX86(80X86机器),就输出符合VC++格式的的LIB文件了.
 EXAMPLE:
  LIB /DEF:VideoDeCoder.def /MACHINE:IX86

4. 接时带上LIB文件链接;注意的是当有些动态库DUMPBIN的只有函数名,无"@nn"的参数格式,如C++Builder写的DLL,输出就只有函数名符号,链接时就会报错:
 error LNK2002:unresolved external symbol "functionname@nn"
提示程序中引入的函数符号无法识别,这时只要将DEF文件中相应的函数名称改为functionname@nn方式,重新建立
LIB,重新链接即可.

这样就制作成功了符合VC调用方式的LIB了!

参考:MSDN2000

要值得一说的是!BORLAND C++BUILDER有一个很好的工具IMPDEF可以直接将DLL中的函数输出到.DEF文件中,只要做一点点修改就可以成为符合VC的DEF文件!

IMPDEF xxx.def xxx.dll

这种方法只能应用于输出为C格式的__stdcall调用方式经过我的论证有些错误!我利用Borland C++和VC++做论证,发现:

而在C++BUILDER中!
__cdecl的函数输出前会带一"_"
__stdcall无特征,只输出函数名
__fastcall函数输出前带一"@"
都无"@nn"后缀格式!
在VC中!
__cdecl无特征,只输出函数名
__stdcall的函数输出前会带一"_"后缀带"@nn"
__fastcall函数输出前带一"@"后缀带"@nn

只要将BC的DEF文件中函数申明格式转换为VC识别的格式就可以利用LIB工具生成LIB;要使用C分格输出(extern "C")才是必须的!而且别忘了在DEF文件中的函数申明不要带“_”啊!:)不然会出现error LNK2001的链接错误!

本文地址:http://com.8s8s.com/it/it2163.htm