You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

74 lines
2.3 KiB

  1. /*
  2. * Proxy.h -- Definitions for proxy-related functionality
  3. *
  4. * The proxy works roughly as follows:
  5. * - We define our exports in proxy.c (computer generated)
  6. * - loadProxy initializes the proxy:
  7. * 1. Look up the name of this DLL
  8. * 2. Find the original DLL with the same name
  9. * 3. Load the original DLL
  10. * 4. Load all functions into originalFunctions array
  11. *
  12. * For more information, refer to proxy.c
  13. */
  14. #pragma once
  15. #pragma warning( disable : 4267 6387 6386 )
  16. #include <Windows.h>
  17. #include <Shlwapi.h>
  18. #include "assert_util.h"
  19. #include <crtdbg.h>
  20. #define ALT_POSTFIX L"_alt.dll"
  21. #define DLL_POSTFIX L".dll"
  22. extern FARPROC originalFunctions[];
  23. extern void loadFunctions(HMODULE dll);
  24. // Load the proxy functions into memory
  25. inline void loadProxy(wchar_t *moduleName)
  26. {
  27. size_t module_name_len = wcslen(moduleName);
  28. size_t alt_name_len = module_name_len + STR_LEN(ALT_POSTFIX);
  29. wchar_t *alt_name = memalloc(sizeof(wchar_t) * alt_name_len);
  30. wmemcpy(alt_name, moduleName, module_name_len);
  31. wmemcpy(alt_name + module_name_len, ALT_POSTFIX, STR_LEN(ALT_POSTFIX));
  32. wchar_t *dll_path = NULL; // The final DLL path
  33. const int alt_full_path_len = GetFullPathNameW(alt_name, 0, NULL, NULL);
  34. wchar_t *alt_full_path = memalloc(sizeof(wchar_t) * alt_full_path_len);
  35. GetFullPathNameW(alt_name, alt_full_path_len, alt_full_path, NULL);
  36. memfree(alt_name);
  37. LOG("Looking for original DLL from %S\n", alt_full_path);
  38. // Try to look for the alternative first in the same directory.
  39. HMODULE handle = LoadLibrary(alt_full_path);
  40. if (handle == NULL)
  41. {
  42. size_t system_dir_len = GetSystemDirectoryW(NULL, 0);
  43. dll_path = memalloc(sizeof(wchar_t) * (system_dir_len + module_name_len + STR_LEN(DLL_POSTFIX)));
  44. _ASSERTE(dll_path != nullptr);
  45. GetSystemDirectoryW(dll_path, system_dir_len);
  46. dll_path[system_dir_len - 1] = L'\\';
  47. wmemcpy(dll_path + system_dir_len, moduleName, module_name_len);
  48. wmemcpy(dll_path + system_dir_len + module_name_len, DLL_POSTFIX, STR_LEN(DLL_POSTFIX));
  49. LOG("Looking for original DLL from %S\n", dll_path);
  50. handle = LoadLibraryW(dll_path);
  51. }
  52. ASSERT_F(handle != NULL, L"Unable to load the original %s.dll (looked from system directory and from %s_alt.dll)!",
  53. moduleName, moduleName);
  54. memfree(alt_full_path);
  55. memfree(dll_path);
  56. loadFunctions(handle);
  57. }