Leaked source code of windows server 2003
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.

164 lines
3.6 KiB

  1. // File: dllutil.cpp
  2. #include <precomp.h>
  3. #include "dllutil.h"
  4. #include "oprahcom.h"
  5. /* F C H E C K D L L V E R S I O N V E R S I O N */
  6. /*-------------------------------------------------------------------------
  7. %%Function: FCheckDllVersionVersion
  8. Make sure the dll is at least the specified version.
  9. -------------------------------------------------------------------------*/
  10. BOOL FCheckDllVersionVersion(LPCTSTR pszDll, DWORD dwMajor, DWORD dwMinor)
  11. {
  12. DLLVERSIONINFO dvi;
  13. if (FAILED(HrGetDllVersion(pszDll, &dvi)))
  14. return FALSE;
  15. if (dwMajor > dvi.dwMajorVersion)
  16. return FALSE;
  17. if (dwMajor == dvi.dwMajorVersion)
  18. {
  19. if (dwMinor > dvi.dwMinorVersion)
  20. return FALSE;
  21. }
  22. // TODO: Add Platform check (DLLVER_PLATFORM_WINDOWS vs _NT)
  23. return TRUE;
  24. }
  25. /* H R G E T D L L V E R S I O N */
  26. /*-------------------------------------------------------------------------
  27. %%Function: HrGetDllVersion
  28. Return the version information for the DLL.
  29. -------------------------------------------------------------------------*/
  30. HRESULT HrGetDllVersion(LPCTSTR pszDll, DLLVERSIONINFO * pDvi)
  31. {
  32. HRESULT hr;
  33. InitStruct(pDvi);
  34. HINSTANCE hInst = LoadLibrary(pszDll);
  35. if (NULL == hInst)
  36. {
  37. hr = E_FILE_NOT_FOUND; // file not found
  38. }
  39. else
  40. {
  41. DLLGETVERSIONPROC pDllGetVersion = (DLLGETVERSIONPROC)GetProcAddress(hInst, "DllGetVersion");
  42. if (NULL == pDllGetVersion)
  43. {
  44. hr = HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND);
  45. }
  46. else
  47. {
  48. hr = (*pDllGetVersion)(pDvi);
  49. WARNING_OUT(("Loaded %s (%d.%d.%d) %s", pszDll,
  50. pDvi->dwMajorVersion, pDvi->dwMinorVersion, pDvi->dwBuildNumber,
  51. (DLLVER_PLATFORM_NT == pDvi->dwPlatformID) ? "for NT" : "" ));
  52. }
  53. FreeLibrary(hInst);
  54. }
  55. return hr;
  56. }
  57. /* H R I N I T L P F N */
  58. /*-------------------------------------------------------------------------
  59. %%Function: HrInitLpfn
  60. Attempt to load the library and the functions declared in the table.
  61. -------------------------------------------------------------------------*/
  62. HRESULT HrInitLpfn(APIFCN *pProcList, int cProcs, HINSTANCE* phLib, LPCTSTR pszDllName)
  63. {
  64. bool bWeLoadedLibrary = false;
  65. if (NULL != pszDllName)
  66. {
  67. *phLib = LoadLibrary(pszDllName);
  68. if (NULL != *phLib)
  69. {
  70. bWeLoadedLibrary = true;
  71. }
  72. }
  73. if (NULL == *phLib)
  74. {
  75. return E_FILE_NOT_FOUND;
  76. }
  77. for (int i = 0; i < cProcs; i++)
  78. {
  79. *pProcList[i].ppfn = (LPVOID) GetProcAddress(*phLib, pProcList[i].szApiName);
  80. if (NULL == *pProcList[i].ppfn)
  81. {
  82. if (bWeLoadedLibrary)
  83. {
  84. FreeLibrary(*phLib);
  85. }
  86. return HRESULT_FROM_WIN32(ERROR_PROC_NOT_FOUND);
  87. }
  88. }
  89. return S_OK;
  90. }
  91. /* N M L O A D L I B R A R Y */
  92. /*-------------------------------------------------------------------------
  93. %%Function: NmLoadLibrary
  94. -------------------------------------------------------------------------*/
  95. HINSTANCE NmLoadLibrary(LPCTSTR pszModule, BOOL bSystemLibrary)
  96. {
  97. HINSTANCE hInstance = NULL;
  98. TCHAR szPath[MAX_PATH];
  99. if(bSystemLibrary)
  100. {
  101. // Get the system directory and account for "\0"
  102. int cbytes = GetSystemDirectory(szPath,(MAX_PATH - 2));
  103. if(cbytes == 0)
  104. {
  105. return NULL;
  106. }
  107. else
  108. {
  109. szPath[cbytes] = '\\';
  110. szPath[cbytes+1] = 0;
  111. }
  112. }
  113. else
  114. {
  115. if (!GetInstallDirectory(szPath))
  116. {
  117. return NULL;
  118. }
  119. }
  120. int cch = lstrlen(szPath);
  121. int cch2 = lstrlen(pszModule);
  122. if((cch + cch2) >=MAX_PATH)
  123. {
  124. return NULL;
  125. }
  126. lstrcpyn(szPath+cch, pszModule, CCHMAX(szPath) - cch);
  127. hInstance = ::LoadLibraryEx(szPath, NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
  128. ASSERT(hInstance);
  129. return hInstance;
  130. }