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.

239 lines
7.4 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: dynload.cxx
  7. //
  8. // Contents: APIs from dynamically loaded system dlls. These APIs
  9. // are rarely used and there are only 1 or 2 per system
  10. // Dll so we dynamically load the Dll so that we improve
  11. // the load time of OLE32.DLL
  12. //
  13. // Functions: OleWNetGetConnection
  14. // OleWNetGetUniversalName
  15. // OleExtractIcon
  16. // OleGetShellLink
  17. // OleSymInitialize
  18. // OleSymCleanup
  19. // OleSymGetSymFromAddr
  20. // OleSymUnDName
  21. //
  22. // History: 10-Jan-95 Rickhi Created
  23. // 10-Mar-95 BillMo Added OleGetShellLink-creates a shortcut object.
  24. // 12-Jul-95 t-stevan Added OleSym* routines
  25. // 22-Nov-95 MikeHill Use Unicode IShellLink object in NT.
  26. //
  27. //--------------------------------------------------------------------------
  28. #include <windows.h>
  29. #include <shellapi.h>
  30. #include <imagehlp.h>
  31. #include <ole2sp.h>
  32. #include <ole2com.h>
  33. // Entry Points from IMAGEHLP.DLL
  34. HINSTANCE hInstIMAGEHLP = NULL;
  35. typedef BOOL (*PFN_SYMINITIALIZE)(HANDLE hProcess, LPSTR UserSearchPath,
  36. BOOL fInvadeProcess);
  37. PFN_SYMINITIALIZE pfnSymInitialize = NULL;
  38. #define SYMINITIALIZE_NAME "SymInitialize"
  39. typedef BOOL (*PFN_SYMCLEANUP)(HANDLE hProcess);
  40. PFN_SYMCLEANUP pfnSymCleanup = NULL;
  41. #define SYMCLEANUP_NAME "SymCleanup"
  42. typedef BOOL (*PFN_SYMGETSYMFROMADDR)(HANDLE hProcess,
  43. DWORD64 dwAddr, PDWORD64 pdwDisplacement, PIMAGEHLP_SYMBOL64 pSym);
  44. PFN_SYMGETSYMFROMADDR pfnSymGetSymFromAddr64 = NULL;
  45. #define SYMGETSYMFROMADDR_NAME "SymGetSymFromAddr64"
  46. typedef BOOL (*PFN_SYMUNDNAME)(PIMAGEHLP_SYMBOL64 sym, LPSTR lpname, DWORD dwmaxLength);
  47. PFN_SYMUNDNAME pfnSymUnDName64 = NULL;
  48. #define SYMUNDNAME_NAME "SymUnDName64"
  49. //+---------------------------------------------------------------------------
  50. //
  51. // Function: LoadSystemProc
  52. //
  53. // Synopsis: Loads the specified DLL if necessary and finds the specified
  54. // entry point.
  55. //
  56. // Returns: 0: the entry point function ptr is valid
  57. // !0: the entry point function ptr is not valid
  58. //
  59. // History: 10-Jan-95 Rickhi Created
  60. //
  61. //----------------------------------------------------------------------------
  62. BOOL LoadSystemProc(LPSTR szDll, LPCSTR szProc,
  63. HINSTANCE *phInst, FARPROC *ppfnProc)
  64. {
  65. if (*phInst == NULL)
  66. {
  67. // Dll not loaded yet, load it now.
  68. if ((*phInst = LoadLibraryA(szDll)) == NULL)
  69. return GetLastError();
  70. }
  71. // load the entry point
  72. if ((*ppfnProc = GetProcAddress(*phInst, szProc)) == NULL)
  73. return GetLastError();
  74. return 0;
  75. }
  76. //+---------------------------------------------------------------------------
  77. //
  78. // Function: FreeSystemDLLs
  79. //
  80. // Synopsis: Frees any system Dlls that we dynamically loaded.
  81. //
  82. // History: 10-Jan-95 Rickhi Created
  83. //
  84. //----------------------------------------------------------------------------
  85. void FreeSystemDLLs()
  86. {
  87. if(hInstIMAGEHLP != NULL && hInstIMAGEHLP != INVALID_HANDLE_VALUE)
  88. {
  89. FreeLibrary(hInstIMAGEHLP);
  90. }
  91. }
  92. //+---------------------------------------------------------------------------
  93. //
  94. // Function: OleSymInitialize
  95. //
  96. // Synopsis: OLE internal implementation of SymInitialize
  97. //
  98. // History: 11-Jul-95 t-stevan Created
  99. //
  100. //----------------------------------------------------------------------------
  101. BOOL OleSymInitialize(HANDLE hProcess, LPSTR UserSearchPath,
  102. BOOL fInvadeProcess)
  103. {
  104. if(hInstIMAGEHLP == (HINSTANCE) -1)
  105. {
  106. // we already tried loading the DLL, give up
  107. return FALSE;
  108. }
  109. if (pfnSymInitialize == NULL)
  110. {
  111. DWORD rc;
  112. rc = LoadSystemProc("IMAGEHLP.DLL", SYMINITIALIZE_NAME,
  113. &hInstIMAGEHLP, (FARPROC *)&pfnSymInitialize);
  114. if (rc != 0)
  115. {
  116. hInstIMAGEHLP = (HINSTANCE) -1;
  117. return FALSE;
  118. }
  119. }
  120. return (pfnSymInitialize)(hProcess, UserSearchPath, fInvadeProcess);
  121. }
  122. //+---------------------------------------------------------------------------
  123. //
  124. // Function: OleSymCleanup
  125. //
  126. // Synopsis: OLE internal implementation of SymCleanup
  127. //
  128. // History: 11-Jul-95 t-stevan Created
  129. //
  130. //----------------------------------------------------------------------------
  131. BOOL OleSymCleanup(HANDLE hProcess)
  132. {
  133. if(hInstIMAGEHLP == (HINSTANCE) -1)
  134. {
  135. // we already tried loading the DLL, give up
  136. return FALSE;
  137. }
  138. if (pfnSymCleanup == NULL)
  139. {
  140. DWORD rc;
  141. rc = LoadSystemProc("IMAGEHLP.DLL", SYMCLEANUP_NAME,
  142. &hInstIMAGEHLP, (FARPROC *)&pfnSymCleanup);
  143. if (rc != 0)
  144. {
  145. hInstIMAGEHLP = (HINSTANCE) -1;
  146. return FALSE;
  147. }
  148. }
  149. return (pfnSymCleanup)(hProcess);
  150. }
  151. //+---------------------------------------------------------------------------
  152. //
  153. // Function: OleSymGetSymFromAddr
  154. //
  155. // Synopsis: OLE internal implementation of SymGetSymFromAddr
  156. //
  157. // History: 11-Jul-95 t-stevan Created
  158. //
  159. //----------------------------------------------------------------------------
  160. BOOL OleSymGetSymFromAddr(HANDLE hProcess, DWORD64 dwAddr, PDWORD64 pdwDisplacement, PIMAGEHLP_SYMBOL64 pSym)
  161. {
  162. if(hInstIMAGEHLP == (HINSTANCE) -1)
  163. {
  164. // we already tried loading the DLL, give up
  165. return NULL;
  166. }
  167. if (pfnSymGetSymFromAddr64 == NULL)
  168. {
  169. DWORD rc;
  170. rc = LoadSystemProc("IMAGEHLP.DLL", SYMGETSYMFROMADDR_NAME,
  171. &hInstIMAGEHLP, (FARPROC *)&pfnSymGetSymFromAddr64);
  172. if (rc != 0)
  173. {
  174. hInstIMAGEHLP = (HINSTANCE) -1;
  175. return NULL;
  176. }
  177. }
  178. return (pfnSymGetSymFromAddr64)(hProcess, dwAddr, pdwDisplacement, pSym);
  179. }
  180. //+---------------------------------------------------------------------------
  181. //
  182. // Function: OleSymUnDName
  183. //
  184. // Synopsis: OLE internal implementation of SymUnDName
  185. //
  186. // History: 11-Jul-95 t-stevan Created
  187. //
  188. //----------------------------------------------------------------------------
  189. BOOL OleSymUnDName(PIMAGEHLP_SYMBOL64 pSym, LPSTR lpname, DWORD dwmaxLength)
  190. {
  191. if(hInstIMAGEHLP == (HINSTANCE) -1)
  192. {
  193. // we already tried loading the DLL, give up
  194. return FALSE;
  195. }
  196. if (pfnSymUnDName64 == NULL)
  197. {
  198. DWORD rc;
  199. rc = LoadSystemProc("IMAGEHLP.DLL", SYMUNDNAME_NAME,
  200. &hInstIMAGEHLP, (FARPROC *)&pfnSymUnDName64);
  201. if (rc != 0)
  202. {
  203. hInstIMAGEHLP = (HINSTANCE) -1;
  204. return FALSE;
  205. }
  206. }
  207. return (pfnSymUnDName64)(pSym, lpname, dwmaxLength);
  208. }