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.

266 lines
7.0 KiB

  1. /****************************************************************************************
  2. * NAME: TimeOfDay.cpp
  3. *
  4. * OVERVIEW
  5. *
  6. * APIs for getting the Time of Day constraint
  7. *
  8. * Copyright (C) Microsoft Corporation, 1998 - 1999 . All Rights Reserved.
  9. *
  10. * History:
  11. * 2/14/98 Created by Byao
  12. *
  13. *****************************************************************************************/
  14. #include "precompiled.h"
  15. //
  16. // declarations for IAS mapping APIs
  17. //
  18. #include "textmap.h"
  19. //
  20. // declaration for the API
  21. #include "TimeOfDay.h"
  22. #include "iasdebug.h"
  23. static BYTE bitSetting[8] = { 0x80, 0x40, 0x20, 0x10, 0x8, 0x4, 0x2, 0x1};
  24. //+---------------------------------------------------------------------------
  25. //
  26. // Function: ReverseHourMap
  27. //
  28. // Synopsis: reverse each byte in the hour map
  29. //
  30. // we have to do this because LogonUI changes the way HourMap is stored(they
  31. // reversed all the bit. We need to do this so our conversion code can leave
  32. // intact.
  33. //
  34. // Arguments: [in] BYTE* map - hour map
  35. // [in] INT nByte - how many bytes are in this hour map
  36. //
  37. // History: byao 4/10/98 10:33:57 PM
  38. //
  39. //+---------------------------------------------------------------------------
  40. void ReverseHourMap(BYTE *map, int nByte)
  41. {
  42. int i, j;
  43. BYTE temp;
  44. for (i=0; i<nByte; i++)
  45. {
  46. temp = 0;
  47. for (j=0;j<8;j++)
  48. {
  49. // set the value temp
  50. if ( map[i] & bitSetting[j] )
  51. {
  52. temp |= bitSetting[7-j];
  53. }
  54. }
  55. map[i] = temp;
  56. }
  57. }
  58. //+---------------------------------------------------------------------------
  59. //
  60. // Function: HourMapToStr
  61. //
  62. // Synopsis: convert the 21-byte hour map to a text string in the following
  63. // format:
  64. // 0 8:30-9:30 10:30-15:30; 2 10:00-14:00
  65. //
  66. //
  67. // Arguments: [in] BYTE* map - hour map
  68. // [out] ATL::CString& strHourMap - hour map in string format
  69. //
  70. // History: Created Header byao 2/14/98 10:33:57 PM
  71. //
  72. //+---------------------------------------------------------------------------
  73. void HourMapToStr(BYTE* map, ATL::CString& strHourMap)
  74. {
  75. int sh, eh; // start hour, (min), end hour (min)
  76. BYTE* pHourMap;
  77. int i, j;
  78. //
  79. // todo: change to a safer allocation method
  80. //
  81. WCHAR wzTmpStr[128] = L"";
  82. WCHAR wzStr[2048] = L"";
  83. WCHAR wzHourStr[8192] = L"";
  84. BOOL bFirstDay=TRUE;
  85. pHourMap = map;
  86. for( i = 0; i < 7; i++) // for each day
  87. {
  88. // if any value for this day
  89. if(*pHourMap || *(pHourMap + 1) || *(pHourMap + 2))
  90. {
  91. // the string for this day
  92. if (bFirstDay)
  93. {
  94. wsprintf(wzTmpStr, _T("%1d"), i);
  95. bFirstDay = FALSE;
  96. }
  97. else
  98. {
  99. wsprintf(wzTmpStr, _T("; %1d"), i);
  100. }
  101. wcscpy(wzStr, wzTmpStr);
  102. sh = -1; eh = -1; // not start yet
  103. for(j = 0; j < 24; j++) // for every hour
  104. {
  105. int k = j / 8;
  106. int m = j % 8;
  107. if(*(pHourMap + k) & bitSetting[m]) // this hour is on
  108. {
  109. if(sh == -1) sh = j; // set start hour is empty
  110. eh = j; // extend end hour
  111. }
  112. else // this is not on
  113. {
  114. if(sh != -1) // some hours need to write out
  115. {
  116. wsprintf(wzTmpStr,_T(" %02d:00-%02d:00"), sh, eh + 1);
  117. wcscat(wzStr, wzTmpStr);
  118. sh = -1; eh = -1;
  119. }
  120. }
  121. }
  122. if(sh != -1)
  123. {
  124. wsprintf(wzTmpStr, _T(" %02d:00-%02d:00"), sh, eh + 1);
  125. wcscat(wzStr, wzTmpStr);
  126. sh = -1; eh = -1;
  127. }
  128. wcscat(wzHourStr, wzStr);
  129. }
  130. pHourMap += 3;
  131. }
  132. strHourMap = wzHourStr;
  133. return;
  134. }
  135. //+---------------------------------------------------------------------------
  136. //
  137. // Function: GetTODConstaint
  138. //
  139. // Synopsis: Get the time of day constraint
  140. // This is implemented by calling an API in NT team
  141. // LogonScheduleDialog(...);
  142. //
  143. // Arguments: [in/out] ATL::CString &strHourMap - TOD constraint in text format
  144. //
  145. // Returns: HRESULT -
  146. //
  147. // History: Created Header byao 2/14/98 10:36:27 PM
  148. //
  149. //+---------------------------------------------------------------------------
  150. typedef HRESULT (APIENTRY *PFN_LOGONSCHEDULEDIALOGEX)(
  151. HWND hwndParent // parent window
  152. , BYTE ** pprgbData // pointer to pointer to array of 21 bytes
  153. , LPCTSTR pszTitle // dialog title
  154. , DWORD dwFlags // in what format to accept the time
  155. );
  156. HRESULT GetTODConstaint( ATL::CString &strHourMap )
  157. {
  158. TRACE_FUNCTION("::GetTODConstraint");
  159. BYTE TimeOfDayHoursMap[21];
  160. BYTE* pMap = &(TimeOfDayHoursMap[0]);
  161. ATL::CString strDialogTitle;
  162. DWORD dwRet;
  163. HRESULT hr = S_OK;
  164. PFN_LOGONSCHEDULEDIALOGEX pfnLogonScheduleDialogEx = NULL;
  165. HMODULE hLogonScheduleDLL = NULL;
  166. ZeroMemory(TimeOfDayHoursMap, 21);
  167. //
  168. // convert the TOD constraint string to HourMap
  169. //
  170. dwRet = IASHourMapFromText(strHourMap, pMap);
  171. if (NO_ERROR != dwRet)
  172. goto win32_error;
  173. // ReverseHourMap() will reverse each byte of the hour map, basically
  174. // reverse every bit in the byte.
  175. // we have to do this because LogonUI changes the way HourMap is stored(they
  176. // reversed all the bit. We need to do this so our conversion code can leave
  177. // intact.
  178. //
  179. // We reverse it here so it can be understood by the LogonSchedule api
  180. //
  181. ReverseHourMap(pMap,21);
  182. //
  183. // get the new TOD Constaint
  184. //
  185. if (!strDialogTitle.LoadString(IDS_TOD_DIALOG_TITLE))
  186. goto win32_error;
  187. hLogonScheduleDLL = LoadLibrary(_T("loghours.dll"));
  188. if ( NULL == hLogonScheduleDLL )
  189. {
  190. hr = HRESULT_FROM_WIN32(GetLastError());
  191. ErrorTrace(ERROR_NAPMMC_TIMEOFDAY, "LoadLibrary() failed, err = %x", hr);
  192. ShowErrorDialog(NULL, IDS_ERROR_CANT_FIND_LOGHOURSDLL, NULL, hr);
  193. goto win32_error;
  194. }
  195. pfnLogonScheduleDialogEx = (PFN_LOGONSCHEDULEDIALOGEX) GetProcAddress(hLogonScheduleDLL, "DialinHoursDialogEx");
  196. if ( ! pfnLogonScheduleDialogEx )
  197. {
  198. hr = HRESULT_FROM_WIN32(GetLastError());
  199. ErrorTrace(ERROR_NAPMMC_TIMEOFDAY, "GetProcAddress() failed for DialinHoursDialogEx, err = %x", hr);
  200. ShowErrorDialog(NULL, IDS_ERROR_CANT_FIND_LOGHOURSAPI, NULL, hr);
  201. FreeLibrary(hLogonScheduleDLL);
  202. goto win32_error;
  203. }
  204. //
  205. // now we do have this DLL, call the API
  206. //
  207. hr = pfnLogonScheduleDialogEx( NULL // We don't have an HWND available to pass, but NULL asks the dialog to display itself modally as desired.
  208. , (BYTE**)&pMap
  209. , strDialogTitle
  210. , 1 // This is defined in loghrapi.h (which we don't have access to) to mean "accept in local time".
  211. );
  212. FreeLibrary(hLogonScheduleDLL);
  213. DebugTrace(DEBUG_NAPMMC_TIMEOFDAY, "LogonScheduleDialogEx() returned %x", hr);
  214. if ( FAILED(hr) )
  215. {
  216. goto win32_error;
  217. }
  218. //
  219. // convert the hourmap to a text string
  220. //
  221. // We need to reverse it first so our conversion code can understand it.
  222. //
  223. ReverseHourMap(pMap,21);
  224. HourMapToStr(pMap, strHourMap) ;
  225. return S_OK;
  226. win32_error:
  227. ShowErrorDialog(NULL,
  228. IDS_ERROR_TIMEOFDAY,
  229. NULL,
  230. HRESULT_FROM_WIN32(GetLastError())
  231. );
  232. return hr;
  233. }