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.

311 lines
7.6 KiB

  1. #include "migwiz.h"
  2. #include "migutil.h"
  3. #include "resource.h"
  4. #include "container.h"
  5. #include <string.h>
  6. #include <tchar.h>
  7. typedef struct _THREADSTARTUPINFO
  8. {
  9. HWND hWnd;
  10. HINSTANCE hInstance;
  11. LPTSTR lpCmdLine;
  12. } THREADSTARTUPINFO, *LPTHREADSTARTUPINFO;
  13. #define WINDOWCLASS TEXT("USMTCobraApp")
  14. #define WINDOWNAME TEXT("Migwiz")
  15. #define ANOTHERUSER_RESLEN 100
  16. BOOL g_ConfirmedLogOff = FALSE;
  17. BOOL g_ConfirmedReboot = FALSE;
  18. static HANDLE g_hThread = NULL;
  19. static DWORD g_dwThreadId = 0;
  20. Container *g_WebContainer = NULL;
  21. DWORD
  22. WINAPI
  23. MigwizThread(
  24. IN LPVOID lpParameter
  25. )
  26. {
  27. HRESULT hr;
  28. TCHAR szAppPath[MAX_PATH] = TEXT("");
  29. TCHAR* pszAppPathOffset;
  30. CoInitialize(NULL);
  31. OleInitialize(NULL);
  32. GetModuleFileName (NULL, szAppPath, ARRAYSIZE(szAppPath));
  33. szAppPath [ARRAYSIZE(szAppPath) - 1] = 0;
  34. pszAppPathOffset = _tcsrchr (szAppPath, TEXT('\\'));
  35. if (pszAppPathOffset) {
  36. *pszAppPathOffset = 0;
  37. SetCurrentDirectory (szAppPath);
  38. }
  39. MigrationWizard* pMigWiz = new MigrationWizard();
  40. if (pMigWiz)
  41. {
  42. LPTSTR lpszUsername = NULL;
  43. LPTSTR lpszCmdLine = ((LPTHREADSTARTUPINFO)lpParameter)->lpCmdLine;
  44. if (lpszCmdLine && 0 == _tcsncmp (lpszCmdLine, TEXT("/t:"), 3))
  45. {
  46. // BUGBUG: need to make this more stable
  47. lpszUsername = lpszCmdLine + 3;
  48. }
  49. hr = pMigWiz->Init(((LPTHREADSTARTUPINFO)lpParameter)->hInstance, lpszUsername);
  50. if (SUCCEEDED(hr))
  51. {
  52. pMigWiz->Execute();
  53. }
  54. // ISSUE: leak
  55. //delete pMigWiz;
  56. }
  57. SendMessage (((LPTHREADSTARTUPINFO)lpParameter)->hWnd, WM_USER_THREAD_COMPLETE, NULL, NULL);
  58. OleUninitialize();
  59. CoUninitialize();
  60. return 0;
  61. }
  62. BOOL
  63. CALLBACK
  64. _SetMigwizActive(
  65. IN HWND hWnd,
  66. IN LPARAM lParam
  67. )
  68. {
  69. SetForegroundWindow( hWnd );
  70. return FALSE;
  71. }
  72. LRESULT CALLBACK WndProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  73. {
  74. switch (message)
  75. {
  76. case WM_ACTIVATE:
  77. if (g_hThread != NULL && g_dwThreadId != 0)
  78. {
  79. EnumThreadWindows( g_dwThreadId, _SetMigwizActive, (LPARAM)NULL );
  80. }
  81. break;
  82. case WM_USER_THREAD_COMPLETE:
  83. CloseHandle( g_hThread );
  84. PostQuitMessage( 0 );
  85. break;
  86. default:
  87. return DefWindowProc( hWnd, message, wParam, lParam );
  88. }
  89. return 0;
  90. }
  91. LRESULT CALLBACK WebHostProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  92. {
  93. switch (message)
  94. {
  95. case WM_SETFOCUS:
  96. if (g_WebContainer) {
  97. g_WebContainer->setFocus(TRUE);
  98. return 0;
  99. }
  100. break;
  101. }
  102. return DefWindowProc(hWnd, message, wParam, lParam);
  103. }
  104. INT
  105. WINAPI
  106. WinMain(
  107. IN HINSTANCE hInstance,
  108. IN HINSTANCE hPrevInstance,
  109. IN LPSTR lpszCmdLine,
  110. IN INT nCmdShow)
  111. {
  112. WNDCLASSEX wcx;
  113. MSG msg;
  114. HANDLE hMutex = NULL;
  115. OSVERSIONINFO vi;
  116. HWND hwnd;
  117. DWORD dwResult = 0;
  118. THREADSTARTUPINFO StartInfo;
  119. PSID pSid = NULL;
  120. PTSTR commandLine = NULL;
  121. #ifdef UNICODE
  122. commandLine = _ConvertToUnicode (CP_ACP, lpszCmdLine);
  123. #else
  124. commandLine = lpszCmdLine;
  125. #endif
  126. hwnd = FindWindow (WINDOWCLASS, WINDOWNAME);
  127. if (hwnd)
  128. {
  129. SetForegroundWindow (hwnd);
  130. goto END;
  131. }
  132. CoInitialize(NULL);
  133. OleInitialize(NULL);
  134. vi.dwOSVersionInfoSize = sizeof (vi);
  135. if (GetVersionEx (&vi) &&
  136. vi.dwPlatformId == VER_PLATFORM_WIN32_NT &&
  137. vi.dwMajorVersion > 4)
  138. {
  139. hMutex = CreateMutex (NULL, TRUE, TEXT("Global\\migwiz.mutex"));
  140. }
  141. else
  142. {
  143. hMutex = CreateMutex (NULL, TRUE, TEXT("migwiz.mutex"));
  144. }
  145. if ((hMutex && GetLastError() == ERROR_ALREADY_EXISTS) ||
  146. (!hMutex && GetLastError() == ERROR_ACCESS_DENIED))
  147. {
  148. TCHAR szTmpBuf[512];
  149. PVOID lpBuf = NULL;
  150. LPTSTR lpUsername = NULL;
  151. lpUsername = (LPTSTR)LocalAlloc(LPTR, ANOTHERUSER_RESLEN * sizeof (TCHAR));
  152. LoadString (hInstance, IDS_ANOTHER_USER, lpUsername, ANOTHERUSER_RESLEN);
  153. LoadString (hInstance, IDS_ALREADY_RUN_USER, szTmpBuf, ARRAYSIZE(szTmpBuf));
  154. FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_ARGUMENT_ARRAY,
  155. szTmpBuf,
  156. 0,
  157. 0,
  158. (LPTSTR)&lpBuf,
  159. 0,
  160. (va_list *)(&lpUsername));
  161. MessageBox (NULL, (LPCTSTR)lpBuf, NULL, MB_OK);
  162. LocalFree (lpBuf);
  163. LocalFree (lpUsername);
  164. goto END;
  165. }
  166. ZeroMemory (&wcx, sizeof (WNDCLASSEX));
  167. wcx.cbSize = sizeof (WNDCLASSEX);
  168. wcx.hInstance = hInstance;
  169. wcx.lpszClassName = WINDOWCLASS;
  170. wcx.lpfnWndProc = WndProc;
  171. if (!RegisterClassEx (&wcx))
  172. {
  173. dwResult = GetLastError();
  174. goto END;
  175. }
  176. ZeroMemory (&wcx, sizeof (WNDCLASSEX));
  177. wcx.cbSize = sizeof (WNDCLASSEX);
  178. wcx.hInstance = hInstance;
  179. wcx.lpszClassName = TEXT("WebHost");
  180. wcx.lpfnWndProc = WebHostProc;
  181. if (!RegisterClassEx (&wcx))
  182. {
  183. dwResult = GetLastError();
  184. goto END;
  185. }
  186. hwnd = CreateWindow (WINDOWCLASS,
  187. WINDOWNAME,
  188. WS_OVERLAPPEDWINDOW,
  189. CW_USEDEFAULT,
  190. CW_USEDEFAULT,
  191. 400, 300,
  192. NULL, NULL,
  193. hInstance,
  194. NULL);
  195. if (!hwnd)
  196. {
  197. dwResult = GetLastError();
  198. goto END;
  199. }
  200. StartInfo.hWnd = hwnd;
  201. StartInfo.hInstance = hInstance;
  202. StartInfo.lpCmdLine = commandLine;
  203. g_hThread = CreateThread( NULL,
  204. 0,
  205. MigwizThread,
  206. (PVOID)&StartInfo,
  207. 0,
  208. &g_dwThreadId );
  209. if (g_hThread == NULL)
  210. {
  211. dwResult = GetLastError();
  212. goto END;
  213. }
  214. // Main message loop:
  215. while (GetMessage( &msg, NULL, 0, 0 ))
  216. {
  217. TranslateMessage( &msg );
  218. DispatchMessage( &msg );
  219. }
  220. END:
  221. OleUninitialize();
  222. CoUninitialize();
  223. if (hMutex)
  224. {
  225. CloseHandle (hMutex);
  226. }
  227. if (pSid)
  228. {
  229. LocalFree (pSid);
  230. }
  231. #ifdef UNICODE
  232. if (commandLine)
  233. {
  234. LocalFree (commandLine);
  235. }
  236. #endif
  237. if (g_ConfirmedReboot) {
  238. HANDLE token;
  239. TOKEN_PRIVILEGES newPrivileges;
  240. LUID luid;
  241. if (OpenProcessToken (GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) {
  242. if (LookupPrivilegeValue (NULL, SE_SHUTDOWN_NAME, &luid)) {
  243. newPrivileges.PrivilegeCount = 1;
  244. newPrivileges.Privileges[0].Luid = luid;
  245. newPrivileges.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  246. if (AdjustTokenPrivileges(
  247. token,
  248. FALSE,
  249. &newPrivileges,
  250. 0,
  251. NULL,
  252. NULL
  253. )) {
  254. ExitWindowsEx (EWX_REBOOT, 0);
  255. }
  256. }
  257. CloseHandle (token);
  258. }
  259. } else if (g_ConfirmedLogOff) {
  260. ExitWindowsEx (EWX_LOGOFF, 0);
  261. }
  262. return dwResult;
  263. }