Advertisement
Guest User

Untitled

a guest
Nov 29th, 2012
741
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. // == Windows API GetProcAddress
  2. void *PeGetProcAddressA(void *Base, LPCSTR Name)
  3. {
  4.     DWORD Tmp;
  5.  
  6.     IMAGE_NT_HEADERS *NT=ImageNtHeader(Base);
  7.     IMAGE_EXPORT_DIRECTORY *Exp=(IMAGE_EXPORT_DIRECTORY*)ImageDirectoryEntryToDataEx(Base,TRUE,IMAGE_DIRECTORY_ENTRY_EXPORT,&Tmp,0);
  8.     if(Exp==0 || Exp->NumberOfFunctions==0)
  9.     {
  10.         SetLastError(ERROR_NOT_FOUND);
  11.         return 0;
  12.     }
  13.  
  14.     DWORD *Names=(DWORD*)(Exp->AddressOfNames+(DWORD_PTR)Base);
  15.     WORD *Ordinals=(WORD*)(Exp->AddressOfNameOrdinals+(DWORD_PTR)Base);
  16.     DWORD *Functions=(DWORD*)(Exp->AddressOfFunctions+(DWORD_PTR)Base);
  17.  
  18.     FARPROC Ret=0;
  19.  
  20.     if((DWORD_PTR)Name<65536)
  21.     {
  22.         if((DWORD_PTR)Name-Exp->Base<Exp->NumberOfFunctions)
  23.             Ret=(FARPROC)(Functions[(DWORD_PTR)Name-Exp->Base]+(DWORD_PTR)Base);
  24.     } else
  25.     {
  26.         for(DWORD i=0; i<Exp->NumberOfNames && Ret==0; i++)
  27.         {
  28.             char *Func=(char*)(Names[i]+(DWORD_PTR)Base);
  29.             if(Func && strcmp(Func,Name)==0)
  30.                 Ret=(FARPROC)(Functions[Ordinals[i]]+(DWORD_PTR)Base);
  31.         }
  32.     }
  33.  
  34.     if(Ret)
  35.     {
  36.         DWORD ExpStart=NT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress+(DWORD)Base;
  37.         DWORD ExpSize=NT->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].Size;
  38.         if((DWORD)Ret>=ExpStart && (DWORD)Ret<=ExpStart+ExpSize)
  39.         {
  40.             // Forwarder
  41.             return 0;
  42.         }
  43.         return Ret;
  44.     }
  45.  
  46.     return 0;  
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement