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.

135 lines
3.7 KiB

  1. /////////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright (c) 1998 Active Voice Corporation. All Rights Reserved.
  4. //
  5. // Active Agent(r) and Unified Communications(tm) are trademarks of Active Voice Corporation.
  6. //
  7. // Other brand and product names used herein are trademarks of their respective owners.
  8. //
  9. // The entire program and user interface including the structure, sequence, selection,
  10. // and arrangement of the dialog, the exclusively "yes" and "no" choices represented
  11. // by "1" and "2," and each dialog message are protected by copyrights registered in
  12. // the United States and by international treaties.
  13. //
  14. // Protected by one or more of the following United States patents: 5,070,526, 5,488,650,
  15. // 5,434,906, 5,581,604, 5,533,102, 5,568,540, 5,625,676, 5,651,054.
  16. //
  17. // Active Voice Corporation
  18. // Seattle, Washington
  19. // USA
  20. //
  21. /////////////////////////////////////////////////////////////////////////////////////////
  22. ////
  23. // loadlib.c - loadlib functions
  24. ////
  25. #include "winlocal.h"
  26. #include <stdlib.h>
  27. #include "loadlib.h"
  28. #include "file.h"
  29. #include "trace.h"
  30. ////
  31. // private definitions
  32. ////
  33. ////
  34. // helper functions
  35. ////
  36. static HINSTANCE WINAPI LoadLib(LPCTSTR lpLibFileName);
  37. ////
  38. // public functions
  39. ////
  40. // LoadLibraryPath - load specified module into address space of calling process
  41. // <lpLibFileName> (i) address of filename of executable module
  42. // <hInst> (i) module handle used to get library path
  43. // NULL use module used to create calling process
  44. // <dwFlags> (i) reserved; must be zero
  45. // return handle of loaded module (NULL if error)
  46. //
  47. // NOTE: This function behaves like the standard LoadLibrary(), except that
  48. // the first attempt to load <lpLibFileName> is made by constructing an
  49. // explicit path name, using GetModuleFileName(hInst, ...) to supply the
  50. // drive and directory, and using <lpLibFileName> to supply the file name
  51. // and extension. If the first attempt fails, LoadLibrary(lpLibFileName)
  52. // is called.
  53. //
  54. HINSTANCE DLLEXPORT WINAPI LoadLibraryPath(LPCTSTR lpLibFileName, HINSTANCE hInst, DWORD dwFlags)
  55. {
  56. BOOL fSuccess = TRUE;
  57. HINSTANCE hInstLib;
  58. TCHAR szPath[_MAX_PATH];
  59. TCHAR szDrive[_MAX_DRIVE];
  60. TCHAR szDir[_MAX_DIR];
  61. TCHAR szFname[_MAX_FNAME];
  62. TCHAR szExt[_MAX_EXT];
  63. if (GetModuleFileName(hInst, szPath, SIZEOFARRAY(szPath)) <= 0)
  64. fSuccess = TraceFALSE(NULL);
  65. else if (FileSplitPath(szPath,
  66. szDrive, szDir, NULL, NULL) != 0)
  67. fSuccess = TraceFALSE(NULL);
  68. else if (FileSplitPath(lpLibFileName,
  69. NULL, NULL, szFname, szExt) != 0)
  70. fSuccess = TraceFALSE(NULL);
  71. else if (FileMakePath(szPath,
  72. szDrive, szDir, szFname, szExt) != 0)
  73. fSuccess = TraceFALSE(NULL);
  74. // load lib using constructed path, or try
  75. // the original library name as a last resort
  76. //
  77. else if ((hInstLib = LoadLib(szPath)) == NULL &&
  78. (hInstLib = LoadLib(lpLibFileName)) == NULL)
  79. fSuccess = TraceFALSE(NULL);
  80. return fSuccess ? hInstLib : NULL;
  81. }
  82. ////
  83. // helper functions
  84. ////
  85. static HINSTANCE WINAPI LoadLib(LPCTSTR lpLibFileName)
  86. {
  87. BOOL fSuccess = TRUE;
  88. HINSTANCE hInstLib;
  89. // load the library if possible
  90. //
  91. if ((hInstLib = LoadLibrary(lpLibFileName))
  92. #ifdef _WIN32
  93. == NULL)
  94. {
  95. DWORD dwLastError = GetLastError();
  96. #else
  97. < HINSTANCE_ERROR)
  98. {
  99. DWORD dwLastError = (DWORD) (WORD) hInstLib;
  100. hInstLib = NULL;
  101. #endif
  102. fSuccess = TraceFALSE(NULL);
  103. TracePrintf_2(NULL, 5,
  104. TEXT("LoadLibrary(\"%s\") failed (%lu)\n"),
  105. (LPTSTR) lpLibFileName,
  106. (unsigned long) dwLastError);
  107. }
  108. else
  109. {
  110. TracePrintf_1(NULL, 6,
  111. TEXT("LoadLibrary(\"%s\") succeeded\n"),
  112. (LPTSTR) lpLibFileName);
  113. }
  114. return fSuccess ? hInstLib : NULL;
  115. }