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.

236 lines
5.9 KiB

  1. //
  2. // APITHK.C
  3. //
  4. // This file has API thunks that allow shlwapi to load and run on
  5. // multiple versions of NT or Win95. Since this component needs
  6. // to load on the base-level NT 4.0 and Win95, any calls to system
  7. // APIs introduced in later OS versions must be done via GetProcAddress.
  8. //
  9. // Also, any code that may need to access data structures that are
  10. // post-4.0 specific can be added here.
  11. //
  12. // NOTE: this file does *not* use the standard precompiled header,
  13. // so it can set _WIN32_WINNT to a later version.
  14. //
  15. #include "priv.h" // Don't use precompiled header here
  16. #define _USERENV_ // Disable the declspec(dllimport)
  17. #include <userenv.h> // for ExpandEnvironmentStringsForUser
  18. // the IsOS code is now centralized in shell\inc since shlwapi and those who cannot
  19. // link to shlwapi.dll both need to include it.
  20. #include <isos.c>
  21. typedef BOOL (* PFNGFAXW) (LPCWSTR, GET_FILEEX_INFO_LEVELS, LPVOID );
  22. STDAPI_(BOOL) MyGetLastWriteTime (LPCWSTR pszPath, FILETIME *pft)
  23. {
  24. if (g_bRunningOnNT5OrHigher)
  25. {
  26. static PFNGFAXW s_pfn = NULL;
  27. WIN32_FILE_ATTRIBUTE_DATA fad;
  28. if (!s_pfn)
  29. {
  30. s_pfn = (PFNGFAXW) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "GetFileAttributesExW");
  31. }
  32. ASSERT(s_pfn);
  33. if (s_pfn && s_pfn(pszPath, GetFileExInfoStandard, &fad))
  34. {
  35. *pft = fad.ftLastWriteTime;
  36. return TRUE;
  37. }
  38. }
  39. else
  40. {
  41. HANDLE hFile = CreateFileW(pszPath, GENERIC_READ, FILE_SHARE_READ,
  42. NULL, OPEN_EXISTING,
  43. FILE_ATTRIBUTE_NORMAL, 0);
  44. if (hFile != INVALID_HANDLE_VALUE)
  45. {
  46. GetFileTime(hFile, NULL, NULL, pft);
  47. CloseHandle(hFile);
  48. return TRUE;
  49. }
  50. }
  51. return FALSE;
  52. }
  53. STDAPI_(BOOL) NT5_ExpandEnvironmentStringsForUserW (HANDLE hToken, LPCWSTR lpSrc, LPWSTR lpDest, DWORD dwSize)
  54. {
  55. RIPMSG(g_bRunningOnNT5OrHigher, "Cannot invoke NT5_ExpandEnvironmentStringsForUserW when not on NT5 or higher");
  56. return(ExpandEnvironmentStringsForUserW(hToken, lpSrc, lpDest, dwSize));
  57. }
  58. typedef BOOL (* PFNSETFILEPOINTEREX) (HANDLE, LARGE_INTEGER, PLARGE_INTEGER, DWORD);
  59. STDAPI_(BOOL) NT5_SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistance, PLARGE_INTEGER pliNewPos, DWORD dwMoveMethod)
  60. {
  61. static PFNSETFILEPOINTEREX s_pfn = (PFNSETFILEPOINTEREX)-1;
  62. if (s_pfn == (PFNSETFILEPOINTEREX)-1)
  63. {
  64. s_pfn = (PFNSETFILEPOINTEREX) GetProcAddress(GetModuleHandle(TEXT("kernel32")), "SetFilePointerEx");
  65. }
  66. if (s_pfn)
  67. {
  68. return s_pfn(hFile, liDistance, pliNewPos, dwMoveMethod);
  69. }
  70. else
  71. {
  72. DWORD dwCurrent = SetFilePointer(hFile, liDistance.LowPart, NULL, dwMoveMethod);
  73. if (pliNewPos)
  74. {
  75. pliNewPos->LowPart = dwCurrent;
  76. pliNewPos->HighPart = 0;
  77. }
  78. // Because the high value is null, this return indicates failure.
  79. if (dwCurrent == INVALID_SET_FILE_POINTER)
  80. {
  81. return FALSE;
  82. }
  83. }
  84. return TRUE;
  85. }
  86. typedef LONG (* PFNREGOPENCURRENTUSER) (REGSAM, HKEY *);
  87. STDAPI_(LONG) NT5_RegOpenCurrentUser(REGSAM sam, HKEY *phk)
  88. {
  89. static PFNREGOPENCURRENTUSER s_pfn = (PFNREGOPENCURRENTUSER)-1;
  90. if (s_pfn == (PFNREGOPENCURRENTUSER)-1)
  91. {
  92. if (g_bRunningOnNT5OrHigher)
  93. s_pfn = (PFNREGOPENCURRENTUSER) GetProcAddress(GetModuleHandle(TEXT("advapi32")), "RegOpenCurrentUser");
  94. else
  95. s_pfn = NULL;
  96. }
  97. if (s_pfn)
  98. {
  99. return s_pfn(sam, phk);
  100. }
  101. else
  102. {
  103. *phk = NULL;
  104. return ERROR_CAN_NOT_COMPLETE;
  105. }
  106. }
  107. typedef BOOL (__stdcall *PFNACTCTX)(HANDLE, ULONG_PTR *);
  108. typedef BOOL (__stdcall *PFNDEACTCTX)(DWORD, ULONG_PTR );
  109. typedef HANDLE (__stdcall *PFNCREATECTX)(ACTCTX*);
  110. typedef void (__stdcall *PFNRELCTX)(HANDLE);
  111. static PFNACTCTX s_pfnAct = (PFNACTCTX)-1;
  112. static PFNDEACTCTX s_pfnDeact = (PFNDEACTCTX)-1;
  113. static PFNCREATECTX s_pfnCreateact = (PFNCREATECTX)-1;
  114. static PFNRELCTX s_pfnReleaseact = (PFNRELCTX)-1;
  115. HANDLE XP_CreateActCtx(ACTCTX* p)
  116. {
  117. if (s_pfnCreateact == (PFNCREATECTX)-1)
  118. {
  119. #ifdef UNICODE
  120. s_pfnCreateact = (PFNCREATECTX)GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "CreateActCtxW");
  121. #else
  122. s_pfnCreateact = (PFNCREATECTX)GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "CreateActCtxA");
  123. #endif
  124. }
  125. if (s_pfnCreateact)
  126. return s_pfnCreateact(p);
  127. return NULL;
  128. }
  129. void XP_ReleaseActCtx(HANDLE h)
  130. {
  131. if (s_pfnReleaseact == (PFNRELCTX)-1)
  132. {
  133. s_pfnReleaseact = (PFNRELCTX)GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "ReleaseActCtx");
  134. }
  135. if (s_pfnReleaseact)
  136. s_pfnReleaseact(h);
  137. }
  138. BOOL XP_ActivateActCtx(HANDLE h, ULONG_PTR* p)
  139. {
  140. if (s_pfnAct == (PFNACTCTX)-1)
  141. {
  142. s_pfnAct = (PFNACTCTX)GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "ActivateActCtx");
  143. }
  144. *p = 0;
  145. if (s_pfnAct)
  146. {
  147. return s_pfnAct(h, p);
  148. }
  149. return TRUE;
  150. }
  151. BOOL XP_DeactivateActCtx(ULONG_PTR p)
  152. {
  153. if (s_pfnDeact == (PFNDEACTCTX)-1)
  154. {
  155. s_pfnDeact = (PFNDEACTCTX)GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "DeactivateActCtx");
  156. }
  157. if (s_pfnDeact && p)
  158. return s_pfnDeact(0, p);
  159. return TRUE;
  160. }
  161. STDAPI_(HANDLE) XP_CreateAndActivateContext(ULONG_PTR* pul)
  162. {
  163. HANDLE hActCtx;
  164. ACTCTX act = {0};
  165. TCHAR szPath[MAX_PATH];
  166. GetModuleFileName(g_hinst, szPath, ARRAYSIZE(szPath));
  167. act.cbSize = sizeof(act);
  168. act.dwFlags = ACTCTX_FLAG_RESOURCE_NAME_VALID;
  169. act.lpResourceName = MAKEINTRESOURCE(123);
  170. act.lpSource = szPath;
  171. hActCtx = XP_CreateActCtx(&act);
  172. if (hActCtx)
  173. {
  174. XP_ActivateActCtx(hActCtx, pul);
  175. }
  176. return hActCtx;
  177. }
  178. STDAPI_(void) XP_DeactivateAndDestroyContext(HANDLE hActCtx, ULONG_PTR ul)
  179. {
  180. if (hActCtx != INVALID_HANDLE_VALUE)
  181. {
  182. if (ul != 0)
  183. XP_DeactivateActCtx(ul);
  184. XP_ReleaseActCtx(hActCtx);
  185. }
  186. }