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.

254 lines
5.0 KiB

  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // MAIN.CPP / Tuneup
  4. //
  5. // Microsoft Confidential
  6. // Copyright (c) Microsoft Corporation 1998
  7. // All rights reserved
  8. //
  9. // Main tuneup application file.
  10. //
  11. // Original engineer: WillisC
  12. // Updated: Jason Cohen (JCOHEN)
  13. //
  14. //////////////////////////////////////////////////////////////////////////////
  15. //
  16. // Internal include file(s).
  17. //
  18. #include <windows.h>
  19. #include <tchar.h>
  20. #include "tasks.h"
  21. #include "schedwiz.h"
  22. #include "howtorun.h"
  23. #include "wizard.h"
  24. #include "scm.h"
  25. //
  26. // Global variable(s).
  27. //
  28. HWND g_hWnd;
  29. HINSTANCE g_hInst;
  30. DWORD g_dwFlags;
  31. LPTASKDATA g_Tasks;
  32. LPTASKDATA g_CurrentTask;
  33. INT g_nTimeScheme;
  34. TCHAR g_szAppName[64];
  35. //
  36. // Internal (static) function prototype(s).
  37. //
  38. static HANDLE GetMutex(VOID);
  39. static HWND CheckPrevInst(LPHANDLE);
  40. static BOOL TanslateCommandLine(LPTSTR);
  41. //
  42. // Main windows function.
  43. //
  44. INT PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
  45. {
  46. HANDLE hMutex;
  47. HWND hWnd;
  48. LPTSTR *lpArgs = NULL;
  49. DWORD dwArgs,
  50. dwIndex;
  51. BOOL bRunWizard;
  52. // First we will init any global memory we need to.
  53. //
  54. g_hInst = hInstance;
  55. g_nTimeScheme = 0;
  56. g_dwFlags = 0;
  57. LoadString(hInstance, IDS_TUNEUP, g_szAppName, sizeof(g_szAppName) / sizeof(TCHAR));
  58. // This will setup any command line options first.
  59. //
  60. dwArgs = GetCommandLineOptions(&lpArgs);
  61. for (dwIndex = 1; dwIndex < dwArgs; dwIndex++)
  62. TanslateCommandLine((LPTSTR) *(lpArgs + dwIndex));
  63. FREE(lpArgs);
  64. // If we proccessed a command line option that is designed to
  65. // not run tuneup, we should return now.
  66. //
  67. if ( g_dwFlags & TUNEUP_QUIT )
  68. return TRUE;
  69. // Now check for another instance of Tuneup.
  70. //
  71. if ( hWnd = CheckPrevInst(&hMutex) )
  72. {
  73. SetForegroundWindow(hWnd);
  74. return FALSE;
  75. }
  76. // We now need to create the job tasks.
  77. //
  78. g_Tasks = CreateTasks();
  79. InitJobs(g_Tasks);
  80. // Now we should pick up any data from the registry that we need.
  81. //
  82. if ( RegExists(HKLM, g_szTuneupKey, g_szRegValFirstTime) )
  83. {
  84. // Get previous time scheme.
  85. //
  86. g_nTimeScheme = RegGetDword(HKLM, g_szTuneupKey, g_szRegValTime);
  87. // Get previous express or custom.
  88. //
  89. if ( RegCheck(HKLM, g_szTuneupKey, g_szRegValCustom) )
  90. g_dwFlags |= TUNEUP_CUSTOM;
  91. // Display the how to run dialog. If TRUE is
  92. // returned, we should run the wizard. Otherwise
  93. // the user choose cancel or the tasks were all run
  94. // and we should just return.
  95. //
  96. bRunWizard = HowToRun();
  97. }
  98. else
  99. {
  100. // Set the flag so that tuneup knows this is
  101. // the first time it has been run.
  102. //
  103. g_dwFlags |= TUNEUP_NOSCHEDULE;
  104. // Set this so that we do run the wizard.
  105. //
  106. bRunWizard = TRUE;
  107. }
  108. // Now set any default values that should be set.
  109. //
  110. if ( g_nTimeScheme == 0)
  111. g_nTimeScheme = DEFAULT_TIMESCHEME;
  112. // Start the wizard if we need to.
  113. //
  114. if ( bRunWizard )
  115. CreateWizard(hInstance, NULL);
  116. // Free the job tasks we created.
  117. //
  118. FreeTasks(g_Tasks);
  119. // Release the mutex.
  120. //
  121. if (hMutex)
  122. ReleaseMutex(hMutex);
  123. return TRUE;
  124. }
  125. //
  126. // Internal function(s).
  127. //
  128. static HANDLE GetMutex()
  129. {
  130. const TCHAR szTuneupMutex[] = _T("TUNEUP97MUTEX");
  131. HANDLE hMutex;
  132. // Try to open the Tuneup mutex.
  133. //
  134. if ( hMutex = OpenMutex(SYNCHRONIZE, FALSE, szTuneupMutex) )
  135. {
  136. // There is already another mutex, so we can't get one
  137. // and there should be another Tuneup window to find.
  138. //
  139. CloseHandle(hMutex);
  140. hMutex = NULL;
  141. }
  142. else
  143. {
  144. // No mutex is present, so we should be able to get one.
  145. //
  146. hMutex = CreateMutex(NULL, FALSE, szTuneupMutex);
  147. }
  148. // Return the mutex, will be NULL if there alread is one.
  149. //
  150. return hMutex;
  151. }
  152. static HWND CheckPrevInst(LPHANDLE hMutex)
  153. {
  154. HWND hWnd = NULL;
  155. BOOL bPrev = FALSE;
  156. int nCount;
  157. // Loop until another tuneup window is found or a mutex can
  158. // be created. Max of 10 times (10 seconds).
  159. //
  160. for (nCount = 0; (nCount < 10) && (!bPrev) && ( (*hMutex = GetMutex()) == NULL ); nCount++)
  161. {
  162. #if 0
  163. // Check for the background window of the wizard.
  164. //
  165. if ( bPrev = ( ( hWnd = FindWindow(NULL, g_szAppName) ) != NULL ) )
  166. {
  167. // The background window was found, so show the prev one (i.e. Wizard).
  168. //
  169. hWnd = GetWindow(hWnd, GW_HWNDPREV);
  170. }
  171. else
  172. {
  173. // Check for the how to run dialog.
  174. //
  175. bPrev = ( ( hWnd = FindWindow(NULL, g_szAppName) ) != NULL );
  176. }
  177. #else
  178. bPrev = ( ( hWnd = FindWindow(NULL, g_szAppName) ) != NULL );
  179. #endif
  180. // If we found no window, sleep for 1 second.
  181. //
  182. if (!bPrev)
  183. Sleep(1000);
  184. }
  185. // Return the window handle of any previous instance (will be null if we
  186. // didn't find or we got a mutex).
  187. //
  188. return hWnd;
  189. }
  190. static BOOL TanslateCommandLine(LPTSTR lpArg)
  191. {
  192. BOOL bTranslated = TRUE;
  193. // Check for the /AUTORUN flag.
  194. //
  195. if ( _tcsicmp(_T("/AUTORUN"), lpArg) == 0 )
  196. g_dwFlags |= TUNEUP_AUTORUN;
  197. // Check for the /SERVICE: flag.
  198. //
  199. else if ( _tcsnicmp(_T("/SERVICE:"), lpArg, 9) == 0 )
  200. {
  201. ServiceStart(lpArg + 9);
  202. g_dwFlags |= TUNEUP_QUIT;
  203. }
  204. // Unknown option.
  205. //
  206. else
  207. bTranslated = FALSE;
  208. return bTranslated;
  209. }