Source code of Windows XP (NT5)
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.

87 lines
2.0 KiB

  1. /******************************************************************************
  2. Copyright (c) 2002 Microsoft Corporation
  3. Module Name:
  4. safelib.cpp
  5. Abstract:
  6. Implements LoadLibraryFromSystemDir function
  7. ******************************************************************************/
  8. #include "stdafx.h"
  9. // **************************************************************************
  10. static
  11. BOOL UseFullPath(void)
  12. {
  13. static BOOL s_fUseFullPath = TRUE;
  14. static BOOL s_fInit = FALSE;
  15. OSVERSIONINFO osvi;
  16. if (s_fInit)
  17. return s_fUseFullPath;
  18. ZeroMemory(&osvi, sizeof(osvi));
  19. osvi.dwOSVersionInfoSize = sizeof(osvi);
  20. if (GetVersionEx(&osvi))
  21. {
  22. if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  23. (osvi.dwMajorVersion > 5 ||
  24. (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion >= 1)))
  25. {
  26. s_fUseFullPath = FALSE;
  27. }
  28. s_fInit = TRUE;
  29. }
  30. return s_fUseFullPath;
  31. }
  32. // **************************************************************************
  33. HMODULE WINAPI LoadLibraryFromSystemDir(LPCTSTR szModule)
  34. {
  35. HRESULT hr = NOERROR;
  36. HMODULE hmod = NULL;
  37. TCHAR szModulePath[MAX_PATH + 1];
  38. if (szModule == NULL)
  39. {
  40. SetLastError(ERROR_INVALID_PARAMETER);
  41. goto done;
  42. }
  43. if (UseFullPath())
  44. {
  45. DWORD cch;
  46. // if the function call fails, make the buffer an empty string, so
  47. // we will just use the dll name in the append below.
  48. cch = GetSystemDirectory(szModulePath, ARRAYSIZE(szModulePath));
  49. if (cch == 0 || cch >= ARRAYSIZE(szModulePath))
  50. szModulePath[0] = _T('\0');
  51. }
  52. else
  53. {
  54. szModulePath[0] = _T('\0');
  55. }
  56. hr = PathCchAppend(szModulePath, ARRAYSIZE(szModulePath), szModule);
  57. if (FAILED(hr))
  58. {
  59. SetLastError(HRESULT_CODE(hr));
  60. goto done;
  61. }
  62. hmod = LoadLibraryEx(szModulePath, NULL, 0);
  63. if (hmod == NULL)
  64. goto done;
  65. done:
  66. return hmod;
  67. }