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.

267 lines
8.5 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. g_hInstance = hInstance;
  45. g_hPrevInstance = hPrevInstance;
  46. g_lpCmdLine = lpCmdLine;
  47. g_nCmdShow = nCmdShow;
  48. LoadString( g_hInstance, IDS_TITLE, szWinName, _MAX_PATH );
  49. lstrcpy(szBuf, "");
  50. // note that this will come back as "system" <-- must be because this is a 16bit app
  51. if (!GetSystemDirectory((LPSTR) szBuf, sizeof(szBuf)))
  52. {return 0;}
  53. lstrcpy(g_szSystemDir, szBuf);
  54. lstrcat(g_szSystemDir, "32");
  55. // set to system if can't find system32 directory
  56. if (CheckIfFileExists(g_szSystemDir) == FALSE) {lstrcpy(g_szSystemDir, szBuf);}
  57. // define windows class
  58. wcl.hInstance = hInstance;
  59. wcl.lpszClassName = szWinName;
  60. wcl.lpfnWndProc = WindowFunc;
  61. wcl.style = 0;
  62. wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  63. wcl.hCursor = LoadCursor(NULL, IDC_ARROW);
  64. wcl.lpszMenuName = NULL;
  65. wcl.cbClsExtra = 0;
  66. wcl.cbWndExtra = 0;
  67. wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
  68. // register the window class.
  69. if (!RegisterClass (&wcl)) return 0;
  70. //hwnd = CreateWindow(szWinName, NULL, WS_DLGFRAME, CW_USEDEFAULT, CW_USEDEFAULT, window_h, window_v, HWND_DESKTOP, NULL, hInstance , NULL);
  71. hwnd = CreateWindow(szWinName, NULL, WS_DISABLED | WS_CHILD, CW_USEDEFAULT, CW_USEDEFAULT, 10, 10, HWND_DESKTOP, NULL, hInstance , NULL);
  72. // display the window
  73. ShowWindow(hwnd, nCmdShow);
  74. // Start a timer -- interrupt once for 1 seconds
  75. SetTimer(hwnd, 1, 500, NULL);
  76. UpdateWindow(hwnd);
  77. // Return true only if we are able to start the setup program and run it.
  78. if (!RunTheApp()) {return FALSE;}
  79. // Check if the process has started by checking for
  80. // the window that should be run...
  81. if (HasTheAppStarted()) {PostQuitMessage(0);}
  82. while(GetMessage(&msg, NULL, 0, 0))
  83. {
  84. TranslateMessage(&msg);
  85. DispatchMessage(&msg);
  86. }
  87. KillTimer(hwnd, 1);
  88. return (int)(msg.wParam);
  89. }
  90. //***************************************************************************
  91. //*
  92. //* purpose: you know what
  93. //*
  94. //***************************************************************************
  95. LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  96. {
  97. switch(message)
  98. {
  99. case WM_TIMER:
  100. // Check if the process has started by checking for
  101. // the window that should be run...
  102. if (HasTheAppStarted()) {PostQuitMessage(0);}
  103. break;
  104. case WM_CREATE:
  105. break;
  106. case WM_PAINT:
  107. break;
  108. case WM_DESTROY:
  109. PostQuitMessage(0);
  110. break;
  111. default:
  112. return DefWindowProc(hwnd,message,wParam, lParam);
  113. }
  114. return 0;
  115. }
  116. //***************************************************************************
  117. //*
  118. //* purpose: return TRUE if the window has started
  119. //*
  120. //***************************************************************************
  121. int RunTheApp(void)
  122. {
  123. char szIISInstalledPath[_MAX_PATH];
  124. char szCommandToRun[_MAX_PATH + _MAX_PATH + 50];
  125. char szTempFilePath[_MAX_PATH];
  126. // check if our files exist...
  127. lstrcpy(szTempFilePath, g_szSystemDir);
  128. AddPath(szTempFilePath, FILE_TO_RUN);
  129. if (CheckIfFileExists(szTempFilePath) == FALSE) {PopUpUnableToSomething(szTempFilePath, IDS_UNABLE_TO_FIND); return FALSE;}
  130. // get iis installed directory
  131. LoadString( g_hInstance, IDS_INETSRV_INSTALLED_DIR, szIISInstalledPath, _MAX_PATH);
  132. lstrcpy(szTempFilePath, g_szSystemDir);
  133. AddPath(szTempFilePath, szIISInstalledPath);
  134. AddPath(szTempFilePath, FILE_TO_RUN_FILE_PARAM);
  135. if (CheckIfFileExists(szTempFilePath) == FALSE) {PopUpUnableToSomething(szTempFilePath, IDS_UNABLE_TO_FIND); return FALSE;}
  136. // Create a command line
  137. //%SystemRoot%\System32\mmc.exe D:\WINNT0\System32\inetsrv\iis.msc
  138. lstrcpy(szCommandToRun, g_szSystemDir);
  139. AddPath(szCommandToRun, FILE_TO_RUN);
  140. lstrcat(szCommandToRun, " ");
  141. lstrcat(szCommandToRun, g_szSystemDir);
  142. AddPath(szCommandToRun, szIISInstalledPath);
  143. AddPath(szCommandToRun, FILE_TO_RUN_FILE_PARAM);
  144. // Run the executable if the file exists
  145. g_WinExecReturn = WinExec(szCommandToRun, SW_SHOW);
  146. if (g_WinExecReturn < 32)
  147. {
  148. // we failed on running it.
  149. PopUpUnableToSomething(szCommandToRun, IDS_UNABLE_TO_RUN);
  150. return FALSE;
  151. }
  152. GetModuleFileName(NULL, g_szWinExecModuleName, sizeof(g_szWinExecModuleName));
  153. return TRUE;
  154. }
  155. //***************************************************************************
  156. //*
  157. //* purpose: return TRUE if the window has started
  158. //*
  159. //***************************************************************************
  160. int HasTheAppStarted(void)
  161. {
  162. // do a findwindow for our setup window to
  163. // see if our setup has started...
  164. // if it has then return TRUE, if not return FALSE.
  165. if (g_WinExecReturn >= 32)
  166. {
  167. if (GetModuleHandle(g_szWinExecModuleName))
  168. //if (GetModuleHandle(g_WinExecReturn))
  169. {return TRUE;}
  170. }
  171. return FALSE;
  172. }
  173. //***************************************************************************
  174. //*
  175. //* purpose: TRUE if the file is opened, FALSE if the file does not exists.
  176. //*
  177. //***************************************************************************
  178. int CheckIfFileExists (char * szFileName)
  179. {
  180. char svTemp1[_MAX_PATH];
  181. char *pdest = NULL;
  182. char *pTemp = NULL;
  183. strcpy(svTemp1, szFileName);
  184. // cut off the trailing \ if need to
  185. pdest = svTemp1;
  186. if (*(pdest + (strlen(pdest) - 1)) == '\\')
  187. {
  188. pTemp = strrchr(svTemp1, '\\');
  189. if (pTemp)
  190. {*pTemp = '\0';}
  191. }
  192. if ((_access(svTemp1,0)) != -1)
  193. {return TRUE;}
  194. else
  195. {return FALSE;}
  196. }
  197. //***************************************************************************
  198. //*
  199. //* purpose: display message that we were unable to runthe exe
  200. //*
  201. //***************************************************************************
  202. void PopUpUnableToSomething(char g_szFilepath[], int WhichString_ID)
  203. {
  204. char szTempString[_MAX_PATH];
  205. LoadString( g_hInstance, WhichString_ID, g_szMsg, _MAX_PATH );
  206. sprintf(szTempString, g_szMsg, g_szFilepath);
  207. MessageBox(NULL, szTempString, NULL, MB_ICONSTOP);
  208. return;
  209. }
  210. //***************************************************************************
  211. //*
  212. //* purpose: add's filename onto path
  213. //*
  214. //***************************************************************************
  215. void AddPath(LPSTR szPath, LPCSTR szName )
  216. {
  217. LPSTR szTmp;
  218. // Find end of the string
  219. szTmp = szPath + lstrlen(szPath);
  220. // If no trailing backslash then add one
  221. if ( szTmp > szPath && *(AnsiPrev( szPath, szTmp )) != '\\' )
  222. *(szTmp++) = '\\';
  223. // Add new name to existing path string
  224. while ( *szName == ' ' ) szName++;
  225. lstrcpy( szTmp, szName );
  226. }