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.

326 lines
8.8 KiB

  1. //+----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1992 - 1993.
  5. //
  6. // File: misc.hxx
  7. //
  8. // Contents: Miscellaneous helper functions
  9. //
  10. // Classes: none.
  11. //
  12. // Functions: StringFromTrigger, GetDaysOfWeekString, GetExitCodeString
  13. //
  14. // History: 08-Dec-95 EricB Created.
  15. //
  16. //-----------------------------------------------------------------------------
  17. #include <job_cls.hxx>
  18. #ifndef __MISC_HXX__
  19. #define __MISC_HXX__
  20. #include <mbstring.h> // for _mbs* funcs
  21. //
  22. // Macro to determine the number of elements in an array
  23. //
  24. #define ARRAY_LEN(a) (sizeof(a)/sizeof(a[0]))
  25. //
  26. // Macro to convert Win32 errors to an HRESULT w/o overwriting the FACILITY.
  27. //
  28. #define _HRESULT_FROM_WIN32(x) \
  29. (HRESULT_FACILITY(x) ? x : HRESULT_FROM_WIN32(x))
  30. //
  31. // minFileTime, maxFileTime - min and max for FILETIMEs
  32. //
  33. inline FILETIME
  34. minFileTime(FILETIME ft1, FILETIME ft2)
  35. {
  36. if (CompareFileTime(&ft1, &ft2) < 0)
  37. {
  38. return ft1;
  39. }
  40. else
  41. {
  42. return ft2;
  43. }
  44. }
  45. inline FILETIME
  46. maxFileTime(FILETIME ft1, FILETIME ft2)
  47. {
  48. if (CompareFileTime(&ft1, &ft2) > 0)
  49. {
  50. return ft1;
  51. }
  52. else
  53. {
  54. return ft2;
  55. }
  56. }
  57. //
  58. // These functions let us use a FILETIME as an unsigned __int64 - which
  59. // it is, after all!
  60. //
  61. inline DWORDLONG
  62. FTto64(FILETIME ft)
  63. {
  64. ULARGE_INTEGER uli = { ft.dwLowDateTime, ft.dwHighDateTime };
  65. return uli.QuadPart;
  66. }
  67. inline FILETIME
  68. FTfrom64(DWORDLONG ft)
  69. {
  70. ULARGE_INTEGER uli;
  71. uli.QuadPart = ft;
  72. FILETIME ftResult = { uli.LowPart, uli.HighPart };
  73. return ftResult;
  74. }
  75. //
  76. // Absolute difference between two filetimes
  77. //
  78. inline DWORDLONG
  79. absFileTimeDiff(FILETIME ft1, FILETIME ft2)
  80. {
  81. if (CompareFileTime(&ft1, &ft2) < 0)
  82. {
  83. return (FTto64(ft2) - FTto64(ft1));
  84. }
  85. else
  86. {
  87. return (FTto64(ft1) - FTto64(ft2));
  88. }
  89. }
  90. //
  91. // GetLocalTimeAsFileTime
  92. //
  93. inline FILETIME
  94. GetLocalTimeAsFileTime()
  95. {
  96. FILETIME ftSystem;
  97. GetSystemTimeAsFileTime(&ftSystem);
  98. FILETIME ftNow;
  99. BOOL fSuccess = FileTimeToLocalFileTime(&ftSystem, &ftNow);
  100. Win4Assert(fSuccess);
  101. return ftNow;
  102. }
  103. //+---------------------------------------------------------------------------
  104. //
  105. // Function: SchedMapRpcError
  106. //
  107. // Purpose: Remap RPC exception codes that are unsuitable for displaying
  108. // to the user to more comprehensible errors.
  109. //
  110. // Arguments: [dwError] - the error returned by RpcExceptionCode().
  111. //
  112. // Returns: An HRESULT.
  113. //
  114. //----------------------------------------------------------------------------
  115. HRESULT
  116. SchedMapRpcError(DWORD dwError);
  117. //+---------------------------------------------------------------------------
  118. //
  119. // Function: ComposeErrorMsg
  120. //
  121. // Purpose: Take the two message IDs and the error code and create an
  122. // error reporting string that can be used by both service
  123. // logging and UI dialogs.
  124. //
  125. // [uErrorClassMsgID] - this indicates the class of error, such
  126. // as "Unable to start task" or "Forced to
  127. // close"
  128. // [dwErrorCode] - if non-zero, then an error from the OS
  129. // that would be expanded by FormatMessage.
  130. // [uHelpHintMsgID] - an optional suggestion as to a possible
  131. // remedy.
  132. // [fIndent] - flag indicating if the text should be
  133. // indented or not.
  134. //
  135. // Returns: A string or NULL on failure.
  136. //
  137. // Notes: Release the string memory when done using LocalFree.
  138. //----------------------------------------------------------------------------
  139. LPTSTR
  140. ComposeErrorMsg(
  141. UINT uErrorClassMsgID,
  142. DWORD dwErrCode,
  143. UINT uHelpHintMsgID = 0,
  144. BOOL fIndent = TRUE);
  145. //+---------------------------------------------------------------------------
  146. //
  147. // Function: GetExitCodeString
  148. //
  149. // Synopsis: Retrieve the string associated with the exit code from a
  150. // message file. Algorithm:
  151. //
  152. // Consult the Software\Microsoft\Job Scheduler subkey.
  153. //
  154. // Attempt to retrieve the ExitCodeMessageFile string value from
  155. // a subkey matching the job executable prefix (i.e., minus the
  156. // extension).
  157. //
  158. // The ExitCodeMessageFile specifies a binary from which the
  159. // message string associated with the exit code value is fetched.
  160. //
  161. // Arguments: [dwExitCode] -- Job exit code.
  162. // [ptszExitCodeValue] -- Job exit code in string form.
  163. // [ptszJobExecutable] -- Binary name executed with the job.
  164. //
  165. // Returns: TCHAR * exit code string
  166. // NULL on error
  167. //
  168. // Notes: FormatMessage allocates the return string. Use LocalFree() to
  169. // deallocate.
  170. //
  171. //----------------------------------------------------------------------------
  172. TCHAR *
  173. GetExitCodeString(DWORD dwExitCode,
  174. TCHAR * ptszExitCodeValue,
  175. TCHAR * ptszJobExecutable);
  176. //+---------------------------------------------------------------------------
  177. //
  178. // Function: ComposeErrorMessage
  179. //
  180. // Synopsis:
  181. //
  182. // Returns: HRESULTs
  183. //
  184. // Notes: FormatMessage allocates the return string. Use LocalFree() to
  185. // deallocate.
  186. //
  187. //----------------------------------------------------------------------------
  188. HRESULT
  189. ComposeErrorMessage(UINT uErrMsgID,
  190. HRESULT hrFailureCode,
  191. UINT uSuggestionID);
  192. SC_HANDLE
  193. OpenScheduleService(DWORD dwDesiredAccess);
  194. #define s_isDriveLetter(c) ((c >= TEXT('a') && c <= TEXT('z')) || \
  195. (c >= TEXT('A') && c <= TEXT('Z')))
  196. //+----------------------------------------------------------------------------
  197. //
  198. // Function: HasSpaces
  199. //
  200. // Synopsis: Scans the string for space characters.
  201. //
  202. // Arguments: [ptstr] - the string to scan
  203. //
  204. // Returns: TRUE if any space characters are found.
  205. //
  206. //-----------------------------------------------------------------------------
  207. inline BOOL
  208. HasSpacesA(LPCSTR pstr)
  209. {
  210. return _mbschr((PUCHAR) pstr, ' ') != NULL;
  211. }
  212. inline BOOL
  213. HasSpacesW(LPCWSTR pwstr)
  214. {
  215. return wcschr(pwstr, L' ') != NULL;
  216. }
  217. #define HasSpaces HasSpacesW
  218. //+----------------------------------------------------------------------------
  219. //
  220. // Function: StringFromTrigger
  221. //
  222. // Synopsis: Returns the string representation of the passed in trigger
  223. // data structure.
  224. //
  225. // Arguments: [pTrigger] - the TASK_TRIGGER struct
  226. // [ppwszTrigger] - the returned string
  227. // [lpDetails] - the SHELLDETAILS struct
  228. //
  229. // Returns: HRESULTS
  230. //
  231. // Notes: The string is allocated by this function with CoTaskMemAlloc.
  232. //-----------------------------------------------------------------------------
  233. HRESULT
  234. StringFromTrigger(const PTASK_TRIGGER pTrigger, LPWSTR * ppwszTrigger, LPSHELLDETAILS lpDetails);
  235. HRESULT GetDaysOfWeekString(WORD rgfDaysOfTheWeek, LPTSTR ptszBuf, UINT cchBuf);
  236. //+---------------------------------------------------------------------------
  237. //
  238. // Function: GetParentDirectory
  239. //
  240. // Synopsis: Return the parent directory of the path indicated.
  241. //
  242. // Arguments: [ptszPath] -- Input path.
  243. // [tszDirectory] -- Caller-allocated returned directory.
  244. // [cchBuf] -- size of caller-allocated buffer
  245. //
  246. // Returns: None.
  247. //
  248. // Notes: None.
  249. //
  250. //----------------------------------------------------------------------------
  251. void
  252. GetParentDirectory(LPCTSTR ptszPath, TCHAR tszDirectory[], size_t cchBuf);
  253. VOID
  254. GetAppNameFromPath(
  255. LPCTSTR tszAppPathName,
  256. LPTSTR tszCopyTo,
  257. ULONG cchMax);
  258. BOOL SetAppPath(LPCTSTR tszAppPathName, LPTSTR *pptszSavedPath);
  259. BOOL
  260. IsThreadCallerAnAdmin(HANDLE hThreadToken);
  261. HRESULT
  262. LoadAtJob(CJob * pJob, TCHAR * ptszAtJobFilename);
  263. BOOL IsValidAtFilename(LPCWSTR wszFilename);
  264. //
  265. // Enum and function for translating between internal and display
  266. // representations of an account. Currently used only for Local System.
  267. //
  268. typedef enum _TRANSLATION_DIRECTION {
  269. TRANSLATE_FOR_DISPLAY,
  270. TRANSLATE_FOR_INTERNAL
  271. } TRANSLATION_DIRECTION;
  272. HRESULT
  273. TranslateAccount(
  274. TRANSLATION_DIRECTION tdDirection,
  275. LPCWSTR pwszAccountIn,
  276. LPWSTR pwszAccountOut,
  277. DWORD cchAccountOut,
  278. LPWSTR* ppwszPassword = NULL);
  279. HRESULT
  280. OpenFileWithRetry(
  281. LPCTSTR ptszFileName,
  282. DWORD dwDesiredAccess,
  283. DWORD dwDesiredShareMode,
  284. HANDLE* phFile);
  285. #endif // __MISC_HXX__