News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Listing the exports of a DLL.

Started by Vortex, May 19, 2024, 08:22:13 PM

Previous topic - Next topic

Vortex

Hello,

This example uses MrBcx's KAST macro to specify the type castings :

#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;
}


greenozon

 hMod = LoadLibrary("kernel32.dll");


this statement might return NULL
so not checking for this might hard hit the app...
just in case :)

PS I know the chance of getting NULL is 0.0001% but who knows

Vortex

#2
Hi greenozon,

This is a quick example to demonstrate the usage of the KAST macro. A similar project here is taking care of the handle against NULL :

https://masm32.com/board/index.php?topic=11438.0

hMod=LoadLibrary(cmd);
  if(hMod==0 ){
      printf("%s%s%s\n","Error : ",cmd," could not be loaded.");
      fflush(stdout);
      ExitProcess(0);
    }