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.

382 lines
9.0 KiB

  1. //____________________________________________________________________________
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1995 - 1996.
  5. //
  6. // File: util.cxx
  7. //
  8. // Contents:
  9. //
  10. // Classes:
  11. //
  12. // Functions:
  13. //
  14. // History: 1/8/1996 RaviR Created (Copied from BruceFo's lmshare)
  15. //
  16. //____________________________________________________________________________
  17. #include "..\pch\headers.hxx"
  18. #pragma hdrstop
  19. #include "dbg.h"
  20. #include <mstask.h> // Necessary for util.hxx
  21. #include "jobidl.hxx"
  22. #include "util.hxx"
  23. #include "resource.h"
  24. #include "commdlg.h"
  25. #include "..\inc\sadat.hxx"
  26. extern HINSTANCE g_hInstance;
  27. HRESULT
  28. GetSchSvcState(
  29. DWORD &dwCurrState);
  30. //___________________________________________________________________________
  31. //___________________________________________________________________________
  32. //___________________________________________________________________________
  33. //___________________________________________________________________________
  34. //
  35. // Menu merging routines
  36. //___________________________________________________________________________
  37. //___________________________________________________________________________
  38. //___________________________________________________________________________
  39. //___________________________________________________________________________
  40. HMENU
  41. LoadPopupMenu(
  42. HINSTANCE hinst,
  43. UINT id)
  44. {
  45. HMENU hmParent = LoadMenu(hinst, MAKEINTRESOURCE(id));
  46. if (NULL == hmParent)
  47. {
  48. return NULL;
  49. }
  50. HMENU hmPopup = GetSubMenu(hmParent, 0);
  51. RemoveMenu(hmParent, 0, MF_BYPOSITION);
  52. DestroyMenu(hmParent);
  53. return hmPopup;
  54. }
  55. HMENU
  56. UtGetMenuFromID(
  57. HMENU hmMain,
  58. UINT uID
  59. )
  60. {
  61. MENUITEMINFO mii;
  62. mii.cbSize = sizeof(mii);
  63. mii.fMask = MIIM_SUBMENU;
  64. mii.cch = 0; // just in case
  65. if (!GetMenuItemInfo(hmMain, uID, FALSE, &mii))
  66. {
  67. return NULL;
  68. }
  69. return mii.hSubMenu;
  70. }
  71. int
  72. UtMergePopupMenus(
  73. HMENU hmMain,
  74. HMENU hmMerge,
  75. int idCmdFirst,
  76. int idCmdLast)
  77. {
  78. int i;
  79. int idTemp;
  80. int idMax = idCmdFirst;
  81. for (i = GetMenuItemCount(hmMerge) - 1; i >= 0; --i)
  82. {
  83. MENUITEMINFO mii;
  84. mii.cbSize = sizeof(mii);
  85. mii.fMask = MIIM_ID | MIIM_SUBMENU;
  86. mii.cch = 0; // just in case
  87. if (!GetMenuItemInfo(hmMerge, i, TRUE, &mii))
  88. {
  89. continue;
  90. }
  91. idTemp = Shell_MergeMenus(
  92. UtGetMenuFromID(hmMain, mii.wID),
  93. mii.hSubMenu,
  94. 0,
  95. idCmdFirst,
  96. idCmdLast,
  97. MM_ADDSEPARATOR | MM_SUBMENUSHAVEIDS);
  98. if (idMax < idTemp)
  99. {
  100. idMax = idTemp;
  101. }
  102. }
  103. return idMax;
  104. }
  105. void
  106. UtMergeMenu(
  107. HINSTANCE hinst,
  108. UINT idMainMerge,
  109. UINT idPopupMerge,
  110. LPQCMINFO pqcm)
  111. {
  112. HMENU hmMerge;
  113. UINT idMax = pqcm->idCmdFirst;
  114. UINT idTemp;
  115. if (idMainMerge
  116. && (hmMerge = LoadPopupMenu(hinst, idMainMerge)) != NULL)
  117. {
  118. idMax = Shell_MergeMenus(
  119. pqcm->hmenu,
  120. hmMerge,
  121. pqcm->indexMenu,
  122. pqcm->idCmdFirst,
  123. pqcm->idCmdLast,
  124. MM_SUBMENUSHAVEIDS);
  125. DestroyMenu(hmMerge);
  126. }
  127. if (idPopupMerge
  128. && (hmMerge = LoadMenu(hinst, MAKEINTRESOURCE(idPopupMerge))) != NULL)
  129. {
  130. idTemp = UtMergePopupMenus(
  131. pqcm->hmenu,
  132. hmMerge,
  133. pqcm->idCmdFirst,
  134. pqcm->idCmdLast);
  135. if (idMax < idTemp)
  136. {
  137. idMax = idTemp;
  138. }
  139. DestroyMenu(hmMerge);
  140. }
  141. pqcm->idCmdFirst = idMax;
  142. }
  143. //+--------------------------------------------------------------------------
  144. //
  145. // Function: ContainsTemplateObject
  146. //
  147. // Synopsis: Return TRUE if [apidl] contains a CJobID object which is
  148. // marked as a template object.
  149. //
  150. // Arguments: [cidl] - number of item id lists in [apidl]
  151. // [apidl] - array of item id lists, each of which can contain
  152. // only one itemid.
  153. //
  154. // History: 5-09-1997 DavidMun Created
  155. //
  156. //---------------------------------------------------------------------------
  157. BOOL
  158. ContainsTemplateObject(
  159. UINT cidl,
  160. LPCITEMIDLIST *apidl)
  161. {
  162. UINT i;
  163. for (i = 0; i < cidl; i++)
  164. {
  165. if (JF_IsValidID(apidl[i]))
  166. {
  167. if (ID_TEMPLATE == ((PJOBID) apidl[i])->_id )
  168. {
  169. return TRUE;
  170. }
  171. }
  172. else
  173. {
  174. DEBUG_OUT((DEB_WARN, "ContainsTemplateObject: item %u not jobid\n", i));
  175. }
  176. }
  177. return FALSE;
  178. }
  179. //____________________________________________________________________________
  180. //
  181. // Function: EnsureUniquenessOfFileName
  182. //
  183. // Synopsis: Internal function. pszFile's buffer size assumed to be
  184. // greater than MAX_PATH.
  185. //
  186. // Arguments: [pszFile] -- IN, OUT
  187. //
  188. // Returns: void
  189. //
  190. // History: 2/8/1996 RaviR Created
  191. //
  192. //____________________________________________________________________________
  193. void
  194. EnsureUniquenessOfFileName(
  195. LPTSTR pszFile,
  196. size_t bufLen)
  197. {
  198. Win4Assert( NULL != pszFile );
  199. int iPostFix = 2;
  200. LPTSTR pszName = PathFindFileName(pszFile);
  201. LPTSTR pszExt = PathFindExtension(pszName);
  202. TCHAR szBufExt[10];
  203. StringCchCopy(szBufExt, 10, pszExt);
  204. int lenUpToExt = (int)(pszExt - pszFile); // lstrlen(pszFile) - lstrlen(pszExt)
  205. Win4Assert(lenUpToExt >= 0);
  206. //
  207. // Ensure uniqueness of the file
  208. //
  209. while (1)
  210. {
  211. HANDLE hFile = CreateFile(pszFile, GENERIC_READ,
  212. FILE_SHARE_READ, NULL, OPEN_EXISTING,
  213. FILE_FLAG_SEQUENTIAL_SCAN, NULL);
  214. if (hFile == INVALID_HANDLE_VALUE)
  215. {
  216. DWORD dwErr = GetLastError();
  217. if (dwErr == ERROR_FILE_NOT_FOUND)
  218. {
  219. //
  220. // No file with this name exists. So this name is unique.
  221. //
  222. break;
  223. }
  224. }
  225. else
  226. {
  227. CloseHandle(hFile);
  228. }
  229. // post fix a number to make the file name unique
  230. TCHAR szBufPostFix[10];
  231. StringCchPrintf(szBufPostFix, 10, TEXT(" %d"), iPostFix++);
  232. StringCchCopy(&pszFile[lenUpToExt], (bufLen - lenUpToExt), szBufPostFix);
  233. StringCchCat(&pszFile[lenUpToExt], (bufLen - lenUpToExt), szBufExt);
  234. }
  235. }
  236. //+--------------------------------------------------------------------------
  237. //
  238. // Function: CheckSaDat
  239. //
  240. // Synopsis: Update or create the sa.dat file if it is missing or its
  241. // waitable timer support flag disagrees with what the system
  242. // reports.
  243. //
  244. // Arguments: [tszFolderPath] - path to tasks folder
  245. //
  246. // History: 11-12-1997 DavidMun Created
  247. //
  248. // Notes: Call only with [tszFolderPath] a path on the local machine.
  249. //
  250. //---------------------------------------------------------------------------
  251. VOID
  252. CheckSaDat(
  253. LPCTSTR tszFolderPath)
  254. {
  255. HRESULT hr;
  256. DWORD dwVersion;
  257. BYTE bSvcFlags;
  258. BYTE bPlatformId;
  259. hr = SADatGetData(tszFolderPath,
  260. &dwVersion,
  261. &bPlatformId,
  262. &bSvcFlags);
  263. BOOL fNeedUpdate = FALSE;
  264. if (SUCCEEDED(hr))
  265. {
  266. BOOL fSaDatTimersFlag = bSvcFlags & SA_DAT_SVCFLAG_RESUME_TIMERS;
  267. BOOL fTimers = ResumeTimersSupported();
  268. if (fSaDatTimersFlag && !fTimers || !fSaDatTimersFlag && fTimers)
  269. {
  270. fNeedUpdate = TRUE;
  271. }
  272. }
  273. else
  274. {
  275. fNeedUpdate = TRUE;
  276. }
  277. if (fNeedUpdate)
  278. {
  279. DWORD dwState;
  280. hr = GetSchSvcState(dwState);
  281. if (SUCCEEDED(hr))
  282. {
  283. BOOL fRunning = (dwState != SERVICE_STOPPED &&
  284. dwState != SERVICE_STOP_PENDING);
  285. SADatCreate(tszFolderPath, fRunning);
  286. }
  287. }
  288. }
  289. #if DBG==1
  290. LPTSTR DbgGetTimeStr(FILETIME &ft)
  291. {
  292. SYSTEMTIME st;
  293. FileTimeToSystemTime(&ft, &st);
  294. return DbgGetTimeStr(st);
  295. }
  296. LPTSTR DbgGetTimeStr(SYSTEMTIME &st)
  297. {
  298. static WCHAR s_szTimeStamp[20]; // space for time & date in format below
  299. StringCchPrintf(s_szTimeStamp,
  300. 20,
  301. TEXT("%02d:%02d:%02d %d/%02d/%d"),
  302. st.wHour,
  303. st.wMinute,
  304. st.wSecond,
  305. st.wMonth,
  306. st.wDay,
  307. st.wYear);
  308. return s_szTimeStamp;
  309. }
  310. #endif