调出dll文件接口的源码<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,<br> Dialogs, StdCtrls, ComCtrls;<br><br>type<br> TFrmmain = class(TForm)<br> Button1: TButton;<br> ListBox1: TListBox;<br> OpenDialog1: TOpenDialog;<br> ListView1: TListView;<br> function ListExport( const name: String; ordinal: Integer; address
ointer ): Boolean;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br><br>var<br> Frmmain: TFrmmain;<br><br>implementation<br>uses dlltools;<br><br>{$R *.dfm}<br>function TFrmmain.ListExport( const name: String; ordinal: Integer; address
ointer ): Boolean;<br>var<br> listentry:TLIstItem;<br> begin<br> Result := true; <br><br> listentry:= listview1.Items.Add;<br><br> listbox1.Items.Add(Format('%p',[address] )+','+<br> format('%d',[ordinal] )+','+<br> name<br> );<br> listentry.Caption := Format('%p',[address] );<br><br> listentry.Subitems.Add( format('%d',[ordinal] ));<br> // edit1.Text:=format('%d',[ordinal] );<br> // edit2.Text:=name;<br> listentry.Subitems.Add( name );<br><br> end;<br><br>procedure TFrmmain.Button1Click(Sender: TObject);<br>begin<br>if opendialog1.execute then<br><br> begin<br><br> listview1.items.clear;<br> listbox1.Items.Clear;<br> ListDLLExports( opendialog1.filename, listexport );<br><br> end;<br><br><br>end;<br><br>end.<br><br><br><br><br><br><br>unit dlltools; <br><br> <br><br> interface <br><br> <br><br> Uses Windows, Classes, Sysutils, imagehlp ; <br><br> <br><br> type <br><br> TDLLExportCallback = function (const name: String; ordinal: Integer; <br><br> address: Pointer): Boolean of Object; <br><br> { Note: address is a RVA here, not a usable virtual address! } <br><br> DLLToolsError = Class( Exception ); <br><br> <br><br> Procedure ListDLLExports( const filename: String; callback: <br><br> TDLLExportCallback ); <br><br> Procedure DumpExportDirectory( Const ExportDirectory: TImageExportDirectory; <br><br> lines: TStrings; const Image: LoadedImage ); <br><br> Function RVAToPchar( rva: DWORD; const Image: LoadedImage ): PChar; <br><br> Function RVAToPointer( rva: DWORD; const Image: LoadedImage ): Pointer; <br><br> <br><br> implementation <br><br> <br><br> resourcestring <br><br> eDLLNotFound = <br><br> 'ListDLLExports: DLL %s does not exist!'; <br><br> <br><br> {+---------------------------------------------------------------------- <br><br> | Procedure EnumExports <br><br> | <br><br> | Parameters : <br><br> | ExportDirectory: IMAGE_EXPORT_DIRECTORY record to enumerate <br><br> | image : LOADED_IMAGE record for the DLL the export directory belongs <br><br> | to. <br><br> | callback : callback function to hand the found exports to, must not be <br><br> Nil <br><br> | Description: <br><br> | The export directory of a PE image contains three RVAs that point at <br><br> tables <br><br> | which describe the exported functions. The first is an array of RVAs <br><br> that <br><br> | refer to the exported function names, these we translate to PChars to <br><br> | get the exported name. The second array is an array of Word that <br><br> contains <br><br> | the export ordinal for the matching entry in the names array. The <br><br> ordinal <br><br> | is biased, that is we have to add the ExportDirectory.Base value to it <br><br> to <br><br> | get the actual export ordinal. The biased ordinal serves as index for <br><br> the <br><br> | third array, which is an array of RVAs that give the position of the <br><br> | function code in the image. We don't translate these RVAs since the DLL <br><br> | is not relocated since we load it via MapAndLoad. The function array is <br><br> | usually much larger than the names array, since the ordinals for the <br><br> | exported functions do not have to be in sequence, there can be (and <br><br> | frequently are) gaps in the sequence, for which the matching entries in <br><br> the <br><br> | function RVA array are garbage. <br><br> | Error Conditions: none <br><br> | Created: 9.1.2000 by P. Below <br><br> +----------------------------------------------------------------------} <br><br> Procedure EnumExports( const ExportDirectory : TImageExportDirectory ; <br><br> const image : LoadedImage ; <br><br> callback : TDLLExportCallback ) ; <br><br> Type <br><br> TDWordArray = Array [0..$FFFFF] of DWORD; <br><br> Var <br><br> i: Cardinal; <br><br> pNameRVAs, pFunctionRVas: ^TDWordArray; <br><br> pOrdinals: ^TWordArray; <br><br> name: String; <br><br> address: Pointer; <br><br> ordinal: Word; <br><br> Begin { EnumExports } <br><br> pNameRVAs := <br><br> RVAToPointer( DWORD(ExportDirectory.AddressOfNames), image ); <br><br> pFunctionRVAs := <br><br> RVAToPointer( DWORD(ExportDirectory.AddressOfFunctions), image ); <br><br> pOrdinals := <br><br> RVAToPointer( DWORD(ExportDirectory.AddressOfNameOrdinals), image ); <br><br> For i:= 0 to Pred( ExportDirectory.NumberOfNames ) Do Begin <br><br> name := RVAToPChar( pNameRVAs^
, image ); <br><br> ordinal := pOrdinals^; <br><br> address := Pointer( pFunctionRVAs^[ ordinal ] ); <br><br> If not callback( name, ordinal+ExportDirectory.Base, address ) Then <br><br> Exit; <br><br> End; { For } <br><br> End; { EnumExports } <br><br> <br><br> {+---------------------------------------------------------------------- <br><br> | Procedure ListDLLExports <br><br> | <br><br> | Parameters : <br><br> | filename : full pathname of DLL to examine <br><br> | callback : callback to hand the found exports to, must not be Nil <br><br> | Description: <br><br> | Loads the passed DLL using the LoadImage function, finds the exported <br><br> | names table and reads it. Each found entry is handed to the callback <br><br> | for further processing, until no more entries remain or the callback <br><br> | returns false. Note that the address passed to the callback for a <br><br> exported <br><br> | function is an RVA, so not identical to the address the function would <br><br> | have in a properly loaded and relocated DLL! <br><br> | Error Conditions: <br><br> | Exceptions are raised if <br><br> | - the passed DLL does not exist or could not be loaded <br><br> | - no callback was passed (only if assertions are on) <br><br> | - an API function failed <br><br> | Created: 9.1.2000 by P. Below <br><br> +----------------------------------------------------------------------} <br><br> Procedure ListDLLExports( const filename : String ; callback : TDLLExportCallback ) ; <br><br> Var <br><br> imageinfo: LoadedImage; <br><br> pExportDirectory: PImageExportDirectory; <br><br> dirsize: Cardinal; <br><br> Begin { ListDLLExports } <br><br> Assert( Assigned( callback )); <br><br> If not FileExists( filename ) Then <br><br> raise DLLToolsError.CreateFmt( eDLLnotFound, [filename] ); <br><br> <br><br> If MapAndLoad( PChar( filename ), nil, @imageinfo, true, true ) Then <br><br> try <br><br> pExportDirectory := <br><br> ImageDirectoryEntryToData( <br><br> imageinfo.MappedAddress, false, <br><br> IMAGE_DIRECTORY_ENTRY_EXPORT, dirsize ); <br><br> <br><br> If pExportDirectory = Nil Then <br><br> RaiseLastWin32Error <br><br> Else <br><br> EnumExports( pExportDirectory^, imageinfo, callback ); <br><br> finally <br><br> UnMapAndLoad( @imageinfo ); <br><br> end <br><br> Else <br><br> RaiseLastWin32Error; <br><br> End; { ListDLLExports } <br><br> <br><br> {+---------------------------------------------------------------------- <br><br> | Procedure DumpExportDirectory <br><br> | <br><br> | Parameters : <br><br> | ExportDirectory: a IMAGE_EXPORT_DIRECTORY record <br><br> | lines : a TStrings descendend to put the info into, must not be Nil <br><br> | Description: <br><br> | Dumps the fields of the passed structure to the passed strings <br><br> descendent <br><br> | as strings. <br><br> | Error Conditions: <br><br> | will raise an exception if lines is Nil and assertions are enabled. <br><br> | Created: 9.1.2000 by P. Below <br><br> +----------------------------------------------------------------------} <br><br> Procedure DumpExportDirectory( Const ExportDirectory : TImageExportDirectory; <br><br> lines : TStrings; const Image: LoadedImage ) ; <br><br> Begin { DumpExportDirectory } <br><br> Assert( Assigned( lines )); <br><br> <br><br> lines.add( 'Dump of IMAGE_EXPORT_DIRECTORY' ); <br><br> lines.add( format('Characteristics: %d', <br><br> [ExportDirectory.Characteristics])); <br><br> lines.add( format('TimeDateStamp: %d', <br><br> [ExportDirectory.TimeDateStamp])); <br><br> lines.add( format('Version: %d.%d', <br><br> [ExportDirectory.MajorVersion, <br><br> ExportDirectory.MinorVersion])); <br><br> lines.add( format('Name (RVA): %x', <br><br> [ExportDirectory.Name])); <br><br> lines.add( format('Name (translated): %s', <br><br> [RVAToPchar( ExportDirectory.name, Image )])); <br><br> lines.add( format('Base: %d', <br><br> [ExportDirectory.Base])); <br><br> lines.add( format('NumberOfFunctions: %d', <br><br> [ExportDirectory.NumberOfFunctions])); <br><br> lines.add( format('NumberOfNames: %d', <br><br> [ExportDirectory.NumberOfNames])); <br><br> lines.add( format('AddressOfFunctions (RVA): %p', <br><br> [Pointer(ExportDirectory.AddressOfFunctions)])); <br><br> lines.add( format('AddressOfNames (RVA): %p', <br><br> [Pointer(ExportDirectory.AddressOfNames)])); <br><br> lines.add( format('AddressOfNameOrdinals (RVA): %p', <br><br> [Pointer(ExportDirectory.AddressOfNameOrdinals)])); <br><br> End; { DumpExportDirectory } <br><br> <br><br> {+---------------------------------------------------------------------- <br><br> | Function RVAToPointer <br><br> | <br><br> | Parameters : <br><br> | rva : a relative virtual address to translate <br><br> | Image : LOADED_IMAGE structure for the image the RVA relates to <br><br> | Returns : translated address <br><br> | Description: <br><br> | Uses the ImageRVAToVA function to translate the RVA to a virtual <br><br> | address. <br><br> | Error Conditions: <br><br> | Will raise an exception if the translation failed <br><br> | Created: 9.1.2000 by P. Below <br><br> +----------------------------------------------------------------------} <br><br> Function RVAToPointer( rva : DWORD ; const Image : LoadedImage ) : Pointer; <br><br> var <br><br> pDummy: PImageSectionHeader; <br><br> Begin { RVAToPchar } <br><br> pDummy := nil; <br><br> Result := <br><br> ImageRvaToVa( Image.FileHeader, Image.MappedAddress, rva, <br><br> pDummy ); <br><br> If Result = Nil Then <br><br> RaiseLastWin32Error; <br><br> End; { RVAToPointer } <br><br> <br><br> {+---------------------------------------------------------------------- <br><br> | Function RVAToPchar <br><br> | <br><br> | Parameters : <br><br> | rva : a relative virtual address to translate <br><br> | Image : LOADED_IMAGE structure for the image the RVA relates to <br><br> | Returns : translated address <br><br> | Description: <br><br> | Uses the RVAToPointer function to translate the RVA to a virtual <br><br> | address. Note that we do not check that the address does indeed point <br><br> | to a zero-terminated string! <br><br> | Error Conditions: <br><br> | Will raise an exception if the translation failed <br><br> | Created: 9.1.2000 by P. Below <br><br> +----------------------------------------------------------------------} <br><br> Function RVAToPchar( rva : DWORD ; const Image : LoadedImage ) : PChar ; <br><br> Begin { RVAToPchar } <br><br> Result := RVAToPointer( rva, image ); <br><br> End; { RVAToPchar } <br><br> <br><br> end. <br><br>