显示DLL可导出的函数
点击次数:51 次 发布日期:2008-11-09 08:38:15 作者:源代码网
|
源代码网推荐 源代码网推荐 // 完整的DLLTools单元的代码在示例代码之后 源代码网推荐 源代码网推荐 uses DLLTools; 源代码网推荐 源代码网推荐 function TForm1.ListExport( const name: String; ordinal: Integer; address:Pointer ): Boolean; 源代码网推荐 var 源代码网推荐 listentry: TLIstItem; 源代码网推荐 begin 源代码网推荐 Result := true; 源代码网推荐 listentry:= listview.Items.Add; 源代码网推荐 listentry.Caption := Format("%p",[address] ); 源代码网推荐 listentry.Subitems.Add( format("%d",[ordinal] )); 源代码网推荐 listentry.Subitems.Add( name ); 源代码网推荐 end; 源代码网推荐 源代码网推荐 procedure TForm1.Button1Click(Sender: TObject); 源代码网推荐 begin 源代码网推荐 if opendialog.execute then 源代码网推荐 begin 源代码网推荐 listview.items.clear; 源代码网推荐 ListDLLExports( opendialog.filename, listexport ); 源代码网推荐 end; 源代码网推荐 end; 源代码网推荐 源代码网推荐 源代码网推荐 ***** 源代码网推荐 DLLTOOLS 单元 源代码网推荐 ***** 源代码网推荐 源代码网推荐 源代码网推荐 unit dlltools; 源代码网推荐 源代码网推荐 interface 源代码网推荐 源代码网推荐 Uses Windows, Classes, Sysutils, imagehlp ; 源代码网推荐 源代码网推荐 type 源代码网推荐 TDLLExportCallback = function (const name: String; ordinal: Integer; 源代码网推荐 address: Pointer): Boolean of Object; 源代码网推荐 { Note: address is a RVA here, not a usable virtual address! } 源代码网推荐 DLLToolsError = Class( Exception ); 源代码网推荐 源代码网推荐 Procedure ListDLLExports( const filename: String; callback: 源代码网推荐 TDLLExportCallback ); 源代码网推荐 Procedure DumpExportDirectory( Const ExportDirectory: TImageExportDirectory; 源代码网推荐 lines: TStrings; const Image: LoadedImage ); 源代码网推荐 Function RVAToPchar( rva: DWORD; const Image: LoadedImage ): PChar; 源代码网推荐 Function RVAToPointer( rva: DWORD; const Image: LoadedImage ): Pointer; 源代码网推荐 源代码网推荐 implementation 源代码网推荐 源代码网推荐 resourcestring 源代码网推荐 eDLLNotFound = 源代码网推荐 "ListDLLExports: DLL %s does not exist!"; 源代码网推荐 源代码网推荐 { ---------------------------------------------------------------------- 源代码网推荐 | Procedure EnumExports 源代码网推荐 | 源代码网推荐 | Parameters : 源代码网推荐 | ExportDirectory: IMAGE_EXPORT_DIRECTORY record to enumerate 源代码网推荐 | image : LOADED_IMAGE record for the DLL the export directory belongs 源代码网推荐 | to. 源代码网推荐 | callback : callback function to hand the found exports to, must not be 源代码网推荐 Nil 源代码网推荐 | Description: 源代码网推荐 | The export directory of a PE image contains three RVAs that point at 源代码网推荐 tables 源代码网推荐 | which describe the exported functions. The first is an array of RVAs 源代码网推荐 that 源代码网推荐 | refer to the exported function names, these we translate to PChars to 源代码网推荐 | get the exported name. The second array is an array of Word that 源代码网推荐 contains 源代码网推荐 | the export ordinal for the matching entry in the names array. The 源代码网推荐 ordinal 源代码网推荐 | is biased, that is we have to add the ExportDirectory.Base value to it 源代码网推荐 to 源代码网推荐 | get the actual export ordinal. The biased ordinal serves as index for 源代码网推荐 the 源代码网推荐 | third array, which is an array of RVAs that give the position of the 源代码网推荐 | function code in the image. We don"t translate these RVAs since the DLL 源代码网推荐 | is not relocated since we load it via MapAndLoad. The function array is 源代码网推荐 | usually much larger than the names array, since the ordinals for the 源代码网推荐 | exported functions do not have to be in sequence, there can be (and 源代码网推荐 | frequently are) gaps in the sequence, for which the matching entries in 源代码网推荐 the 源代码网推荐 | function RVA array are garbage. 软件开发网 www.mscto.com 源代码网推荐 | Error Conditions: none 源代码网推荐 | Created: 9.1.2000 by P. Below 源代码网推荐 ----------------------------------------------------------------------} 源代码网推荐 Procedure EnumExports( const ExportDirectory : TImageExportDirectory ; 源代码网推荐 const image : LoadedImage ; 源代码网推荐 callback : TDLLExportCallback ) ; 源代码网推荐 Type 源代码网推荐 TDWordArray = Array [0..$FFFFF] of DWORD; 源代码网推荐 Var 源代码网推荐 i: Cardinal; 源代码网推荐 pNameRVAs, pFunctionRVas: ^TDWordArray; 源代码网推荐 pOrdinals: ^TWordArray; 源代码网推荐 name: String; 源代码网推荐 address: Pointer; 源代码网推荐 ordinal: Word; 源代码网推荐 Begin { EnumExports } 源代码网推荐 pNameRVAs := 源代码网推荐 RVAToPointer( DWORD(ExportDirectory.AddressOfNames), image ); 源代码网推荐 pFunctionRVAs := 源代码网推荐 RVAToPointer( DWORD(ExportDirectory.AddressOfFunctions), image ); 源代码网推荐 pOrdinals := 源代码网推荐 RVAToPointer( DWORD(ExportDirectory.AddressOfNameOrdinals), image ); 源代码网推荐 For i:= 0 to Pred( ExportDirectory.NumberOfNames ) Do Begin 源代码网推荐 name := RVAToPChar( pNameRVAs^[i], image ); 源代码网推荐 ordinal := pOrdinals^[i]; 源代码网推荐 address := Pointer( pFunctionRVAs^[ ordinal ] ); 源代码网推荐 If not callback( name, ordinal ExportDirectory.Base, address ) Then 软件开发网 www.mscto.com 源代码网推荐 Exit; 源代码网推荐 End; { For } 源代码网推荐 End; { EnumExports } 源代码网推荐 源代码网推荐 { ---------------------------------------------------------------------- 源代码网推荐 | Procedure ListDLLExports 源代码网推荐 | 源代码网推荐 | Parameters : 源代码网推荐 | filename : full pathname of DLL to examine 源代码网推荐 | callback : callback to hand the found exports to, must not be Nil 源代码网推荐 | Description: 源代码网推荐 | Loads the passed DLL using the LoadImage function, finds the exported 源代码网推荐 | names table and reads it. Each found entry is handed to the callback 源代码网推荐 | for further processing, until no more entries remain or the callback 源代码网推荐 | returns false. Note that the address passed to the callback for a 源代码网推荐 exported 源代码网推荐 | function is an RVA, so not identical to the address the function would 源代码网推荐 | have in a properly loaded and relocated DLL! 源代码网推荐 | Error Conditions: 源代码网推荐 | Exceptions are raised if 源代码网推荐 | - the passed DLL does not exist or could not be loaded 源代码网推荐 | - no callback was passed (only if assertions are on) 源代码网推荐 | - an API function failed 源代码网推荐 | Created: 9.1.2000 by P. Below 软件开发网 www.mscto.com 源代码网推荐 ----------------------------------------------------------------------} 源代码网推荐 Procedure ListDLLExports( const filename : String ; callback : 源代码网推荐 TDLLExportCallback ) ; 源代码网推荐 Var 源代码网推荐 imageinfo: LoadedImage; 源代码网推荐 pExportDirectory: PImageExportDirectory; 源代码网推荐 dirsize: Cardinal; 源代码网推荐 Begin { ListDLLExports } 源代码网推荐 Assert( Assigned( callback )); 源代码网推荐 If not FileExists( filename ) Then 源代码网推荐 raise DLLToolsError.CreateFmt( eDLLnotFound, [filename] ); 源代码网推荐 源代码网推荐 If MapAndLoad( PChar( filename ), nil, @imageinfo, true, true ) Then 源代码网推荐 try 源代码网推荐 pExportDirectory := 源代码网推荐 ImageDirectoryEntryToData( 源代码网推荐 imageinfo.MappedAddress, false, 源代码网推荐 IMAGE_DIRECTORY_ENTRY_EXPORT, dirsize ); 源代码网推荐 源代码网推荐 If pExportDirectory = Nil Then 源代码网推荐 RaiseLastWin32Error 源代码网推荐 Else 源代码网推荐 EnumExports( pExportDirectory^, imageinfo, callback ); 源代码网推荐 finally 源代码网推荐 UnMapAndLoad( @imageinfo ); 源代码网推荐 end 源代码网推荐 Else 源代码网推荐 RaiseLastWin32Error; 源代码网推荐 End; { ListDLLExports } 源代码网推荐 源代码网推荐 { ---------------------------------------------------------------------- 源代码网推荐 | Procedure DumpExportDirectory 源代码网推荐 | 源代码网推荐 | Parameters : 源代码网推荐 | ExportDirectory: a IMAGE_EXPORT_DIRECTORY record 源代码网推荐 | lines : a TStrings descendend to put the info into, must not be Nil 源代码网推荐 | Description: 源代码网推荐 | Dumps the fields of the passed structure to the passed strings 源代码网推荐 descendent 源代码网推荐 | as strings. 源代码网推荐 | Error Conditions: 源代码网推荐 | will raise an exception if lines is Nil and assertions are enabled. 源代码网推荐 | Created: 9.1.2000 by P. Below 源代码网推荐 ----------------------------------------------------------------------} 源代码网推荐 Procedure DumpExportDirectory( Const ExportDirectory : TImageExportDirectory; 源代码网推荐 lines : TStrings; const Image: LoadedImage ) ; 源代码网推荐 Begin { DumpExportDirectory } 源代码网推荐 Assert( Assigned( lines )); 源代码网推荐 源代码网推荐 lines.add( "Dump of IMAGE_EXPORT_DIRECTORY" ); 源代码网推荐 lines.add( format("Characteristics: %d", 源代码网推荐 [ExportDirectory.Characteristics])); 源代码网推荐 lines.add( format("TimeDateStamp: %d", 源代码网推荐 [ExportDirectory.TimeDateStamp])); 源代码网推荐 lines.add( format("Version: %d.%d", 源代码网推荐 [ExportDirectory.MajorVersion, 源代码网推荐 ExportDirectory.MinorVersion])); 源代码网推荐 lines.add( format("Name (RVA): %x", 源代码网推荐 [ExportDirectory.Name])); 源代码网推荐 lines.add( format("Name (translated): %s", 源代码网推荐 [RVAToPchar( ExportDirectory.name, Image )])); 源代码网推荐 lines.add( format("Base: %d", 源代码网推荐 [ExportDirectory.Base])); 源代码网推荐 lines.add( format("NumberOfFunctions: %d", 源代码网推荐 [ExportDirectory.NumberOfFunctions])); 源代码网推荐 lines.add( format("NumberOfNames: %d", 源代码网推荐 [ExportDirectory.NumberOfNames])); 源代码网推荐 lines.add( format("AddressOfFunctions (RVA): %p", 源代码网推荐 [Pointer(ExportDirectory.AddressOfFunctions)])); 源代码网推荐 lines.add( format("AddressOfNames (RVA): %p", 源代码网推荐 [Pointer(ExportDirectory.AddressOfNames)])); 源代码网推荐 lines.add( format("AddressOfNameOrdinals (RVA): %p", 源代码网推荐 [Pointer(ExportDirectory.AddressOfNameOrdinals)])); 源代码网推荐 End; { DumpExportDirectory } 源代码网推荐 源代码网推荐 { ---------------------------------------------------------------------- 源代码网推荐 | Function RVAToPointer 源代码网推荐 | 源代码网推荐 | Parameters : 源代码网推荐 | rva : a relative virtual address to translate 源代码网推荐 | Image : LOADED_IMAGE structure for the image the RVA relates to 源代码网推荐 | Returns : translated address 源代码网推荐 | Description: 源代码网推荐 | Uses the ImageRVAToVA function to translate the RVA to a virtual 源代码网推荐 | address. 源代码网推荐 | Error Conditions: 源代码网推荐 | Will raise an exception if the translation failed 源代码网推荐 | Created: 9.1.2000 by P. Below 源代码网推荐 ----------------------------------------------------------------------} 源代码网推荐 Function RVAToPointer( rva : DWORD ; const Image : LoadedImage ) : Pointer; 源代码网推荐 var 源代码网推荐 pDummy: PImageSectionHeader; 源代码网推荐 Begin { RVAToPchar } 源代码网推荐 pDummy := nil; 源代码网推荐 Result := 源代码网推荐 ImageRvaToVa( Image.FileHeader, Image.MappedAddress, rva, 源代码网推荐 pDummy ); 源代码网推荐 If Result = Nil Then 源代码网推荐 RaiseLastWin32Error; 源代码网推荐 End; { RVAToPointer } 源代码网推荐 源代码网推荐 { ---------------------------------------------------------------------- 源代码网推荐 | Function RVAToPchar 源代码网推荐 | 源代码网推荐 | Parameters : 源代码网推荐 | rva : a relative virtual address to translate 源代码网推荐 | Image : LOADED_IMAGE structure for the image the RVA relates to 源代码网推荐 | Returns : translated address 源代码网推荐 | Description: 源代码网推荐 | Uses the RVAToPointer function to translate the RVA to a virtual 源代码网推荐 | address. Note that we do not check that the address does indeed point 源代码网推荐 | to a zero-terminated string! 源代码网推荐 | Error Conditions: 源代码网推荐 | Will raise an exception if the translation failed 源代码网推荐 | Created: 9.1.2000 by P. Below 源代码网推荐 ----------------------------------------------------------------------} 源代码网推荐 Function RVAToPchar( rva : DWORD ; const Image : LoadedImage ) : PChar ; 源代码网推荐 Begin { RVAToPchar } 源代码网推荐 Result := RVAToPointer( rva, image ); 源代码网推荐 End; { RVAToPchar } 源代码网推荐 源代码网推荐 end. 源代码网推荐 源代码网供稿. |
