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.

260 lines
10 KiB

  1. //
  2. // delay.cpp
  3. //
  4. // Delay load imported functions for perf.
  5. //
  6. #include "private.h"
  7. #include "ciccs.h"
  8. #include "cuitheme.h"
  9. //////////////////////////////////////////////////////////////////////////////
  10. //
  11. // Manifest Support
  12. //
  13. //////////////////////////////////////////////////////////////////////////////
  14. extern HINSTANCE g_hInst;
  15. extern CCicCriticalSectionStatic g_cs;
  16. #ifdef DEBUG
  17. extern DWORD g_dwThreadDllMain;
  18. #endif
  19. typedef HANDLE (WINAPI *PFNCREATEACTCTXA)(PCACTCTXA pActCtx);
  20. typedef VOID (WINAPI *PFNRELEASEACTCTX)( HANDLE hActCtx );
  21. typedef BOOL (WINAPI *PFNACTIVATEACTCTX)( HANDLE hActCtx, ULONG_PTR *lpCookie );
  22. typedef BOOL (WINAPI *PFNDEACTIVATEACTCTX)( DWORD dwFlags, ULONG_PTR ulCookie );
  23. static PFNCREATEACTCTXA s_pfnCreateActCtxA = NULL;
  24. static PFNRELEASEACTCTX s_pfnReleaseActCtx = NULL;
  25. static PFNACTIVATEACTCTX s_pfnActivateActCtx = NULL;
  26. static PFNDEACTIVATEACTCTX s_pfnDeactivateActCtx = NULL;
  27. static BOOL s_InitActAPI = FALSE;
  28. //+---------------------------------------------------------------------------
  29. //
  30. // InitActAPI
  31. //
  32. //+---------------------------------------------------------------------------
  33. void InitActAPI()
  34. {
  35. if (s_InitActAPI)
  36. return;
  37. HMODULE hModKernel32 = CUIGetSystemModuleHandle("kernel32.dll");
  38. if (!hModKernel32)
  39. return;
  40. s_pfnCreateActCtxA = (PFNCREATEACTCTXA)GetProcAddress(hModKernel32, "CreateActCtxA");
  41. s_pfnReleaseActCtx = (PFNRELEASEACTCTX)GetProcAddress(hModKernel32, "ReleaseActCtx");
  42. s_pfnActivateActCtx = (PFNACTIVATEACTCTX)GetProcAddress(hModKernel32, "ActivateActCtx");
  43. s_pfnDeactivateActCtx = (PFNDEACTIVATEACTCTX)GetProcAddress(hModKernel32, "DeactivateActCtx");
  44. s_InitActAPI = TRUE;
  45. }
  46. //+---------------------------------------------------------------------------
  47. //
  48. // CUICreateActCtx
  49. //
  50. //+---------------------------------------------------------------------------
  51. HANDLE CUICreateActCtx(PCACTCTXA pActCtx)
  52. {
  53. InitActAPI();
  54. if (!s_pfnCreateActCtxA)
  55. return NULL;
  56. return s_pfnCreateActCtxA(pActCtx);
  57. }
  58. //+---------------------------------------------------------------------------
  59. //
  60. // CUIReleaseActCtx
  61. //
  62. //+---------------------------------------------------------------------------
  63. VOID CUIReleaseActCtx( HANDLE hActCtx )
  64. {
  65. InitActAPI();
  66. if (!s_pfnReleaseActCtx)
  67. return;
  68. s_pfnReleaseActCtx(hActCtx);
  69. }
  70. //+---------------------------------------------------------------------------
  71. //
  72. // CUIActivateActCtx
  73. //
  74. //+---------------------------------------------------------------------------
  75. BOOL CUIActivateActCtx( HANDLE hActCtx, ULONG_PTR *lpCookie )
  76. {
  77. InitActAPI();
  78. if (!s_pfnActivateActCtx)
  79. return FALSE;
  80. return s_pfnActivateActCtx(hActCtx, lpCookie);
  81. }
  82. //+---------------------------------------------------------------------------
  83. //
  84. // CUIDeactivateActCtx
  85. //
  86. //+---------------------------------------------------------------------------
  87. BOOL CUIDeactivateActCtx( DWORD dwFlags, ULONG_PTR ulCookie )
  88. {
  89. InitActAPI();
  90. if (!s_pfnDeactivateActCtx)
  91. return FALSE;
  92. return s_pfnDeactivateActCtx(dwFlags, ulCookie);
  93. }
  94. //////////////////////////////////////////////////////////////////////////////
  95. //
  96. // DelayLoad
  97. //
  98. //////////////////////////////////////////////////////////////////////////////
  99. //+---------------------------------------------------------------------------
  100. //
  101. // CUIGetFn
  102. //
  103. //+---------------------------------------------------------------------------
  104. FARPROC CUIGetFn(HINSTANCE *phInst, TCHAR *pchLib, TCHAR *pchFunc, int nManifestResource)
  105. {
  106. Assert(g_dwThreadDllMain != GetCurrentThreadId());
  107. if (*phInst == 0)
  108. {
  109. TCHAR szPath[MAX_PATH];
  110. HANDLE hActCtx = INVALID_HANDLE_VALUE;
  111. ULONG_PTR ulCookie = 0;
  112. EnterCriticalSection(g_cs);
  113. _try {
  114. _try {
  115. if (nManifestResource > 0)
  116. {
  117. GetModuleFileName(g_hInst, szPath, sizeof(szPath));
  118. ACTCTX act = {0};
  119. act.cbSize = sizeof(act);
  120. act.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID;
  121. act.lpResourceName = MAKEINTRESOURCE(nManifestResource);
  122. act.lpSource = szPath;
  123. hActCtx = CUICreateActCtx(&act);
  124. if (hActCtx != INVALID_HANDLE_VALUE)
  125. CUIActivateActCtx(hActCtx, &ulCookie);
  126. }
  127. *phInst = LoadLibrary(pchLib);
  128. }
  129. _except(1)
  130. {
  131. Assert(0);
  132. }
  133. if (ulCookie)
  134. CUIDeactivateActCtx(0, ulCookie);
  135. if (hActCtx != INVALID_HANDLE_VALUE)
  136. CUIReleaseActCtx(hActCtx);
  137. }
  138. _except(1)
  139. {
  140. Assert(0);
  141. }
  142. LeaveCriticalSection(g_cs);
  143. }
  144. if (*phInst == 0)
  145. {
  146. //
  147. // new dlls in whistler are not in downlevels.
  148. //
  149. // Assert(0);
  150. return NULL;
  151. }
  152. return GetProcAddress(*phInst, pchFunc);
  153. }
  154. #define DELAYLOAD(_hInst, _DllName, _ManifestResource, _CallConv, _FuncName, _Args1, _Args2, _RetType, _ErrVal) \
  155. _RetType _CallConv CUI ## _FuncName ## _Args1 \
  156. { \
  157. static FARPROC pfn = NULL; \
  158. \
  159. if (pfn == NULL || _hInst == NULL) \
  160. { \
  161. pfn = CUIGetFn(&_hInst, #_DllName, #_FuncName, _ManifestResource); \
  162. \
  163. if (pfn == NULL) \
  164. { \
  165. return (_RetType) _ErrVal; \
  166. } \
  167. } \
  168. \
  169. return ((_RetType (_CallConv *)_Args1) (pfn)) _Args2; \
  170. }
  171. //
  172. // comctl32.dll
  173. //
  174. HINSTANCE g_hComctl32 = 0;
  175. #define DELAYLOADCOMCTL32(_FuncName, _Args1, _Args2, _RetType, _ErrVal) \
  176. DELAYLOAD(g_hComctl32, comctl32.dll, 1, WINAPI, _FuncName, _Args1, _Args2, _RetType, _ErrVal)
  177. DELAYLOADCOMCTL32(ImageList_Create, (int cx, int cy, UINT flags, int cInitial, int cGrow), (cx, cy, flags, cInitial, cGrow), HIMAGELIST, NULL)
  178. DELAYLOADCOMCTL32(ImageList_Destroy, (HIMAGELIST himl), (himl), BOOL, FALSE)
  179. DELAYLOADCOMCTL32(ImageList_ReplaceIcon, (HIMAGELIST himl, int i, HICON hIcon), (himl, i, hIcon), BOOL, FALSE)
  180. //
  181. // uxtheme.dll
  182. //
  183. HINSTANCE g_hUsTheme = 0;
  184. #define DELAYLOADUSTHEME(_FuncName, _Args1, _Args2, _RetType, _ErrVal) \
  185. DELAYLOAD(g_hUsTheme, uxtheme.dll, -1, WINAPI, _FuncName, _Args1, _Args2, _RetType, _ErrVal)
  186. DELAYLOADUSTHEME(IsThemeActive, (void), (), BOOL, FALSE)
  187. DELAYLOADUSTHEME(OpenThemeData, (HWND hwnd, LPCWSTR pszClassList), (hwnd, pszClassList), HTHEME, NULL)
  188. DELAYLOADUSTHEME(CloseThemeData, (HTHEME hTheme), (hTheme), HRESULT, E_FAIL)
  189. DELAYLOADUSTHEME(SetWindowTheme, (HWND hwnd, LPCWSTR pszSubAppname, LPCWSTR pszSubIdList), (hwnd, pszSubAppname, pszSubIdList), HRESULT, E_FAIL)
  190. DELAYLOADUSTHEME(DrawThemeBackground, ( HTHEME hTheme, HDC hDC, int iPartId, int iStateId, const RECT *pRect, DWORD dwBgFlags ), ( hTheme, hDC, iPartId, iStateId, pRect, dwBgFlags ), HRESULT, E_FAIL)
  191. DELAYLOADUSTHEME(DrawThemeParentBackground, ( HWND hwnd, HDC hDC, const RECT *pRect ), ( hwnd, hDC, pRect), HRESULT, E_FAIL)
  192. DELAYLOADUSTHEME(DrawThemeText, ( HTHEME hTheme, HDC hDC, int iPartId, int iStateId, LPCWSTR pszText, int iCharCount, DWORD dwTextFlags, DWORD dwTextFlags2, const RECT *pRect ), ( hTheme, hDC, iPartId, iStateId, pszText, iCharCount, dwTextFlags, dwTextFlags2, pRect ), HRESULT, E_FAIL)
  193. DELAYLOADUSTHEME(DrawThemeIcon, ( HTHEME hTheme, HDC hDC, int iPartId, int iStateId, const RECT *pRect, HIMAGELIST himl, int iImageIndex ), ( hTheme, hDC, iPartId, iStateId, pRect, himl, iImageIndex ), HRESULT, E_FAIL)
  194. DELAYLOADUSTHEME(GetThemeBackgroundExtent, ( HTHEME hTheme, HDC hDC, int iPartId, int iStateId, const RECT *pContentRect, RECT *pExtentRect ), ( hTheme, hDC, iPartId, iStateId, pContentRect, pExtentRect ), HRESULT, E_FAIL)
  195. DELAYLOADUSTHEME(GetThemeBackgroundContentRect, ( HTHEME hTheme, HDC hDC, int iPartId, int iStateId, const RECT *pBoundingRect, RECT *pContentRect ), ( hTheme, hDC, iPartId, iStateId, pBoundingRect, pContentRect ), HRESULT, E_FAIL)
  196. DELAYLOADUSTHEME(GetThemeTextExtent, ( HTHEME hTheme, HDC hdc, int iPartId, int iStateId, LPCWSTR pszText, int iCharCount, DWORD dwTextFlags, const RECT *pBoundingRect, RECT *pExtentRect ), ( hTheme, hdc, iPartId, iStateId, pszText, iCharCount, dwTextFlags, pBoundingRect, pExtentRect ), HRESULT, E_FAIL)
  197. DELAYLOADUSTHEME(GetThemePartSize, ( HTHEME hTheme, HDC hDC, int iPartId, int iStateId, RECT *prc, enum THEMESIZE eSize, SIZE *pSize ), ( hTheme, hDC, iPartId, iStateId, prc, eSize, pSize ), HRESULT, E_FAIL)
  198. DELAYLOADUSTHEME(DrawThemeEdge, (HTHEME hTheme, HDC hdc, int iPartId, int iStateId, const RECT *pDestRect, UINT uEdge, UINT uFlags, RECT *pContentRect), (hTheme, hdc, iPartId, iStateId, pDestRect, uEdge, uFlags, pContentRect), HRESULT , E_FAIL);
  199. DELAYLOADUSTHEME(GetThemeColor, (HTHEME hTheme, int iPartId, int iStateId, int iPropId, COLORREF *pColor), (hTheme, iPartId, iStateId, iPropId, pColor), HRESULT , E_FAIL);
  200. DELAYLOADUSTHEME(GetThemeMargins, (HTHEME hTheme, HDC hdc, int iPartId, int iStateId, int iPropId, RECT *prc, MARGINS *pMargins), (hTheme, hdc, iPartId, iStateId, iPropId, prc, pMargins), HRESULT , E_FAIL);
  201. DELAYLOADUSTHEME(GetThemeFont, (HTHEME hTheme, HDC hdc, int iPartId, int iStateId, int iPropId, LOGFONTW *plf), (hTheme, hdc, iPartId, iStateId, iPropId, plf), HRESULT , E_FAIL);
  202. DELAYLOADUSTHEME(GetThemeSysColor, (HTHEME hTheme, int iColorId), (hTheme, iColorId), COLORREF , 0);
  203. DELAYLOADUSTHEME(GetThemeSysSize, (HTHEME hTheme, int iSizeId), (hTheme, iSizeId), int , 0);