A nice macro by MrBcx used in the BCX language.
Traditional type casting :
#include <windows.h>
#include <stdio.h>
int main(void)
{
HMODULE hMod;
PIMAGE_NT_HEADERS pNThdr;
PIMAGE_EXPORT_DIRECTORY pDesc;
DWORD NumbOfNames,i;
DWORD* AddrOfNames;
hMod=LoadLibrary("kernel32.dll");
pNThdr=(PIMAGE_NT_HEADERS)((LPBYTE)hMod + ((PIMAGE_DOS_HEADER)hMod)->e_lfanew);
pDesc=(PIMAGE_EXPORT_DIRECTORY)((LPBYTE)hMod + pNThdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
NumbOfNames = pDesc -> NumberOfNames;
AddrOfNames = (DWORD *)((LPBYTE)hMod + pDesc -> AddressOfNames);
for(i=0;i<NumbOfNames;i++)
{
printf("%s\n",(*AddrOfNames+(LPBYTE)hMod));
AddrOfNames=AddrOfNames+1;
}
FreeLibrary(hMod);
return 0;
}
The KAST macro for type casting :
#include <windows.h>
#include <stdio.h>
/* KAST macro by MrBcx */
#define KAST(to_type,old_obj) ((to_type)(old_obj))
int main(void)
{
HMODULE hMod;
PIMAGE_NT_HEADERS pNThdr;
PIMAGE_EXPORT_DIRECTORY pDesc;
DWORD NumbOfNames,i;
DWORD* AddrOfNames;
hMod=LoadLibrary("kernel32.dll");
pNThdr=KAST(PIMAGE_NT_HEADERS,KAST(LPBYTE,hMod) + KAST(PIMAGE_DOS_HEADER,hMod)->e_lfanew);
pDesc=KAST(PIMAGE_EXPORT_DIRECTORY,KAST(LPBYTE,hMod) + pNThdr->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);
NumbOfNames = pDesc -> NumberOfNames;
AddrOfNames = KAST(DWORD *,KAST(LPBYTE,hMod) + pDesc -> AddressOfNames);
for(i=0;i<NumbOfNames;i++)
{
printf("%s\n",*AddrOfNames+KAST(LPBYTE,hMod));
AddrOfNames=AddrOfNames+1;
}
FreeLibrary(hMod);
return 0;
}