MemoryModule.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * Memory DLL loading code
  3. * Version 0.0.4
  4. *
  5. * Copyright (c) 2004-2015 by Joachim Bauch / mail@joachim-bauch.de
  6. * http://www.joachim-bauch.de
  7. *
  8. * The contents of this file are subject to the Mozilla Public License Version
  9. * 2.0 (the "License"); you may not use this file except in compliance with
  10. * the License. You may obtain a copy of the License at
  11. * http://www.mozilla.org/MPL/
  12. *
  13. * Software distributed under the License is distributed on an "AS IS" basis,
  14. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  15. * for the specific language governing rights and limitations under the
  16. * License.
  17. *
  18. * The Original Code is MemoryModule.h
  19. *
  20. * The Initial Developer of the Original Code is Joachim Bauch.
  21. *
  22. * Portions created by Joachim Bauch are Copyright (C) 2004-2015
  23. * Joachim Bauch. All Rights Reserved.
  24. *
  25. */
  26. #ifndef __MEMORY_MODULE_HEADER
  27. #define __MEMORY_MODULE_HEADER
  28. #include <windows.h>
  29. typedef void *HMEMORYMODULE;
  30. typedef void *HMEMORYRSRC;
  31. typedef void *HCUSTOMMODULE;
  32. #ifdef __cplusplus
  33. extern "C" {
  34. #endif
  35. typedef HCUSTOMMODULE (*CustomLoadLibraryFunc)(LPCSTR, void *);
  36. typedef FARPROC (*CustomGetProcAddressFunc)(HCUSTOMMODULE, LPCSTR, void *);
  37. typedef void (*CustomFreeLibraryFunc)(HCUSTOMMODULE, void *);
  38. /**
  39. * Load EXE/DLL from memory location with the given size.
  40. *
  41. * All dependencies are resolved using default LoadLibrary/GetProcAddress
  42. * calls through the Windows API.
  43. */
  44. HMEMORYMODULE MemoryLoadLibrary(const void *, size_t);
  45. /**
  46. * Load EXE/DLL from memory location with the given size using custom dependency
  47. * resolvers.
  48. *
  49. * Dependencies will be resolved using passed callback methods.
  50. */
  51. HMEMORYMODULE MemoryLoadLibraryEx(const void *, size_t,
  52. CustomLoadLibraryFunc,
  53. CustomGetProcAddressFunc,
  54. CustomFreeLibraryFunc,
  55. void *);
  56. /**
  57. * Get address of exported method. Supports loading both by name and by
  58. * ordinal value.
  59. */
  60. FARPROC MemoryGetProcAddress(HMEMORYMODULE, LPCSTR);
  61. /**
  62. * Free previously loaded EXE/DLL.
  63. */
  64. void MemoryFreeLibrary(HMEMORYMODULE);
  65. /**
  66. * Execute entry point (EXE only). The entry point can only be executed
  67. * if the EXE has been loaded to the correct base address or it could
  68. * be relocated (i.e. relocation information have not been stripped by
  69. * the linker).
  70. *
  71. * Important: calling this function will not return, i.e. once the loaded
  72. * EXE finished running, the process will terminate.
  73. *
  74. * Returns a negative value if the entry point could not be executed.
  75. */
  76. int MemoryCallEntryPoint(HMEMORYMODULE);
  77. /**
  78. * Find the location of a resource with the specified type and name.
  79. */
  80. HMEMORYRSRC MemoryFindResource(HMEMORYMODULE, LPCTSTR, LPCTSTR);
  81. /**
  82. * Find the location of a resource with the specified type, name and language.
  83. */
  84. HMEMORYRSRC MemoryFindResourceEx(HMEMORYMODULE, LPCTSTR, LPCTSTR, WORD);
  85. /**
  86. * Get the size of the resource in bytes.
  87. */
  88. DWORD MemorySizeofResource(HMEMORYMODULE, HMEMORYRSRC);
  89. /**
  90. * Get a pointer to the contents of the resource.
  91. */
  92. LPVOID MemoryLoadResource(HMEMORYMODULE, HMEMORYRSRC);
  93. /**
  94. * Load a string resource.
  95. */
  96. int MemoryLoadString(HMEMORYMODULE, UINT, LPTSTR, int);
  97. /**
  98. * Load a string resource with a given language.
  99. */
  100. int MemoryLoadStringEx(HMEMORYMODULE, UINT, LPTSTR, int, WORD);
  101. /**
  102. * Default implementation of CustomLoadLibraryFunc that calls LoadLibraryA
  103. * internally to load an additional libary.
  104. *
  105. * This is the default as used by MemoryLoadLibrary.
  106. */
  107. HCUSTOMMODULE MemoryDefaultLoadLibrary(LPCSTR, void *);
  108. /**
  109. * Default implementation of CustomGetProcAddressFunc that calls GetProcAddress
  110. * internally to get the address of an exported function.
  111. *
  112. * This is the default as used by MemoryLoadLibrary.
  113. */
  114. FARPROC MemoryDefaultGetProcAddress(HCUSTOMMODULE, LPCSTR, void *);
  115. /**
  116. * Default implementation of CustomFreeLibraryFunc that calls FreeLibrary
  117. * internally to release an additional libary.
  118. *
  119. * This is the default as used by MemoryLoadLibrary.
  120. */
  121. void MemoryDefaultFreeLibrary(HCUSTOMMODULE, void *);
  122. #ifdef __cplusplus
  123. }
  124. #endif
  125. #endif // __MEMORY_MODULE_HEADER