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.

390 lines
9.2 KiB

  1. /*++
  2. Copyright (c) 1994-1998, Microsoft Corporation All rights reserved.
  3. Module Name:
  4. worldmap.c
  5. Abstract:
  6. This module implements the world map for the Date/Time applet.
  7. Revision History:
  8. --*/
  9. // Include Files.
  10. #include <nt.h>
  11. #include <ntrtl.h>
  12. #include <nturtl.h>
  13. #include "timedate.h"
  14. #include "rc.h"
  15. #include <cpl.h>
  16. //
  17. // Global Variables.
  18. //
  19. HINSTANCE g_hInst = NULL;
  20. CRITICAL_SECTION g_csDll; /* The shared critical section */
  21. //
  22. // Function Prototypes.
  23. //
  24. extern BOOL OpenDateTimePropertySheet(HWND hwnd, LPCTSTR cmdline);
  25. BOOL
  26. EnableTimePrivilege(
  27. PTOKEN_PRIVILEGES *pPreviousState,
  28. ULONG *pPreviousStateLength);
  29. BOOL
  30. ResetTimePrivilege(
  31. PTOKEN_PRIVILEGES PreviousState,
  32. ULONG PreviousStateLength);
  33. int DoMessageBox(
  34. HWND hWnd,
  35. DWORD wText,
  36. DWORD wCaption,
  37. DWORD wType);
  38. ////////////////////////////////////////////////////////////////////////////
  39. //
  40. // LibMain
  41. //
  42. ////////////////////////////////////////////////////////////////////////////
  43. BOOL WINAPI LibMain(
  44. HANDLE hDll,
  45. DWORD dwReason,
  46. LPVOID lpReserved)
  47. {
  48. switch (dwReason)
  49. {
  50. case ( DLL_PROCESS_ATTACH ) :
  51. {
  52. g_hInst = hDll;
  53. DisableThreadLibraryCalls(hDll);
  54. InitializeCriticalSection(&g_csDll);
  55. break;
  56. }
  57. case ( DLL_PROCESS_DETACH ) :
  58. {
  59. DeleteCriticalSection(&g_csDll);
  60. break;
  61. }
  62. }
  63. return (TRUE);
  64. }
  65. ////////////////////////////////////////////////////////////////////////////
  66. //
  67. // CplApplet
  68. //
  69. // The main applet information manager.
  70. //
  71. ////////////////////////////////////////////////////////////////////////////
  72. LONG WINAPI CPlApplet(
  73. HWND hwnd,
  74. UINT uMsg,
  75. LPARAM lParam1,
  76. LPARAM lParam2)
  77. {
  78. static BOOL fReEntered = FALSE;
  79. switch (uMsg)
  80. {
  81. case ( CPL_INIT ) :
  82. {
  83. return (TRUE);
  84. }
  85. case ( CPL_GETCOUNT ) :
  86. {
  87. //
  88. // How many applets are in this DLL?
  89. //
  90. return (1);
  91. }
  92. case ( CPL_INQUIRE ) :
  93. {
  94. //
  95. // Fill the CPLINFO with the pertinent information.
  96. //
  97. #define lpOldCPlInfo ((LPCPLINFO)lParam2)
  98. switch (lParam1)
  99. {
  100. case ( 0 ) :
  101. {
  102. lpOldCPlInfo->idIcon = IDI_TIMEDATE;
  103. lpOldCPlInfo->idName = IDS_TIMEDATE;
  104. lpOldCPlInfo->idInfo = IDS_TIMEDATEINFO;
  105. break;
  106. }
  107. }
  108. lpOldCPlInfo->lData = 0L;
  109. return (TRUE);
  110. }
  111. case ( CPL_NEWINQUIRE ) :
  112. {
  113. #define lpCPlInfo ((LPNEWCPLINFO)lParam2)
  114. switch (lParam1)
  115. {
  116. case ( 0 ) :
  117. {
  118. lpCPlInfo->hIcon = LoadIcon( g_hInst,
  119. MAKEINTRESOURCE(IDI_TIMEDATE) );
  120. LoadString( g_hInst,
  121. IDS_TIMEDATE,
  122. lpCPlInfo->szName,
  123. sizeof(lpCPlInfo->szName) );
  124. LoadString( g_hInst,
  125. IDS_TIMEDATEINFO,
  126. lpCPlInfo->szInfo,
  127. sizeof(lpCPlInfo->szInfo) );
  128. lpCPlInfo->dwHelpContext = 0;
  129. break;
  130. }
  131. }
  132. lpCPlInfo->dwSize = sizeof(NEWCPLINFO);
  133. lpCPlInfo->lData = 0L;
  134. lpCPlInfo->szHelpFile[0] = 0;
  135. return (TRUE);
  136. }
  137. case ( CPL_DBLCLK ) :
  138. {
  139. lParam2 = (LPARAM)0;
  140. // fall thru...
  141. }
  142. case ( CPL_STARTWPARMS ) :
  143. {
  144. //
  145. // Do the applet thing.
  146. //
  147. switch (lParam1)
  148. {
  149. case ( 0 ) :
  150. {
  151. PTOKEN_PRIVILEGES PreviousState;
  152. ULONG PreviousStateLength;
  153. if (EnableTimePrivilege(&PreviousState, &PreviousStateLength))
  154. {
  155. OpenDateTimePropertySheet(hwnd, (LPCTSTR)lParam2);
  156. ResetTimePrivilege(PreviousState, PreviousStateLength);
  157. }
  158. else
  159. {
  160. DoMessageBox( hwnd,
  161. IDS_NOTIMEERROR,
  162. IDS_CAPTION,
  163. MB_OK | MB_ICONINFORMATION );
  164. }
  165. break;
  166. }
  167. }
  168. break;
  169. }
  170. case ( CPL_EXIT ) :
  171. {
  172. fReEntered = FALSE;
  173. //
  174. // Free up any allocations of resources made.
  175. //
  176. break;
  177. }
  178. default :
  179. {
  180. return (0L);
  181. }
  182. }
  183. return (1L);
  184. }
  185. ////////////////////////////////////////////////////////////////////////////
  186. //
  187. // EnableTimePrivilege
  188. //
  189. ////////////////////////////////////////////////////////////////////////////
  190. BOOL EnableTimePrivilege(
  191. PTOKEN_PRIVILEGES *pPreviousState,
  192. ULONG *pPreviousStateLength)
  193. {
  194. #ifndef WINNT
  195. return TRUE;
  196. #else
  197. NTSTATUS NtStatus;
  198. HANDLE Token;
  199. LUID SystemTimePrivilege;
  200. PTOKEN_PRIVILEGES NewState;
  201. //
  202. // Open our own token.
  203. //
  204. NtStatus = NtOpenProcessToken( NtCurrentProcess(),
  205. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  206. &Token );
  207. if (!NT_SUCCESS(NtStatus))
  208. {
  209. return (FALSE);
  210. }
  211. //
  212. // Initialize the adjustment structure.
  213. //
  214. SystemTimePrivilege = RtlConvertLongToLuid(SE_SYSTEMTIME_PRIVILEGE);
  215. NewState = (PTOKEN_PRIVILEGES)LocalAlloc(LPTR, 100);
  216. if (NewState == NULL)
  217. {
  218. return (FALSE);
  219. }
  220. NewState->PrivilegeCount = 1;
  221. NewState->Privileges[0].Luid = SystemTimePrivilege;
  222. NewState->Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  223. *pPreviousState = (PTOKEN_PRIVILEGES)LocalAlloc(LPTR, 100);
  224. if (*pPreviousState == NULL)
  225. {
  226. LocalFree(NewState);
  227. return (FALSE);
  228. }
  229. // Set the state of the privilege to ENABLED.
  230. NtStatus = NtAdjustPrivilegesToken( Token, // TokenHandle
  231. FALSE, // DisableAllPrivileges
  232. NewState, // NewState
  233. 100, // BufferLength
  234. *pPreviousState, // PreviousState (OPTIONAL)
  235. pPreviousStateLength ); // ReturnLength
  236. //
  237. // Clean up some stuff before returning.
  238. //
  239. LocalFree(NewState);
  240. if (NtStatus == STATUS_SUCCESS)
  241. {
  242. NtClose(Token);
  243. return (TRUE);
  244. }
  245. else
  246. {
  247. LocalFree(*pPreviousState);
  248. NtClose(Token);
  249. return (FALSE);
  250. }
  251. #endif //WINNT
  252. }
  253. ////////////////////////////////////////////////////////////////////////////
  254. //
  255. // ResetTimePrivilege
  256. //
  257. // Restore previous privilege state for setting system time.
  258. //
  259. ////////////////////////////////////////////////////////////////////////////
  260. BOOL ResetTimePrivilege(
  261. PTOKEN_PRIVILEGES PreviousState,
  262. ULONG PreviousStateLength)
  263. {
  264. #ifndef WINNT
  265. return TRUE;
  266. #else
  267. NTSTATUS NtStatus;
  268. HANDLE Token;
  269. LUID SystemTimePrivilege;
  270. ULONG ReturnLength;
  271. if (PreviousState == NULL)
  272. {
  273. return (FALSE);
  274. }
  275. // Open our own token.
  276. NtStatus = NtOpenProcessToken( NtCurrentProcess(),
  277. TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY,
  278. &Token);
  279. if (!NT_SUCCESS(NtStatus))
  280. {
  281. return (FALSE);
  282. }
  283. // Initialize the adjustment structure.
  284. SystemTimePrivilege = RtlConvertLongToLuid(SE_SYSTEMTIME_PRIVILEGE);
  285. // Restore previous state of the privilege.
  286. NtStatus = NtAdjustPrivilegesToken( Token, // TokenHandle
  287. FALSE, // DisableAllPrivileges
  288. PreviousState, // NewState
  289. PreviousStateLength, // BufferLength
  290. NULL, // PreviousState (OPTIONAL)
  291. &ReturnLength ); // ReturnLength
  292. // Clean up some stuff before returning.
  293. LocalFree(PreviousState);
  294. NtClose(Token);
  295. return (NT_SUCCESS(NtStatus));
  296. #endif //WINNT
  297. }
  298. ////////////////////////////////////////////////////////////////////////////
  299. //
  300. // DoMessageBox
  301. //
  302. ////////////////////////////////////////////////////////////////////////////
  303. int DoMessageBox(
  304. HWND hWnd,
  305. DWORD wText,
  306. DWORD wCaption,
  307. DWORD wType)
  308. {
  309. TCHAR szText[2 * MAX_PATH];
  310. TCHAR szCaption[MAX_PATH];
  311. if (!LoadString(g_hInst, wText, szText, CharSizeOf(szText)))
  312. {
  313. return (0);
  314. }
  315. if (!LoadString(g_hInst, wCaption, szCaption, CharSizeOf(szCaption)))
  316. {
  317. return (0);
  318. }
  319. return ( MessageBox(hWnd, szText, szCaption, wType) );
  320. }