Support Forums
[PureBasic] Alternate GetProcAddress - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Visual Basic and the .NET Framework (https://www.supportforums.net/forumdisplay.php?fid=19)
+---- Thread: [PureBasic] Alternate GetProcAddress (/showthread.php?tid=15464)



[PureBasic] Alternate GetProcAddress - Modest - 01-09-2011

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)


RE: [PureBasic] Alternate GetProcAddress - KoBE - 01-10-2011

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.


RE: [PureBasic] Alternate GetProcAddress - Modest - 01-10-2011

Yeah its a strange strange language, that only a few know, I find it to be a combination of asm/c/basic.