DllSecur.c 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* DllSecur.c -- DLL loading security
  2. 2016-10-04 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #ifdef _WIN32
  5. #include <windows.h>
  6. #include "DllSecur.h"
  7. #ifndef UNDER_CE
  8. typedef BOOL (WINAPI *Func_SetDefaultDllDirectories)(DWORD DirectoryFlags);
  9. #define MY_LOAD_LIBRARY_SEARCH_USER_DIRS 0x400
  10. #define MY_LOAD_LIBRARY_SEARCH_SYSTEM32 0x800
  11. static const char * const g_Dlls =
  12. #ifndef _CONSOLE
  13. "UXTHEME\0"
  14. #endif
  15. "USERENV\0"
  16. "SETUPAPI\0"
  17. "APPHELP\0"
  18. "PROPSYS\0"
  19. "DWMAPI\0"
  20. "CRYPTBASE\0"
  21. "OLEACC\0"
  22. "CLBCATQ\0"
  23. ;
  24. #endif
  25. void LoadSecurityDlls()
  26. {
  27. #ifndef UNDER_CE
  28. wchar_t buf[MAX_PATH + 100];
  29. {
  30. // at Vista (ver 6.0) : CoCreateInstance(CLSID_ShellLink, ...) doesn't work after SetDefaultDllDirectories() : Check it ???
  31. OSVERSIONINFO vi;
  32. vi.dwOSVersionInfoSize = sizeof(vi);
  33. if (!GetVersionEx(&vi) || vi.dwMajorVersion != 6 || vi.dwMinorVersion != 0)
  34. {
  35. Func_SetDefaultDllDirectories setDllDirs = (Func_SetDefaultDllDirectories)
  36. GetProcAddress(GetModuleHandle(TEXT("kernel32.dll")), "SetDefaultDllDirectories");
  37. if (setDllDirs)
  38. if (setDllDirs(MY_LOAD_LIBRARY_SEARCH_SYSTEM32 | MY_LOAD_LIBRARY_SEARCH_USER_DIRS))
  39. return;
  40. }
  41. }
  42. {
  43. unsigned len = GetSystemDirectoryW(buf, MAX_PATH + 2);
  44. if (len == 0 || len > MAX_PATH)
  45. return;
  46. }
  47. {
  48. const char *dll;
  49. unsigned pos = (unsigned)lstrlenW(buf);
  50. if (buf[pos - 1] != '\\')
  51. buf[pos++] = '\\';
  52. for (dll = g_Dlls; dll[0] != 0;)
  53. {
  54. unsigned k = 0;
  55. for (;;)
  56. {
  57. char c = *dll++;
  58. buf[pos + k] = c;
  59. k++;
  60. if (c == 0)
  61. break;
  62. }
  63. lstrcatW(buf, L".dll");
  64. LoadLibraryExW(buf, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  65. }
  66. }
  67. #endif
  68. }
  69. #endif