#include <windows.h>
#include <stdio.h>
DWORD MyGetProcAddress(HMODULE hMod, char strfunc[])
{
PIMAGE_DOS_HEADER peFile;
PIMAGE_NT_HEADERS peHeader;
PIMAGE_OPTIONAL_HEADER peOptionalH;
PIMAGE_DATA_DIRECTORY DataDir;
PIMAGE_EXPORT_DIRECTORY ExportTable;
DWORD numberOfFunction;
DWORD *pName, *pFunc;
size_t i;
if(!hMod) return -1;
peFile = (PIMAGE_DOS_HEADER)hMod;
if(peFile->e_magic != IMAGE_DOS_SIGNATURE) return -1;
peHeader = (PIMAGE_NT_HEADERS)((DWORD)peFile + peFile->e_lfanew);
if(peHeader->Signature != IMAGE_NT_SIGNATURE) return -1;
peOptionalH = &peHeader->OptionalHeader;
DataDir = &peOptionalH->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT];
ExportTable = (PIMAGE_EXPORT_DIRECTORY)((DWORD)peFile + DataDir->VirtualAddress);
numberOfFunction = ExportTable->NumberOfFunctions;
pName = (DWORD*)((DWORD)peFile + ExportTable->AddressOfNames);
pFunc = (DWORD*)((DWORD)peFile + ExportTable->AddressOfFunctions);
for(i=0;i<numberOfFunction;i++)
{
if(!lstrcmp((DWORD)peFile + *pName, strfunc)) return (DWORD)peFile + *pFunc;
pName++;
pFunc++;
}
return 0;
}
int main(void)
{
HMODULE hMod = GetModuleHandle("kernel32.dll");
printf("%x\n", MyGetProcAddress(hMod, "lstrlenW"));
return 0;
}