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.

455 lines
13 KiB

  1. /****************************************************************************
  2. PROGRAM: Generic.c
  3. PURPOSE: Generic template for Windows applications
  4. ****************************************************************************/
  5. #include <windows.h>
  6. #include <userenv.h>
  7. #include <userenvp.h>
  8. #include "generic.h"
  9. HINSTANCE hInst;
  10. HWND hwndMain;
  11. HANDLE hProfile, hUserToken;
  12. TCHAR szAppName[] = TEXT("Generic");
  13. #ifdef UNICODE
  14. TCHAR szTitle[] = TEXT("UserEnv Profile Test App - UNICODE");
  15. #else
  16. TCHAR szTitle[] = TEXT("UserEnv Profile Test App - ANSI");
  17. #endif
  18. TCHAR szSrcDir[MAX_PATH];
  19. TCHAR szDestDir[MAX_PATH];
  20. TCHAR szProfilePath[MAX_PATH];
  21. TCHAR szDefaultPath[MAX_PATH];
  22. TCHAR szUserName[MAX_PATH];
  23. TCHAR szDomainName[MAX_PATH];
  24. void ChangeMenuState (BOOL bLoggedOn);
  25. BOOL GetProfileType(DWORD *flags);
  26. /****************************************************************************
  27. FUNCTION: WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  28. PURPOSE: calls initialization function, processes message loop
  29. ****************************************************************************/
  30. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  31. LPSTR lpCmdLine, INT nCmdShow)
  32. {
  33. MSG msg;
  34. HANDLE hAccelTable;
  35. if (!hPrevInstance)
  36. {
  37. if (!InitApplication(hInstance))
  38. {
  39. return (FALSE);
  40. }
  41. }
  42. // Perform initializations that apply to a specific instance
  43. if (!InitInstance(hInstance, nCmdShow))
  44. {
  45. return (FALSE);
  46. }
  47. hAccelTable = LoadAccelerators (hInstance, szAppName);
  48. while (GetMessage(&msg, NULL, 0, 0))
  49. {
  50. if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg))
  51. {
  52. TranslateMessage(&msg);
  53. DispatchMessage(&msg);
  54. }
  55. }
  56. return (msg.wParam);
  57. lpCmdLine;
  58. }
  59. /****************************************************************************
  60. FUNCTION: InitApplication(HINSTANCE)
  61. PURPOSE: Initializes window data and registers window class
  62. ****************************************************************************/
  63. BOOL InitApplication(HINSTANCE hInstance)
  64. {
  65. WNDCLASS wc;
  66. wc.style = CS_HREDRAW | CS_VREDRAW;
  67. wc.lpfnWndProc = WndProc;
  68. wc.cbClsExtra = 0;
  69. wc.cbWndExtra = 0;
  70. wc.hInstance = hInstance;
  71. wc.hIcon = LoadIcon (hInstance, szAppName);
  72. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  73. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  74. wc.lpszMenuName = szAppName;
  75. wc.lpszClassName = szAppName;
  76. return (RegisterClass(&wc));
  77. }
  78. /****************************************************************************
  79. FUNCTION: InitInstance(HINSTANCE, int)
  80. PURPOSE: Saves instance handle and creates main window
  81. ****************************************************************************/
  82. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
  83. {
  84. HWND hWnd;
  85. hInst = hInstance;
  86. hProfile = NULL;
  87. szSrcDir[0] = TEXT('\0');
  88. szDestDir[0] = TEXT('\0');
  89. szProfilePath[0] = TEXT('\0');
  90. szDefaultPath[0] = TEXT('\0');
  91. szUserName[0] = TEXT('\0');
  92. szDomainName[0] = TEXT('\0');
  93. hWnd = CreateWindow(szAppName,
  94. szTitle,
  95. WS_OVERLAPPEDWINDOW,
  96. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0,
  97. NULL,
  98. NULL,
  99. hInstance,
  100. NULL);
  101. if (!hWnd)
  102. {
  103. return (FALSE);
  104. }
  105. else
  106. {
  107. hwndMain = hWnd;
  108. }
  109. ChangeMenuState(FALSE);
  110. PostMessage (hWnd, WM_COMMAND, IDM_LOGON, 0);
  111. ShowWindow(hWnd, SW_SHOWDEFAULT);
  112. UpdateWindow(hWnd);
  113. return (TRUE);
  114. }
  115. /****************************************************************************
  116. FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
  117. PURPOSE: Processes messages
  118. ****************************************************************************/
  119. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  120. {
  121. DWORD dwFlags;
  122. TCHAR szBuffer[MAX_PATH];
  123. switch (message)
  124. {
  125. case WM_COMMAND:
  126. {
  127. switch (LOWORD(wParam))
  128. {
  129. case IDM_LOGON:
  130. if (DialogBox (hInst, TEXT("LOGONDLG"), hWnd, LogonDlgProc)) {
  131. ChangeMenuState(TRUE);
  132. }
  133. break;
  134. case IDM_LOGOFF:
  135. CloseHandle(hUserToken);
  136. ChangeMenuState(FALSE);
  137. break;
  138. case IDM_LUP:
  139. DialogBox (hInst, TEXT("LUPDLG"), hWnd, LUPDlgProc);
  140. break;
  141. case IDM_ULUP:
  142. if (hProfile) {
  143. if (UnloadUserProfile(hUserToken, hProfile)) {
  144. hProfile = NULL;
  145. }
  146. }
  147. break;
  148. case IDM_PFTYPE:
  149. if (hUserToken) {
  150. if (!ImpersonateLoggedOnUser(hUserToken)) {
  151. wsprintf(szBuffer, TEXT("Impersonate Failed with Error %d"), GetLastError());
  152. MessageBox (hWnd, szBuffer, TEXT("Impersonate"), MB_OK | MB_ICONEXCLAMATION);
  153. return TRUE;
  154. }
  155. if (GetProfileType(&dwFlags)) {
  156. }
  157. RevertToSelf();
  158. }
  159. break;
  160. case IDM_ABOUT:
  161. DialogBox (hInst, TEXT("AboutBox"), hWnd, About);
  162. break;
  163. case IDM_EXIT:
  164. DestroyWindow (hwndMain);
  165. break;
  166. default:
  167. return (DefWindowProc(hWnd, message, wParam, lParam));
  168. }
  169. }
  170. break;
  171. case WM_DESTROY:
  172. if (hProfile) {
  173. if (UnloadUserProfile(hUserToken, hProfile)) {
  174. hProfile = NULL;
  175. }
  176. }
  177. PostQuitMessage(0);
  178. break;
  179. default:
  180. return (DefWindowProc(hWnd, message, wParam, lParam));
  181. }
  182. return FALSE;
  183. }
  184. /****************************************************************************
  185. FUNCTION: About(HWND, UINT, WPARAM, LPARAM)
  186. PURPOSE: Processes messages for "About" dialog box
  187. ****************************************************************************/
  188. LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  189. {
  190. switch (message)
  191. {
  192. case WM_COMMAND:
  193. if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
  194. {
  195. EndDialog(hDlg, TRUE);
  196. return (TRUE);
  197. }
  198. break;
  199. }
  200. return (FALSE);
  201. lParam;
  202. }
  203. /****************************************************************************
  204. FUNCTION: LUPDlgProc(HWND, UINT, WPARAM, LPARAM)
  205. ****************************************************************************/
  206. LRESULT CALLBACK LUPDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  207. {
  208. switch (message)
  209. {
  210. case WM_INITDIALOG:
  211. SetDlgItemText(hDlg, IDD_PROFILEPATH, szProfilePath);
  212. SetDlgItemText(hDlg, IDD_DEFAULTPATH, szDefaultPath);
  213. SetFocus (GetDlgItem (hDlg, IDD_PROFILEPATH));
  214. return FALSE;
  215. case WM_COMMAND:
  216. if (LOWORD(wParam) == IDOK)
  217. {
  218. PROFILEINFO pi;
  219. DWORD dwStart, dwEnd, dwFlags = 0;
  220. TCHAR szBuffer[200];
  221. HCURSOR hCursor;
  222. BOOL bResult;
  223. SetDlgItemText(hDlg, IDD_RETVAL, NULL);
  224. SetDlgItemText(hDlg, IDD_TIME, NULL);
  225. SetDlgItemText(hDlg, IDD_PROFILE, NULL);
  226. GetDlgItemText(hDlg, IDD_PROFILEPATH, szProfilePath, MAX_PATH);
  227. GetDlgItemText(hDlg, IDD_DEFAULTPATH, szDefaultPath, MAX_PATH);
  228. if (IsDlgButtonChecked(hDlg, IDD_NOUI)) {
  229. dwFlags |= PI_NOUI;
  230. }
  231. if (IsDlgButtonChecked(hDlg, IDD_APPLYPOLICY)) {
  232. dwFlags |= PI_APPLYPOLICY;
  233. pi.lpPolicyPath = TEXT("ntconfig.pol");
  234. }
  235. if (IsDlgButtonChecked(hDlg, IDD_LITELOAD)) {
  236. dwFlags |= PI_LITELOAD;
  237. }
  238. pi.dwSize = sizeof(PROFILEINFO);
  239. pi.dwFlags = dwFlags;
  240. pi.lpUserName = szUserName;
  241. pi.lpProfilePath = szProfilePath;
  242. pi.lpDefaultPath = szDefaultPath;
  243. pi.lpServerName = NULL;
  244. hCursor = SetCursor(LoadCursor(NULL, IDC_WAIT));
  245. dwStart = GetTickCount();
  246. bResult = LoadUserProfile(hUserToken, &pi);
  247. dwEnd = GetTickCount();
  248. SetCursor(hCursor);
  249. SetDlgItemInt(hDlg, IDD_TIME, dwEnd - dwStart, FALSE);
  250. SetDlgItemInt(hDlg, IDD_RETVAL, bResult, FALSE);
  251. if (bResult) {
  252. hProfile = pi.hProfile;
  253. wsprintf (szBuffer, TEXT("0x%x"), pi.hProfile);
  254. SetDlgItemText(hDlg, IDD_PROFILE, szBuffer);
  255. }
  256. }
  257. if (LOWORD(wParam) == IDCANCEL)
  258. {
  259. EndDialog(hDlg, TRUE);
  260. return (TRUE);
  261. }
  262. break;
  263. }
  264. return (FALSE);
  265. }
  266. void ChangeMenuState (BOOL bLoggedOn)
  267. {
  268. HMENU hMenu;
  269. hMenu = GetMenu(hwndMain);
  270. if (bLoggedOn) {
  271. EnableMenuItem (hMenu, IDM_LOGON, MF_BYCOMMAND | MF_GRAYED);
  272. EnableMenuItem (hMenu, IDM_LOGOFF, MF_BYCOMMAND | MF_ENABLED);
  273. EnableMenuItem (hMenu, IDM_LUP, MF_BYCOMMAND | MF_ENABLED);
  274. EnableMenuItem (hMenu, IDM_ULUP, MF_BYCOMMAND | MF_ENABLED);
  275. } else {
  276. EnableMenuItem (hMenu, IDM_LOGOFF, MF_BYCOMMAND | MF_GRAYED);
  277. EnableMenuItem (hMenu, IDM_LUP, MF_BYCOMMAND | MF_GRAYED);
  278. EnableMenuItem (hMenu, IDM_ULUP, MF_BYCOMMAND | MF_GRAYED);
  279. EnableMenuItem (hMenu, IDM_LOGON, MF_BYCOMMAND | MF_ENABLED);
  280. }
  281. }
  282. /****************************************************************************
  283. FUNCTION: LogonDlgProc(HWND, UINT, WPARAM, LPARAM)
  284. ****************************************************************************/
  285. LRESULT CALLBACK LogonDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  286. {
  287. switch (message)
  288. {
  289. case WM_INITDIALOG:
  290. {
  291. HBITMAP hBmp;
  292. hBmp = LoadBitmap (hInst, TEXT("Logo"));
  293. if (hBmp) {
  294. SendDlgItemMessage (hDlg, IDD_ICON, STM_SETIMAGE,
  295. IMAGE_BITMAP, (LPARAM) hBmp);
  296. }
  297. SetDlgItemText(hDlg, IDD_USERNAME, szUserName);
  298. SetDlgItemText(hDlg, IDD_DOMAIN, szDomainName);
  299. }
  300. return TRUE;
  301. case WM_COMMAND:
  302. if (LOWORD(wParam) == IDOK)
  303. {
  304. TCHAR szPassword[MAX_PATH];
  305. GetDlgItemText(hDlg, IDD_USERNAME, szUserName, MAX_PATH);
  306. GetDlgItemText(hDlg, IDD_DOMAIN, szDomainName, MAX_PATH);
  307. GetDlgItemText(hDlg, IDD_PASSWORD, szPassword, MAX_PATH);
  308. if (LogonUser(szUserName, szDomainName, szPassword,
  309. LOGON32_LOGON_INTERACTIVE,
  310. LOGON32_PROVIDER_DEFAULT,
  311. &hUserToken)) {
  312. EndDialog(hDlg, TRUE);
  313. return TRUE;
  314. } else {
  315. TCHAR szBuffer[200];
  316. wsprintf(szBuffer, TEXT("Logon Failed with Error %d"),
  317. GetLastError());
  318. MessageBox (hDlg, szBuffer, TEXT("Logon"), MB_OK | MB_ICONEXCLAMATION);
  319. return TRUE;
  320. }
  321. }
  322. if (LOWORD(wParam) == IDCANCEL)
  323. {
  324. EndDialog(hDlg, FALSE);
  325. return TRUE;
  326. }
  327. break;
  328. }
  329. return (FALSE);
  330. }