
/**
 * @file: listdll-toolhelp.c
 *
 * Tento modul implementuje funkce pro ziskavani seznamu modulu ciloveho
 * procesu pomoci knihovny ToolHelp.
 */

#include <stdio.h>
#include <windows.h>
#include <TlHelp32.h>
#include "common.h"
#include "moduleinfo.h"




/** Zjisti seznam modulu ciloveho procesu pomoci knihovny Tool Help.
 *
 *  @param PID Identifikator ciloveho procesu.
 *  @param ModuleList Adresa promenne, kam funkce v pripade uspechu zapise
 *  adresu prvniho prvku seznamu modulu ciloveho procesu.
 *
 *  @return Funkce vraci TRUE v pripade uspechu a FALSE v pripade 
 *  neuspechu.
 */
BOOL ListDllToolHelpGetModules(DWORD PID, PMODULE_INFO_ITEM *ModuleList)
{
  BOOL Ret = FALSE;
  HANDLE hSnapshot = NULL;
  MODULEENTRY32W me;
  PMODULE_INFO_ITEM ModuleListEnd = NULL;
  PMODULE_INFO_ITEM ModuleInfoItem = NULL;
  DebugMsg(("listdll-toolhelp.c: ListDllToolHelpGetModules(PID=%u; ModuleList=0x%p)\n", PID, ModuleList));

  *ModuleList = NULL;
  hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, PID);
  Ret = hSnapshot != NULL;
  if (Ret) {
    ZeroMemory(&me, sizeof(me));
    me.dwSize = sizeof(me);
    Ret = Module32FirstW(hSnapshot, &me);
    if (Ret) {
      do {
        ModuleInfoItem = ModuleInfoItemAlloc(me.modBaseAddr, me.modBaseSize, me.szExePath, wcslen(me.szExePath) * sizeof(WCHAR));
		Ret = ModuleInfoItem != NULL;
		if (Ret) {
          if (*ModuleList == NULL)
            *ModuleList = ModuleInfoItem;
		  else ModuleInfoItemInsert(ModuleListEnd, ModuleInfoItem);

		  ModuleListEnd = ModuleInfoItem;
        } else DebugMsg(("listdll-toolhelp.c: ListDllToolHelpGetModules(): ModuleInfoItemAlloc failed with error %u\n", GetLastError()));

        ZeroMemory(&me, sizeof(me));
        me.dwSize = sizeof(me);
	  } while (Module32NextW(hSnapshot, &me));
	} else DebugMsg(("listdll-toolhelp.c: ListDllToolHelpGetModules(): Module32First failed with error %u\n", GetLastError()));

    CloseHandle(hSnapshot);
  } else DebugMsg(("listdll-toolhelp.c: ListDllToolHelpGetModules(): CreateToolhelp32Snapshot failed with error %u\n", GetLastError()));
 
  DebugMsg(("listdll-toolhelp.c: ListDllToolHelpGetModules(-):%u, *ModuleList=0x%p\n", Ret, ModuleList));
  return Ret;
}



/** Inicializuje modul pro ziskavani seznamu modulu procesu pres knihovnu
 *  Toolhelp.
 *
 *  @return Funkce vraci TRUE v pripade uspechu a FALSE v pripade neuspechu.
 */
BOOL ListDllToolHelpInit(VOID)
{
  BOOL Ret = FALSE;
  DebugMsg(("listdll-toolhelp.c: ListDllToolHelpInit()\n"));

  Ret = TRUE;

  DebugMsg(("listdll-toolhelp.c: ListDllToolHelpInit(-):%u\n", Ret));
  return Ret;
}


/** Provede uklid po modulu zjistujicim seznam modulu procesu pomoci
 *  knihovny Toolhelp.
 *
 *  Jelikoz knihovna Toolhelp a ani modul samotny inicializaci vlastne
 *  nepotrebuje (inicializacni rutina je tu jen pro konzistenci), tato
 *  rutina nedela nic.
 */
VOID ListDllToolHelpFinit(VOID)
{
  DebugMsg(("listdll-toolhelp.c: ListDllToolHelpFinit()\n"));

  DebugMsg(("listdll-toolhelp.c: ListDllToolHelpFinit(-)\n"));
  return;
}
