Support Forums

Full Version: [PureBasic] Alternate GetProcAddress
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Code:
Procedure.l GetProcAddressEx(*hModule, lpProcName.s)
  
  Protected *Image_Dos_Header.IMAGE_DOS_HEADER = *hModule
  
  Protected *Image_Nt_Headers.IMAGE_NT_HEADERS = *hModule + *Image_Dos_Header\e_lfanew
  
  Protected *Image_Export_Directory.IMAGE_EXPORT_DIRECTORY = *hModule + *Image_Nt_Headers\OptionalHeader\DataDirectory[0]\VirtualAddress
  
  Protected dwNumberOfNames.l
  
  Protected Ordinal.w
  
  Protected dwAddress.l
  
  Protected dwName.l

  For dwNumberOfNames = 0 To *Image_Export_Directory\NumberOfNames - 1
    
    dwAddress = *hModule + *Image_Export_Directory\AddressOfNames + (dwNumberOfNames * 4)
    
    dwName = *hModule + PeekL(dwAddress)
    
    If lstrcmp_(PeekS(dwName, #PB_Any, #PB_Ascii), lpProcName) = 0
      
      dwAddress = *Image_Export_Directory\AddressOfNameOrdinals + (dwNumberOfNames * 2)
      
      Ordinal = PeekW(*hModule + dwAddress)
      
      dwAddress = *Image_Export_Directory\AddressOfFunctions + (Ordinal * 4)
      
      ProcedureReturn *hModule + PeekL(*hModule + dwAddress)
      
    EndIf
    
  Next dwNumberOfNames
  
EndProcedure

Example usage of this procedure.

Code:
Prototype proto_MessageBox(hwnd.l, lpText.s, lpCaption.s, wType.l)
Define _MessageBox.proto_MessageBox

  _MessageBox = GetProcAddressEx(LoadLibrary_("USER32.DLL"), "MessageBoxW")

_MessageBox(0, "It Works!", "Guess What!", 0)

Advised to compile In unicode mode, and xp theme support disabled. Thanks! Victoire (Written By Me, One of my first ventures into the realm of PureBasic)
Not sure how many people on here know PureBasic, (I don't). So, i cannot say whether this is good code or not. But thanks for contributing anyways.
Yeah its a strange strange language, that only a few know, I find it to be a combination of asm/c/basic.