The MASM Forum

Specialised Projects => CoderStudio => Topic started by: Vortex on May 19, 2024, 08:22:13 PM

Title: Listing the exports of a DLL.
Post by: Vortex on May 19, 2024, 08:22:13 PM
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;
}

Title: Re: Listing the exports of a DLL.
Post by: greenozon on May 20, 2024, 06:12:54 PM
 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
Title: Re: Listing the exports of a DLL.
Post by: Vortex on May 21, 2024, 04:09:20 AM
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 (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);
    }