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.

449 lines
13 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997.
  5. //
  6. // File: OneStop.cpp
  7. //
  8. // Contents: Main application
  9. //
  10. // Classes:
  11. //
  12. // Notes:
  13. //
  14. // History: 05-Nov-97 rogerg Created.
  15. //
  16. //--------------------------------------------------------------------------
  17. #include "precomp.h"
  18. // Global Variables:
  19. HINSTANCE g_hInst = NULL; // current instance
  20. LANGID g_LangIdSystem; // LangId of system we are running on.
  21. DWORD g_WMTaskbarCreated; // TaskBar Created WindowMessage;
  22. // Forward declarations of functions included in this code module:
  23. BOOL InitApplication(HINSTANCE);
  24. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow,CMsgServiceHwnd *pMsgService);
  25. void UnitApplication();
  26. BOOL SetupUserEnvironment();
  27. //+---------------------------------------------------------------------------
  28. //
  29. // Function: WinMain, public
  30. //
  31. // Synopsis: The start of all things
  32. //
  33. // Arguments: Standard WinMain.
  34. //
  35. // Returns:
  36. //
  37. // Modifies:
  38. //
  39. // History: 18-Nov-97 rogerg Created.
  40. //
  41. //----------------------------------------------------------------------------
  42. #ifdef _DEBUG
  43. extern DWORD g_ThreadCount;
  44. #endif // _DEBUG
  45. int APIENTRY WinMain(HINSTANCE hInstance,
  46. HINSTANCE hPrevInstance,
  47. LPSTR lpCmdLine,
  48. int nCmdShow)
  49. {
  50. MSG msg;
  51. CMsgServiceHwnd *pMsgService;
  52. BOOL fMsgServiceCreated = FALSE;
  53. HRESULT hr;
  54. g_hInst = hInstance; // Store instance handle in our global variable
  55. #ifdef _DEBUG
  56. InitDebugFlags();
  57. #endif // _DEBUG
  58. InitCommonLib();
  59. g_LangIdSystem = GetSystemDefaultLangID();
  60. SetupUserEnvironment();
  61. if (!hPrevInstance)
  62. {
  63. // Perform instance initialization:
  64. if (!InitApplication(hInstance))
  65. {
  66. return (FALSE);
  67. }
  68. }
  69. hr = CoInitializeEx(NULL,COINIT_MULTITHREADED | COINIT_DISABLE_OLE1DDE); // main thread is freeThreaded
  70. if (FAILED(hr))
  71. {
  72. AssertSz(0,"CoInitFailed");
  73. return FALSE;
  74. }
  75. // Initialize the Message Service for all threads
  76. if (S_OK != InitMessageService())
  77. {
  78. AssertSz(0,"Unable to Init Message Service");
  79. return FALSE;
  80. }
  81. // crate a MessageService for main thread
  82. pMsgService = new CMsgServiceHwnd;
  83. if (pMsgService)
  84. {
  85. fMsgServiceCreated = pMsgService->Initialize(GetCurrentThreadId(),MSGHWNDTYPE_MAINTHREAD);
  86. Assert(fMsgServiceCreated);
  87. }
  88. if (fMsgServiceCreated && InitInstance(hInstance, nCmdShow,pMsgService))
  89. {
  90. // hAccelTable = LoadAccelerators (hInstance, APPNAME); // Currently don't have any accelerators
  91. // Main message loop:
  92. while (GetMessage(&msg, NULL, 0, 0))
  93. {
  94. TranslateMessage(&msg);
  95. DispatchMessage(&msg);
  96. }
  97. }
  98. else
  99. {
  100. msg.wParam = 0;
  101. if (pMsgService) // get rid of this threads messageService
  102. pMsgService->Destroy();
  103. }
  104. UnitApplication(); // unitialize application.
  105. return (int)(msg.wParam);
  106. lpCmdLine; // This will prevent 'unused formal parameter' warnings
  107. }
  108. //+---------------------------------------------------------------------------
  109. //
  110. // Function: InitApplication, public
  111. //
  112. // Synopsis: Peforms any application specific tasks
  113. //
  114. // Arguments: [hInstance] - hInstance.
  115. //
  116. // Returns:
  117. //
  118. // Modifies:
  119. //
  120. // History: 18-Nov-97 rogerg Created.
  121. //
  122. //----------------------------------------------------------------------------
  123. BOOL InitApplication(HINSTANCE hInstance)
  124. {
  125. return TRUE;
  126. }
  127. //+---------------------------------------------------------------------------
  128. //
  129. // Function: UnitApplication, public
  130. //
  131. // Synopsis: Peforms any application specific cleanup
  132. //
  133. // Arguments:
  134. //
  135. // Returns:
  136. //
  137. // Modifies:
  138. //
  139. // History: 19-Jun-98 rogerg Created.
  140. //
  141. //----------------------------------------------------------------------------
  142. void UnitApplication()
  143. {
  144. ReleaseConnectionObjects(); // release global connection object class.
  145. gSingleNetApiObj.DeleteNetApiObj(); // get rid of globa NetObj
  146. Assert(g_ThreadCount == 0); // make sure all our threads are cleaned up.
  147. CoFreeUnusedLibraries();
  148. CoUninitialize();
  149. UnInitCommonLib();
  150. }
  151. //+---------------------------------------------------------------------------
  152. //
  153. // Function: InitInstance, public
  154. //
  155. // Synopsis: Peforms instance specific initialization
  156. //
  157. // Arguments: [hInstance] - hInstance.
  158. // [nCmdShow] - value to start windows as
  159. // [pMsgService] - Message service for this instance
  160. //
  161. // Returns: TRUE will put application into a message loop
  162. // FALSE will cause application to terminate immediately
  163. //
  164. // Modifies:
  165. //
  166. // History: 18-Nov-97 rogerg Created.
  167. //
  168. //----------------------------------------------------------------------------
  169. BOOL InitInstance(HINSTANCE hInstance, int nCmdShow,CMsgServiceHwnd *pMsgService)
  170. {
  171. HRESULT hr;
  172. CCmdLine cmdLine;
  173. DWORD cmdLineFlags;
  174. BOOL fEmbeddingFlag;
  175. ATOM aWndClass;
  176. g_WMTaskbarCreated = RegisterWindowMessage(L"TaskbarCreated"); // get taskbar created message.
  177. cmdLine.ParseCommandLine();
  178. cmdLineFlags = cmdLine.GetCmdLineFlags();
  179. fEmbeddingFlag = cmdLineFlags & CMDLINE_COMMAND_EMBEDDING;
  180. // register a windows class to store the icon for the OneStop dialogs
  181. // get an icon for the dialog
  182. WNDCLASS wc;
  183. wc.style = CS_DBLCLKS | CS_SAVEBITS | CS_BYTEALIGNWINDOW;
  184. wc.lpfnWndProc = DefDlgProcW;
  185. // WRAPPER, need wrappers for LoadIcon/LoadCursor.
  186. wc.cbClsExtra = 0;
  187. wc.cbWndExtra = DLGWINDOWEXTRA;
  188. wc.hInstance = g_hInst;
  189. wc.hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_SYNCMGR));
  190. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  191. wc.hbrBackground = (HBRUSH) (COLOR_WINDOW + 1);
  192. wc.lpszMenuName = NULL;
  193. wc.lpszClassName = TEXT(MAINDIALOG_HWNDCLASSNAME);
  194. aWndClass = RegisterClass(&wc);
  195. Assert(aWndClass);
  196. // if register flag is passed, just register and return.
  197. if (cmdLineFlags & CMDLINE_COMMAND_REGISTER)
  198. {
  199. AssertSz(0,"SyncMgr launched with obsolete /register flag.");
  200. return FALSE;
  201. }
  202. INITCOMMONCONTROLSEX controlsEx;
  203. controlsEx.dwSize = sizeof(INITCOMMONCONTROLSEX);
  204. controlsEx.dwICC = ICC_USEREX_CLASSES | ICC_WIN95_CLASSES | ICC_NATIVEFNTCTL_CLASS;
  205. InitCommonControlsEx(&controlsEx);
  206. hr = InitObjectManager(pMsgService); // Initialize Object manager
  207. if (S_OK != hr)
  208. {
  209. Assert(S_OK == hr);
  210. return FALSE;
  211. }
  212. hr = InitConnectionObjects(); // initialize connection objects.
  213. if (S_OK != hr)
  214. {
  215. Assert(S_OK == hr);
  216. return FALSE;
  217. }
  218. // If the embedding flag is set force a Register.
  219. // Review - Activate as Interactive user doesn't work for Logoff
  220. // don't register class factory for schedules or Idle since if an error occurs
  221. // we want TS to launch us again next time it is time for the
  222. // schedule to Fire.
  223. if (!(cmdLineFlags & CMDLINE_COMMAND_SCHEDULE) && !(cmdLineFlags & CMDLINE_COMMAND_IDLE))
  224. {
  225. hr = RegisterOneStopClassFactory(fEmbeddingFlag);
  226. }
  227. else
  228. {
  229. hr = S_OK;
  230. }
  231. // if didn't force a register then continue on our journey
  232. // and wait to fail until CoCreateInstance does
  233. Assert( (S_OK == hr) || !fEmbeddingFlag);
  234. if (S_OK == hr || !fEmbeddingFlag )
  235. {
  236. // if only /Embedding is specified we were launch to service someone else so
  237. // dont' do anything, just wait for them to connect.
  238. if (!fEmbeddingFlag)
  239. {
  240. LPPRIVSYNCMGRSYNCHRONIZEINVOKE pUnk;
  241. // If there are other command lines or known to the proper thing. (manual, schedule, etc.).
  242. // addref our lifetime in case update doesn't take, treat as external
  243. // since invoke can go to another process.
  244. AddRefOneStopLifetime(TRUE /*External*/);
  245. // if class factory registered successful then CoCreate
  246. if (SUCCEEDED(hr))
  247. {
  248. // CODE REVIEW:
  249. // NOTENOTE:
  250. // Is this a valid use of CLSCTX_ALL ?
  251. hr = CoCreateInstance(CLSID_SyncMgrp,NULL,CLSCTX_ALL,IID_IPrivSyncMgrSynchronizeInvoke,(void **) &pUnk);
  252. }
  253. // if have failure either from class factory or CoCreateIntance
  254. // then create a class directlry.
  255. if (FAILED(hr))
  256. {
  257. // this is really an error path that shouldn't happen.
  258. // AssertSz(SUCCEEDED(hr),"COM Activation Failed");
  259. // If COM Activation Fails go ahead and create a class directly
  260. // unless it is a schedule or idle event.
  261. if ( !(cmdLineFlags & CMDLINE_COMMAND_SCHEDULE) && !(cmdLineFlags & CMDLINE_COMMAND_IDLE) )
  262. {
  263. pUnk = new CSynchronizeInvoke;
  264. hr = pUnk ? S_OK : E_OUTOFMEMORY;
  265. // Assert(S_OK == hr);
  266. }
  267. }
  268. if (S_OK == hr)
  269. {
  270. AllowSetForegroundWindow(ASFW_ANY); // let mobsync.exe come to front if necessary
  271. if (cmdLineFlags & CMDLINE_COMMAND_LOGON)
  272. {
  273. pUnk->Logon();
  274. }
  275. else if (cmdLineFlags & CMDLINE_COMMAND_LOGOFF)
  276. {
  277. pUnk->Logoff();
  278. }
  279. else if (cmdLineFlags & CMDLINE_COMMAND_SCHEDULE)
  280. {
  281. pUnk->Schedule(cmdLine.GetJobFile());
  282. }
  283. else if (cmdLineFlags & CMDLINE_COMMAND_IDLE)
  284. {
  285. pUnk->Idle();
  286. }
  287. else
  288. {
  289. // default is a manual sync
  290. pUnk->UpdateAll();
  291. }
  292. pUnk->Release();
  293. }
  294. else
  295. {
  296. // AssertSz(0,"Unable to Create Invoke Instance");
  297. }
  298. ReleaseOneStopLifetime(TRUE /*External*/); // Release our reference.
  299. }
  300. return TRUE; // even on failure return true, locking will take care of releasing object.
  301. }
  302. return (FALSE); // if couldn't forward the update then end.
  303. }
  304. //+---------------------------------------------------------------------------
  305. //
  306. // Function: SetupUserEnvironment,private
  307. //
  308. // Synopsis: Sets up any use environment variables we need to run.
  309. //
  310. // When we are launched as interactive User by DCOM the
  311. // environment variables aren't set so we need to set
  312. // any up that us or the handlers rely on.
  313. //
  314. // Arguments:
  315. //
  316. // Returns:
  317. //
  318. // Modifies:
  319. //
  320. // History: 14-Aug-98 rogerg Created.
  321. //
  322. //----------------------------------------------------------------------------
  323. #define SZ_ENVIRONVARIABLE_USERPROFILE TEXT("USERPROFILE")
  324. #define SZ_ENVIRONVARIABLE_USERNAME TEXT("USERNAME")
  325. BOOL SetupUserEnvironment()
  326. {
  327. HANDLE hToken = NULL;
  328. BOOL fValidToken;
  329. BOOL fSetEnviron = FALSE;
  330. BOOL fSetUserName = FALSE;
  331. // setup the User Profile Dir
  332. fValidToken = TRUE;
  333. if (!OpenThreadToken (GetCurrentThread(), TOKEN_READ,TRUE, &hToken))
  334. {
  335. if (!OpenProcessToken(GetCurrentProcess(), TOKEN_READ, &hToken))
  336. {
  337. AssertSz(0,"Failed to GetToken");
  338. fValidToken = FALSE;
  339. }
  340. }
  341. if (fValidToken)
  342. {
  343. DWORD cbSize;
  344. // Call GetUserProfile once for Size and then again for real allocation
  345. cbSize = 0;
  346. GetUserProfileDirectory(hToken,NULL,&cbSize);
  347. if (cbSize > 0)
  348. {
  349. WCHAR *pwszProfileDir = (WCHAR *) ALLOC(cbSize*sizeof(WCHAR));
  350. if (pwszProfileDir && GetUserProfileDirectory(hToken,pwszProfileDir,&cbSize))
  351. {
  352. fSetEnviron = SetEnvironmentVariable(SZ_ENVIRONVARIABLE_USERPROFILE,pwszProfileDir);
  353. }
  354. if (pwszProfileDir)
  355. {
  356. FREE(pwszProfileDir);
  357. }
  358. }
  359. Assert(fSetEnviron); // assert if anything fails when we have a valid token
  360. CloseHandle(hToken);
  361. }
  362. // setup the UserName
  363. TCHAR szBuffer[UNLEN + 1];
  364. DWORD dwBufSize = ARRAYSIZE(szBuffer);
  365. if (GetUserName(szBuffer,&dwBufSize))
  366. {
  367. fSetUserName = SetEnvironmentVariable(SZ_ENVIRONVARIABLE_USERNAME,szBuffer);
  368. Assert(fSetUserName);
  369. }
  370. return (fSetEnviron && fSetUserName);
  371. }