LoadDLL.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #pragma once
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #ifdef __cplusplus
  5. extern "C" {
  6. #endif
  7. /*
  8. // Conventions:
  9. // RVA: relative virtual address
  10. // VA: virtual address that is RVA+ImageBase
  11. //
  12. // LOAD_DLL_INFO
  13. // A structure that holds information about a "manually loaded" DLL.
  14. //
  15. // - size: size of the structure.
  16. // - flags: The flags parameter that was passed to the LoadDLL() function.
  17. // - image_base: VA that was used to do the base relocation.
  18. // It is always the same as mem_block when you loaded the DLL including the headers.
  19. // - mem_block: VA of the big memory block that was allocated to hold the data of
  20. // sections and additionally the headers if we want.
  21. // - dll_main: VA of DllMain(). Be careful cause it can be NULL.
  22. // - export_dir_rva: RVA of the export directory of the DLL.
  23. // It can be NULL if there is no export info. It is needed when you want to load a
  24. // DLL without header but the address of exported functions must be retrieved.
  25. */
  26. typedef BOOL (WINAPI *LPDLLMAIN)(DWORD_PTR image_base, DWORD reason, LPVOID reserved);
  27. typedef struct LOAD_DLL_INFO
  28. {
  29. size_t size;
  30. int flags;
  31. DWORD_PTR image_base;
  32. void* mem_block;
  33. LPDLLMAIN dll_main;
  34. DWORD export_dir_rva;
  35. HMODULE* loaded_import_modules_array;
  36. unsigned num_import_modules;
  37. } LOAD_DLL_INFO;
  38. /*
  39. // LOAD_DLL_READPROC:
  40. // The type of callback procedure we must support for LoadDLL(). It reads size number of bytes to
  41. // buff from the DLL data after seeking to position. It must return TRUE on success and FALSE
  42. // if the specified number of bytes could not be read. LoadDLL() will assume that the DLL file's
  43. // DOS header is at zero position. We can use this callback easily to load module from both file
  44. // or memory. The param receives the param value we pass to LoadDLL(). This is useful when you
  45. // are multithreading.
  46. */
  47. typedef BOOL (*LOAD_DLL_READPROC)(void* buff, size_t position, size_t size, void* param);
  48. /* LoadDLL() error codes: */
  49. typedef enum ELoadDLLResult
  50. {
  51. ELoadDLLResult_OK = 0,
  52. /* The read procedure we provided returned FALSE for a read request. */
  53. ELoadDLLResult_ReadProcError = 1,
  54. /* Bad DLL file. Wrong header or a similar error. */
  55. ELoadDLLResult_InvalidImage = 2,
  56. /* Memory allocation error. */
  57. ELoadDLLResult_MemoryAllocationError = 3,
  58. /* The DLL could not be loaded to the preferred imagebase and there is no base relocation info in the module. */
  59. ELoadDLLResult_RelocationError = 4,
  60. /* The DLL relocation data seems to be bad. */
  61. ELoadDLLResult_BadRelocationTable = 5,
  62. /* An imported DLL could not be loaded. */
  63. ELoadDLLResult_ImportModuleError = 6,
  64. /* A function was not found in an imported DLL. */
  65. ELoadDLLResult_ImportFunctionError = 6,
  66. /* Bad import table contents. */
  67. ELoadDLLResult_ImportTableError = 7,
  68. /* We do not support import directories that contain only bound import info. */
  69. ELoadDLLResult_BoundImportDirectoriesNotSupported = 8,
  70. /* Error setting the memory page protection of loaded and relocated sections. */
  71. ELoadDLLResult_ErrorSettingMemoryProtection = 9,
  72. /* The DllMain() returned FALSE or caused an exception. */
  73. ELoadDLLResult_DllMainCallError = 10,
  74. ELoadDLLResult_DLLFileNotFound = 11,
  75. /* LoadDLL() was called with wrong parameters */
  76. ELoadDLLResult_WrongFunctionParameters = -2,
  77. ELoadDLLResult_UnknownError = -1,
  78. } ELoadDLLResult;
  79. /* LoadDLL() flags */
  80. typedef enum ELoadDLLFlag
  81. {
  82. /* Don't call the DllMain() of the loaded DLL. */
  83. ELoadDLLFlag_NoEntryCall = 0x01,
  84. /* Don't load the DOS/PE headers, only the data/code sections. This is useful only with LoadDLL(). */
  85. ELoadDLLFlag_NoHeaders = 0x02,
  86. } ELoadDLLFlag;
  87. /*
  88. // Loads a DLL file that must be read by lpfRead() at zero position from its on stream.
  89. // @param read_proc_param: this custom parameter is passed to the read proc.
  90. // @param flags: a combination of ELoadDLLFlags.
  91. // @param info: Can be NULL. If not NULL then filled with info in case of success.
  92. */
  93. ELoadDLLResult LoadDLL(LOAD_DLL_READPROC read_proc, void* read_proc_param, int flags, LOAD_DLL_INFO* info);
  94. /*
  95. // Some LoadDLL() functions if you don't want to mess with callback functions.
  96. // They provide you the ability to load a DLL from file or from memory.
  97. // * LoadDLLFromFile:
  98. // Opens a file and loads a DLL whose data is placed in the file at
  99. // offset dwDLLOffset. The size of the DLL is dwDLLSize bytes.
  100. // * LoadDLLFromMemory:
  101. // We assume that you read the data of a DLL file to memory.
  102. // lpvDLLData points to it and dwDLLSize it its size in bytes.
  103. //
  104. // You can use the DLL_SIZE_UNK constant as dwDLLSize parameter if you
  105. // don't know the size of the DLL and you let to read any number of
  106. // bytes starting from dwDLLOffset. Only the required amount of data
  107. // will be read from the file or memory buffer not more. (Headers, sections)
  108. */
  109. #define DLL_SIZE_UNKNOWN ((DWORD)-1)
  110. ELoadDLLResult LoadDLLFromFileName(const char* filename, int flags, LOAD_DLL_INFO* info);
  111. ELoadDLLResult LoadDLLFromFileNameOffset(const char* filename, size_t dll_offset, size_t dll_size, int flags, LOAD_DLL_INFO* info);
  112. ELoadDLLResult LoadDLLFromCFile(FILE* f, size_t dll_offset, size_t dll_size, int flags, LOAD_DLL_INFO* info);
  113. ELoadDLLResult LoadDLLFromMemory(const void* dll_data, size_t dll_size, int flags, LOAD_DLL_INFO* info);
  114. BOOL UnloadDLL(LOAD_DLL_INFO* info);
  115. FARPROC myGetProcAddress_LoadDLLInfo(LOAD_DLL_INFO* info, const char* func_name);
  116. FARPROC MyGetProcAddress(HMODULE module, const char* func_name);
  117. FARPROC MyGetProcAddress_ExportDir(DWORD export_dir_rva, DWORD_PTR image_base, const char* func_name);
  118. #ifdef __cplusplus
  119. }
  120. #endif