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.

329 lines
9.6 KiB

  1. //----------------------------------------------
  2. //
  3. // 16 bit stub to run mmc.exe with parameters
  4. //
  5. //----------------------------------------------
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include <io.h>
  9. #include <string.h>
  10. #include <direct.h>
  11. #include <windows.h>
  12. #include <shellapi.h> // 16-bit Windows header
  13. #include "wownt16.h" // available from Win32 SDK
  14. #include "resource.h"
  15. #define FILE_TO_RUN "mmc.exe"
  16. #define FILE_TO_RUN_FILE_PARAM "iis.msc"
  17. #define REG_PRODUCT_KEY "SYSTEM\\CurrentControlSet\\Control\\ProductOptions"
  18. /* ************************ prototypes ***************************** */
  19. int RunTheApp(void);
  20. int HasTheAppStarted(void);
  21. int CheckIfFileExists(char *input_filespec);
  22. void PopUpUnableToSomething(char[], int);
  23. void AddPath(LPSTR szPath, LPCSTR szName );
  24. LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);
  25. /* ************************* globals ******************************* */
  26. HANDLE g_hInstance;
  27. HANDLE g_hPrevInstance;
  28. LPSTR g_lpCmdLine;
  29. int g_nCmdShow;
  30. char g_szTime[100] = "";
  31. UINT g_WinExecReturn;
  32. char g_szWinExecModuleName[260];
  33. char g_szMsg[_MAX_PATH];
  34. char g_szSystemDir[_MAX_PATH];
  35. char g_szSystemDir32[_MAX_PATH];
  36. /* **************************************************************** */
  37. int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  38. {
  39. HWND hwnd;
  40. MSG msg;
  41. WNDCLASS wcl;
  42. char szWinName[_MAX_PATH];
  43. char szBuf[_MAX_PATH];
  44. DWORD dwRet;
  45. g_hInstance = hInstance;
  46. g_hPrevInstance = hPrevInstance;
  47. g_lpCmdLine = lpCmdLine;
  48. g_nCmdShow = nCmdShow;
  49. LoadString( g_hInstance, IDS_TITLE, szWinName, _MAX_PATH );
  50. // note that this will come back as "system" <-- must be because this is a 16bit app
  51. dwRet = GetSystemDirectory( szBuf, sizeof(szBuf) - sizeof("32") );
  52. if ( ( dwRet == 0 ) ||
  53. ( dwRet > ( sizeof(szBuf) - sizeof("32") ) )
  54. )
  55. {
  56. return 0;
  57. }
  58. lstrcpy(g_szSystemDir, szBuf);
  59. lstrcat(g_szSystemDir, "32");
  60. // set to system if can't find system32 directory
  61. if ( CheckIfFileExists( g_szSystemDir ) == FALSE )
  62. {
  63. lstrcpy(g_szSystemDir, szBuf);
  64. }
  65. // define windows class
  66. wcl.hInstance = hInstance;
  67. wcl.lpszClassName = szWinName;
  68. wcl.lpfnWndProc = WindowFunc;
  69. wcl.style = 0;
  70. wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  71. wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
  72. wcl.lpszMenuName = NULL;
  73. wcl.cbClsExtra = 0;
  74. wcl.cbWndExtra = 0;
  75. wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  76. // register the window class.
  77. if (!RegisterClass (&wcl)) return 0;
  78. //hwnd = CreateWindow(szWinName, NULL, WS_DLGFRAME, CW_USEDEFAULT, CW_USEDEFAULT, window_h, window_v, HWND_DESKTOP, NULL, hInstance , NULL);
  79. hwnd = CreateWindow(szWinName, NULL, WS_DISABLED | WS_CHILD, CW_USEDEFAULT, CW_USEDEFAULT, 10, 10, HWND_DESKTOP, NULL, hInstance , NULL);
  80. // display the window
  81. ShowWindow(hwnd, nCmdShow);
  82. // Start a timer -- interrupt once for 1 seconds
  83. SetTimer(hwnd, 1, 500, NULL);
  84. UpdateWindow(hwnd);
  85. // Return true only if we are able to start the setup program and run it.
  86. if (!RunTheApp()) {return FALSE;}
  87. // Check if the process has started by checking for
  88. // the window that should be run...
  89. if (HasTheAppStarted()) {PostQuitMessage(0);}
  90. while(GetMessage(&msg, NULL, 0, 0))
  91. {
  92. TranslateMessage(&msg);
  93. DispatchMessage(&msg);
  94. }
  95. KillTimer(hwnd, 1);
  96. return (int)(msg.wParam);
  97. }
  98. //***************************************************************************
  99. //*
  100. //* purpose: you know what
  101. //*
  102. //***************************************************************************
  103. LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  104. {
  105. switch(message)
  106. {
  107. case WM_TIMER:
  108. // Check if the process has started by checking for
  109. // the window that should be run...
  110. if (HasTheAppStarted()) {PostQuitMessage(0);}
  111. break;
  112. case WM_CREATE:
  113. break;
  114. case WM_PAINT:
  115. break;
  116. case WM_DESTROY:
  117. PostQuitMessage(0);
  118. break;
  119. default:
  120. return DefWindowProc(hwnd,message,wParam, lParam);
  121. }
  122. return 0;
  123. }
  124. //***************************************************************************
  125. //*
  126. //* purpose: return TRUE if the window has started
  127. //*
  128. //***************************************************************************
  129. int RunTheApp(void)
  130. {
  131. char szIISInstalledPath[_MAX_PATH];
  132. char szCommandToRun[_MAX_PATH + _MAX_PATH + 50];
  133. char szTempFilePath[_MAX_PATH + sizeof( FILE_TO_RUN ) ];
  134. // check if our files exist...
  135. lstrcpy(szTempFilePath, g_szSystemDir);
  136. AddPath(szTempFilePath, FILE_TO_RUN);
  137. if (CheckIfFileExists(szTempFilePath) == FALSE)
  138. {
  139. PopUpUnableToSomething(szTempFilePath, IDS_UNABLE_TO_FIND);
  140. return FALSE;
  141. }
  142. // get iis installed directory
  143. LoadString( g_hInstance, IDS_INETSRV_INSTALLED_DIR, szIISInstalledPath, _MAX_PATH);
  144. if ( ( strlen(g_szSystemDir) +
  145. strlen(szIISInstalledPath) +
  146. strlen(FILE_TO_RUN_FILE_PARAM)
  147. ) >= sizeof(szTempFilePath) )
  148. {
  149. return FALSE;
  150. }
  151. lstrcpy(szTempFilePath, g_szSystemDir);
  152. AddPath(szTempFilePath, szIISInstalledPath);
  153. AddPath(szTempFilePath, FILE_TO_RUN_FILE_PARAM);
  154. if (CheckIfFileExists(szTempFilePath) == FALSE)
  155. {
  156. PopUpUnableToSomething(szTempFilePath, IDS_UNABLE_TO_FIND);
  157. return FALSE;
  158. }
  159. // Create a command line
  160. //%SystemRoot%\System32\mmc.exe D:\WINNT0\System32\inetsrv\iis.msc
  161. if ( ( strlen( g_szSystemDir ) +
  162. strlen( FILE_TO_RUN ) +
  163. strlen( " " ) +
  164. strlen( g_szSystemDir ) +
  165. strlen( szIISInstalledPath ) +
  166. strlen( FILE_TO_RUN_FILE_PARAM ) +
  167. 1 ) > sizeof(szCommandToRun) )
  168. {
  169. return FALSE;
  170. }
  171. lstrcpy(szCommandToRun, g_szSystemDir);
  172. AddPath(szCommandToRun, FILE_TO_RUN);
  173. lstrcat(szCommandToRun, " ");
  174. lstrcat(szCommandToRun, g_szSystemDir);
  175. AddPath(szCommandToRun, szIISInstalledPath);
  176. AddPath(szCommandToRun, FILE_TO_RUN_FILE_PARAM);
  177. // Run the executable if the file exists
  178. g_WinExecReturn = WinExec(szCommandToRun, SW_SHOW);
  179. if (g_WinExecReturn < 32)
  180. {
  181. // we failed on running it.
  182. PopUpUnableToSomething(szCommandToRun, IDS_UNABLE_TO_RUN);
  183. return FALSE;
  184. }
  185. GetModuleFileName(NULL, g_szWinExecModuleName, sizeof(g_szWinExecModuleName));
  186. return TRUE;
  187. }
  188. //***************************************************************************
  189. //*
  190. //* purpose: return TRUE if the window has started
  191. //*
  192. //***************************************************************************
  193. int HasTheAppStarted(void)
  194. {
  195. // do a findwindow for our setup window to
  196. // see if our setup has started...
  197. // if it has then return TRUE, if not return FALSE.
  198. if (g_WinExecReturn >= 32)
  199. {
  200. if (GetModuleHandle(g_szWinExecModuleName))
  201. {
  202. return TRUE;
  203. }
  204. }
  205. return FALSE;
  206. }
  207. //***************************************************************************
  208. //*
  209. //* purpose: TRUE if the file is opened, FALSE if the file does not exists.
  210. //*
  211. //***************************************************************************
  212. int CheckIfFileExists (char * szFileName)
  213. {
  214. char svTemp1[_MAX_PATH];
  215. char *pdest = NULL;
  216. char *pTemp = NULL;
  217. if ( strlen( szFileName ) > sizeof( svTemp1 ) )
  218. {
  219. return FALSE;
  220. }
  221. strcpy(svTemp1, szFileName);
  222. // cut off the trailing \ if need to
  223. pdest = svTemp1;
  224. if (*(pdest + (strlen(pdest) - 1)) == '\\')
  225. {
  226. pTemp = strrchr(svTemp1, '\\');
  227. if (pTemp)
  228. {
  229. *pTemp = '\0';
  230. }
  231. }
  232. if ( (_access(svTemp1,0) ) != -1 )
  233. {
  234. return TRUE;
  235. }
  236. else
  237. {
  238. return FALSE;
  239. }
  240. }
  241. //***************************************************************************
  242. //*
  243. //* purpose: display message that we were unable to runthe exe
  244. //*
  245. //***************************************************************************
  246. void PopUpUnableToSomething(char g_szFilepath[], int WhichString_ID)
  247. {
  248. char szTempString[_MAX_PATH];
  249. LoadString( g_hInstance, WhichString_ID, g_szMsg, _MAX_PATH );
  250. if ( ( strlen( g_szMsg ) + strlen( g_szFilepath ) ) > sizeof( szTempString ) )
  251. {
  252. return;
  253. }
  254. sprintf(szTempString, g_szMsg, g_szFilepath);
  255. MessageBox(NULL, szTempString, NULL, MB_ICONSTOP);
  256. return;
  257. }
  258. //***************************************************************************
  259. //*
  260. //* purpose: add's filename onto path
  261. //*
  262. //***************************************************************************
  263. void AddPath(LPSTR szPath, LPCSTR szName )
  264. {
  265. LPSTR szTmp;
  266. // Find end of the string
  267. szTmp = szPath + lstrlen(szPath);
  268. // If no trailing backslash then add one
  269. if ( szTmp > szPath && *(AnsiPrev( szPath, szTmp )) != '\\' )
  270. *(szTmp++) = '\\';
  271. // Add new name to existing path string
  272. while ( *szName == ' ' ) szName++;
  273. lstrcpy( szTmp, szName );
  274. }