Code: Select all
; Here it is how to enumerate the exported functions from a DLL using API and some pointer arithmetic instead of PB commands.
; Should work for Ascii/Unicode/x86/x64
; Why use this ? I don't know.
; Maybe the ImageDirectoryEntryToData() usage example can be useful as a starting point for someone wanting to explore the PE file format.
EnableExplicit
#PBCODE = 1
#DLLNAME$ = "OPENGL32.DLL" ; also try "USER32.DLL"
CompilerIf #PBCODE = 1
Define hDLL
Debug "USING PB COMMANDS"
hDLL = OpenLibrary(#PB_Any, #DLLNAME$)
If hDLL
Debug CountLibraryFunctions(hDLL)
If ExamineLibraryFunctions(hDLL)
While NextLibraryFunction()
Debug LibraryFunctionName() + " = $" + Hex(LibraryFunctionAddress())
Wend
EndIf
CloseLibrary(hDLL)
EndIf
CompilerElse
Define hDLL
Define *ied.IMAGE_EXPORT_DIRECTORY
Define NumberOfNames, size, i, ord
Define *AddressOfFunctions, *AddressOfNames, *AddressOfNameOrdinals
Define FunctionName$, *FunctionAddress
Debug "USING API AND IMAGE_EXPORT_DIRECTORY STRUCTURE"
hDLL = LoadLibrary_(#DLLNAME$)
If hDLL
*ied = ImageDirectoryEntryToData_(hDLL, 1, #IMAGE_DIRECTORY_ENTRY_EXPORT, @size)
If *ied
NumberOfNames = *ied\NumberOfNames
Debug NumberOfNames
*AddressOfFunctions = hDLL + *ied\AddressOfFunctions
*AddressOfNames = hDLL + *ied\AddressOfNames
*AddressOfNameOrdinals = hDLL + *ied\AddressOfNameOrdinals
For i = 0 To NumberOfNames - 1
FunctionName$ = PeekS(hDLL + PeekL(*AddressOfNames + (i * SizeOf(Long))), -1, #PB_Ascii)
ord = PeekU(*AddressOfNameOrdinals + (i * SizeOf(Word)))
*FunctionAddress = hDLL + PeekL(*AddressOfFunctions + (ord * SizeOf(Long)))
Debug FunctionName$ + " = $" + Hex(*FunctionAddress)
Next
EndIf
FreeLibrary_(hDLL)
EndIf
CompilerEndIf
BTW: Processing USER32.DLL with the PB code branch I get strange results, while I seem to get nice ones with my code.
Did I found a bug while doing this ?