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.

381 lines
10 KiB

  1. //*********************************************************************
  2. //* Microsoft Windows **
  3. //* Copyright(c) Microsoft Corp., 1993 **
  4. //*********************************************************************
  5. #include "admincfg.h"
  6. HINSTANCE ghInst; // current instance
  7. CHAR *pbufTemplates; //Buffer containing list of all active template files
  8. HGLOBAL hBufTemplates;
  9. DWORD dwBufTemplates;
  10. BOOL ParseCommandLine(LPSTR lpszCommandLine,DWORD * pdwFlags);
  11. extern VOID RunDialogMode(HWND hWnd,HWND hwndUser);
  12. BOOL RestoreWindowPlacement( HWND hWnd,int nCmdShow);
  13. //extern HBRUSH hbrWindow;
  14. //extern HBRUSH hbrWindowText;
  15. //extern HFONT hfontHelv;
  16. CHAR szAppName[SMALLBUF];
  17. DWORD dwCmdLineFlags=0;
  18. DWORD dwDlgRetCode=AD_SUCCESS;
  19. BOOL g_bWinnt;
  20. /*******************************************************************
  21. NAME: WinMain
  22. SYNOPSIS: App entry point
  23. ********************************************************************/
  24. int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,
  25. LPSTR lpCmdLine,int nCmdShow)
  26. {
  27. MSG msg;
  28. HANDLE hAccelTable;
  29. OSVERSIONINFO ver;
  30. //
  31. // Determine if we are running on Windows NT
  32. //
  33. ver.dwOSVersionInfoSize = sizeof(ver);
  34. if (GetVersionEx(&ver)) {
  35. g_bWinnt = (ver.dwPlatformId == VER_PLATFORM_WIN32_NT);
  36. } else {
  37. g_bWinnt = FALSE;
  38. }
  39. //
  40. // If this is NT, turn off alignment faults
  41. //
  42. if (g_bWinnt) {
  43. UINT uiErrMode;
  44. uiErrMode = SetErrorMode(0);
  45. SetErrorMode (uiErrMode | SEM_NOALIGNMENTFAULTEXCEPT);
  46. }
  47. if (!LoadString(hInstance,IDS_APPNAME,szAppName,sizeof(szAppName))) {
  48. MsgBox(NULL,IDS_ErrOUTOFMEMORY,MB_ICONEXCLAMATION,MB_OK);
  49. return FALSE;
  50. }
  51. // allocate the buffer to read template files into
  52. if (!(hBufTemplates = GlobalAlloc(GHND,MEDIUMBUF)) ||
  53. !(pbufTemplates = (CHAR *) GlobalLock(hBufTemplates))) {
  54. if (hBufTemplates)
  55. GlobalFree(hBufTemplates);
  56. return FALSE;
  57. }
  58. dwBufTemplates = MEDIUMBUF;
  59. // validate command line flags, if in dialog mode make sure we have all the requred
  60. // pieces. If not, bag out
  61. if (!ParseCommandLine(lpCmdLine,&dwCmdLineFlags) ||
  62. (dwCmdLineFlags & CLF_DIALOGMODE) && (!(dwCmdLineFlags &
  63. CLF_USETEMPLATENAME) || !(dwCmdLineFlags & CLF_USEPOLICYFILENAME) ||
  64. !(dwCmdLineFlags & (CLF_USEWORKSTATIONNAME | CLF_USEUSERNAME)))) {
  65. MsgBox(NULL,IDS_ErrCOMMANDLINE,MB_ICONEXCLAMATION,MB_OK);
  66. return FALSE;
  67. }
  68. if (!hPrevInstance) {
  69. if (!InitApplication(hInstance)) {
  70. return (FALSE);
  71. }
  72. }
  73. /* Perform initializations that apply to a specific instance */
  74. if (!InitInstance(hInstance, nCmdShow)) {
  75. #ifdef DEBUG
  76. OutputDebugString("InitInstance returned FALSE\r\n");
  77. #endif
  78. return (FALSE);
  79. }
  80. hAccelTable = LoadAccelerators (hInstance,MAKEINTRESOURCE(IDA_ACCEL));
  81. /* Acquire and dispatch messages until a WM_QUIT message is received. */
  82. while (GetMessage(&msg,NULL,0,0))
  83. {
  84. if (!TranslateAccelerator (msg.hwnd, hAccelTable, &msg)) {
  85. TranslateMessage(&msg);// Translates virtual key codes
  86. DispatchMessage(&msg); // Dispatches message to window
  87. }
  88. }
  89. // if (hfontHelv) DeleteObject(hfontHelv);
  90. if (dwCmdLineFlags & CLF_DIALOGMODE)
  91. ExitProcess(dwDlgRetCode);
  92. return (int)(msg.wParam); // Returns the value from PostQuitMessage
  93. lpCmdLine; // This will prevent 'unused formal parameter' warnings
  94. }
  95. /****************************************************************************
  96. NAME: ParseCommandLine
  97. SYNOPSIS: Parses command line for filename and other information
  98. ****************************************************************************/
  99. BOOL ParseCommandLine(LPSTR lpszCommandLine,DWORD * pdwFlags)
  100. {
  101. CHAR * GetTextToNextSpace(LPSTR pszText,CHAR * pszOutBuf,UINT cbOutBuf,
  102. BOOL fSkipLeading);
  103. if (!lpszCommandLine || !*lpszCommandLine)
  104. return TRUE; // nothing to do
  105. while (*lpszCommandLine) {
  106. // advance past spaces
  107. while (*lpszCommandLine == ' ')
  108. lpszCommandLine ++;
  109. if (*lpszCommandLine == '/') {
  110. lpszCommandLine ++;
  111. if (!*lpszCommandLine)
  112. return FALSE;
  113. switch (*lpszCommandLine) {
  114. case 'u':
  115. case 'U': // user name
  116. lpszCommandLine = GetTextToNextSpace(lpszCommandLine,
  117. szDlgModeUserName,sizeof(szDlgModeUserName),TRUE);
  118. if (!lstrlen(szDlgModeUserName)) return FALSE;
  119. *pdwFlags |= CLF_USEUSERNAME;
  120. break;
  121. case 'w':
  122. case 'W': // workstation name
  123. lpszCommandLine = GetTextToNextSpace(lpszCommandLine,
  124. szDlgModeUserName,sizeof(szDlgModeUserName),TRUE);
  125. if (!lstrlen(szDlgModeUserName)) return FALSE;
  126. *pdwFlags |= CLF_USEWORKSTATIONNAME;
  127. break;
  128. case 't':
  129. case 'T': // template name
  130. lpszCommandLine = GetTextToNextSpace(lpszCommandLine,
  131. pbufTemplates,dwBufTemplates,TRUE);
  132. if (!lstrlen(pbufTemplates)) return FALSE;
  133. *pdwFlags |= CLF_USETEMPLATENAME;
  134. break;
  135. case 'f':
  136. case 'F': // file name
  137. lpszCommandLine = GetTextToNextSpace(lpszCommandLine,
  138. szDatFilename,sizeof(szDatFilename),TRUE);
  139. if (!lstrlen(szDatFilename)) return FALSE;
  140. *pdwFlags |= CLF_USEPOLICYFILENAME;
  141. break;
  142. case 'd':
  143. case 'D': // dialog mode switch
  144. *pdwFlags |= CLF_DIALOGMODE;
  145. lpszCommandLine++;
  146. break;
  147. default:
  148. return FALSE;
  149. }
  150. } else {
  151. lpszCommandLine = GetTextToNextSpace(lpszCommandLine,
  152. szDatFilename,sizeof(szDatFilename),FALSE);
  153. return TRUE;
  154. }
  155. }
  156. return TRUE;
  157. }
  158. CHAR * GetTextToNextSpace(LPSTR pszText,CHAR * pszOutBuf,UINT cbOutBuf,
  159. BOOL fSkipLeading)
  160. {
  161. BOOL fInQuote = FALSE;
  162. lstrcpy(pszOutBuf,szNull);
  163. if (!pszText)
  164. return NULL;
  165. if (fSkipLeading) {
  166. // skip 1st character
  167. pszText++;
  168. // skip leading colon, if there is one
  169. if (*pszText == ':') pszText ++;
  170. }
  171. while (*pszText && cbOutBuf>1) {
  172. if (*pszText == ' ' && !fInQuote)
  173. break;
  174. if (*pszText == '\"') {
  175. fInQuote = !fInQuote;
  176. } else {
  177. *pszOutBuf = *pszText;
  178. pszOutBuf ++;
  179. cbOutBuf --;
  180. }
  181. pszText ++;
  182. }
  183. if (cbOutBuf)
  184. *pszOutBuf = '\0'; // null-terminate
  185. while (*pszText == ' ')
  186. pszText++; // advance past spaces
  187. return pszText;
  188. }
  189. /****************************************************************************
  190. NAME: InitApplication
  191. SYNOPSIS: Initializes window data and registers window class
  192. ****************************************************************************/
  193. BOOL InitApplication(HINSTANCE hInstance)
  194. {
  195. WNDCLASS wc;
  196. // Fill in window class structure with parameters that describe the
  197. // main window.
  198. wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;// Class style(s).
  199. wc.lpfnWndProc = (WNDPROC)WndProc; // Window Procedure
  200. wc.cbClsExtra = 0; // No per-class extra data.
  201. wc.cbWndExtra = 0; // No per-window extra data.
  202. wc.hInstance = hInstance; // Owner of this class
  203. wc.hIcon = LoadIcon (hInstance, MAKEINTRESOURCE(IDI_APPICON));
  204. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  205. wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
  206. wc.lpszMenuName = MAKEINTRESOURCE(IDM_MAIN);
  207. wc.lpszClassName = szAppName; // Name to register as
  208. // Register the window class and return success/failure code.
  209. if (!RegisterClass(&wc)) return FALSE;
  210. wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;// Class style(s).
  211. wc.lpfnWndProc = (WNDPROC)ClipWndProc; // Window Procedure
  212. wc.cbClsExtra = 0; // No per-class extra data.
  213. wc.cbWndExtra = sizeof(DWORD);
  214. wc.hInstance = hInstance; // Owner of this class
  215. wc.hIcon = NULL;
  216. wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1);
  217. wc.lpszMenuName = NULL;
  218. wc.lpszClassName = "ClipClass"; // Name to register as
  219. if (!RegisterClass(&wc)) return FALSE;
  220. return TRUE;
  221. }
  222. /*******************************************************************
  223. NAME: InitInstance
  224. SYNOPSIS: Saves instance handle and creates main window
  225. ********************************************************************/
  226. BOOL InitInstance(HINSTANCE hInstance,int nCmdShow)
  227. {
  228. HWND hWnd; // Main window handle.
  229. DWORD dwStyle = WS_OVERLAPPEDWINDOW;
  230. // Save the instance handle in static variable, which will be used in
  231. // many subsequence calls from this application to Windows.
  232. ghInst = hInstance; // Store instance handle in our global variable
  233. if (dwCmdLineFlags & CLF_DIALOGMODE)
  234. dwStyle = (WS_OVERLAPPED | WS_CAPTION) &~ WS_VISIBLE; // invisible window
  235. // Create a main window for this application instance.
  236. hWnd = CreateWindow(
  237. szAppName, // See RegisterClass() call.
  238. szAppName, // Text for window title bar.
  239. dwStyle, // Window style.
  240. CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, // Use default positioning
  241. NULL, // Overlapped windows have no parent.
  242. NULL, // Use the window class menu.
  243. hInstance, // This instance owns this window.
  244. NULL // We don't use any data in our WM_CREATE
  245. );
  246. // If window could not be created, return "failure"
  247. if (!hWnd) {
  248. return (FALSE);
  249. }
  250. SetClassLongPtr(hWnd, GCLP_HICONSM, (LONG_PTR)LoadIcon(hInstance, szAppName));
  251. // hide app window if in dialog mode (but still create it to minimize
  252. // code path changes elsewhere)
  253. if (dwCmdLineFlags & CLF_DIALOGMODE)
  254. nCmdShow = SW_HIDE;
  255. RestoreWindowPlacement(hWnd, nCmdShow);
  256. UpdateWindow(hWnd); // Sends WM_PAINT message
  257. if (dwCmdLineFlags & CLF_DIALOGMODE) {
  258. RunDialogMode(hWnd,hwndUser);
  259. DestroyWindow(hWnd);
  260. }
  261. return (TRUE);
  262. }
  263. /****************************************************************************
  264. FUNCTION: About(HWND, UINT, WPARAM, LPARAM)
  265. PURPOSE: Processes messages for "About" dialog box
  266. MESSAGES:
  267. WM_INITDIALOG - initialize dialog box
  268. WM_COMMAND - Input received
  269. COMMENTS:
  270. Display version information from the version section of the
  271. application resource.
  272. Wait for user to click on "Ok" button, then close the dialog box.
  273. ****************************************************************************/
  274. LRESULT CALLBACK About(
  275. HWND hDlg, // window handle of the dialog box
  276. UINT message, // type of message
  277. WPARAM uParam, // message-specific information
  278. LPARAM lParam)
  279. {
  280. return (FALSE); // Didn't process the message
  281. lParam; // This will prevent 'unused formal parameter' warnings
  282. }