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.

438 lines
14 KiB

  1. // stubexe.cpp A command line program which runs the appropriate version
  2. // of MSInfo, based on the registry settings
  3. //
  4. // History: a-jsari 10/13/97
  5. //
  6. // Copyright (c) 1998-1999 Microsoft Corporation
  7. #include <afx.h>
  8. #include <afxwin.h>
  9. #include <io.h>
  10. #include <process.h>
  11. #include <errno.h>
  12. #include "StdAfx.h"
  13. #include "Resource.h"
  14. #include "StubExe.h"
  15. BOOL CMSInfoApp::InitInstance()
  16. {
  17. if (!RunMSInfoInHelpCtr())
  18. {
  19. CDialog help(IDD_MSICMDLINE);
  20. help.DoModal();
  21. }
  22. return FALSE;
  23. }
  24. //-----------------------------------------------------------------------------
  25. // Required to use the new MSInfo DLL in HelpCtr.
  26. //-----------------------------------------------------------------------------
  27. typedef class MSInfo MSInfo;
  28. EXTERN_C const IID IID_IMSInfo;
  29. struct IMSInfo : public IDispatch
  30. {
  31. public:
  32. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_AutoSize(
  33. /* [in] */ VARIANT_BOOL vbool) = 0;
  34. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_AutoSize(
  35. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *pbool) = 0;
  36. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_BackColor(
  37. /* [in] */ OLE_COLOR clr) = 0;
  38. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_BackColor(
  39. /* [retval][out] */ OLE_COLOR __RPC_FAR *pclr) = 0;
  40. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_BackStyle(
  41. /* [in] */ long style) = 0;
  42. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_BackStyle(
  43. /* [retval][out] */ long __RPC_FAR *pstyle) = 0;
  44. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_BorderColor(
  45. /* [in] */ OLE_COLOR clr) = 0;
  46. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_BorderColor(
  47. /* [retval][out] */ OLE_COLOR __RPC_FAR *pclr) = 0;
  48. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_BorderStyle(
  49. /* [in] */ long style) = 0;
  50. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_BorderStyle(
  51. /* [retval][out] */ long __RPC_FAR *pstyle) = 0;
  52. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_BorderWidth(
  53. /* [in] */ long width) = 0;
  54. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_BorderWidth(
  55. /* [retval][out] */ long __RPC_FAR *width) = 0;
  56. virtual /* [id][propputref] */ HRESULT STDMETHODCALLTYPE putref_Font(
  57. /* [in] */ IFontDisp __RPC_FAR *pFont) = 0;
  58. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_Font(
  59. /* [in] */ IFontDisp __RPC_FAR *pFont) = 0;
  60. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_Font(
  61. /* [retval][out] */ IFontDisp __RPC_FAR *__RPC_FAR *ppFont) = 0;
  62. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_ForeColor(
  63. /* [in] */ OLE_COLOR clr) = 0;
  64. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_ForeColor(
  65. /* [retval][out] */ OLE_COLOR __RPC_FAR *pclr) = 0;
  66. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_Window(
  67. /* [retval][out] */ long __RPC_FAR *phwnd) = 0;
  68. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_BorderVisible(
  69. /* [in] */ VARIANT_BOOL vbool) = 0;
  70. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_BorderVisible(
  71. /* [retval][out] */ VARIANT_BOOL __RPC_FAR *pbool) = 0;
  72. virtual /* [id][propput] */ HRESULT STDMETHODCALLTYPE put_Appearance(
  73. /* [in] */ short appearance) = 0;
  74. virtual /* [id][propget] */ HRESULT STDMETHODCALLTYPE get_Appearance(
  75. /* [retval][out] */ short __RPC_FAR *pappearance) = 0;
  76. virtual /* [helpstring][id] */ HRESULT STDMETHODCALLTYPE SetHistoryStream(
  77. IStream __RPC_FAR *pStream) = 0;
  78. virtual /* [helpstring][id][propget] */ HRESULT STDMETHODCALLTYPE get_DCO_IUnknown(
  79. /* [retval][out] */ IUnknown __RPC_FAR *__RPC_FAR *pVal) = 0;
  80. virtual /* [helpstring][id][propput] */ HRESULT STDMETHODCALLTYPE put_DCO_IUnknown(
  81. /* [in] */ IUnknown __RPC_FAR *newVal) = 0;
  82. virtual /* [helpstring][id] */ HRESULT STDMETHODCALLTYPE SaveFile(
  83. BSTR filename,
  84. BSTR computer,
  85. BSTR category) = 0;
  86. };
  87. #include "msinfo32_i.c"
  88. //-----------------------------------------------------------------------------
  89. // This function encapsulates the functionality to run the new MSInfo in
  90. // HelpCtr. If this function returns false, the help should be displayed.
  91. //-----------------------------------------------------------------------------
  92. void StringReplace(CString & str, LPCTSTR szLookFor, LPCTSTR szReplaceWith);
  93. BOOL CMSInfoApp::RunMSInfoInHelpCtr()
  94. {
  95. //-------------------------------------------------------------------------
  96. // Parse the command line parameters into one big string to pass to the
  97. // ActiveX control. There are a few which would keep us from launching
  98. // HelpCtr.
  99. //-------------------------------------------------------------------------
  100. CString strCommandLine(CWinApp::m_lpCmdLine);
  101. CString strLastFlag;
  102. CString strCategory;
  103. CString strCategories;
  104. CString strComputer;
  105. CString strOpenFile;
  106. CString strPrintFile;
  107. CString strSilentNFO;
  108. CString strSilentExport;
  109. CString strTemp;
  110. BOOL fShowPCH = FALSE;
  111. BOOL fShowHelp = FALSE;
  112. BOOL fShowCategories = FALSE;
  113. CString strFileFlag(_T("msinfo_file"));
  114. // treating a commandline that comes from the shell as a special case.
  115. // I'm assuming that the case of the shell flag will not vary
  116. // and no other paramters will be packaged on the command line, so everything to the right of
  117. // msinfo_file will be the filename, which main contain spaces and multiple .'s
  118. // for XPServer bug: 609844 NFO file corrupt from Server 3615
  119. if (strCommandLine.Find(strFileFlag) > 0)
  120. {
  121. strOpenFile = strCommandLine.Right(strCommandLine.GetLength() - strFileFlag.GetLength() - 2);
  122. }
  123. else while (!strCommandLine.IsEmpty())
  124. {
  125. // Remove the leading whitespace from the string.
  126. strTemp = strCommandLine.SpanIncluding(_T(" \t=:"));
  127. strCommandLine = strCommandLine.Right(strCommandLine.GetLength() - strTemp.GetLength());
  128. // If the first character is a / or a -, then this is a flag.
  129. if (strCommandLine[0] == _T('/') || strCommandLine[0] == _T('-'))
  130. {
  131. strCommandLine = strCommandLine.Right(strCommandLine.GetLength() - 1);
  132. strLastFlag = strCommandLine.SpanExcluding(_T(" \t=:"));
  133. strCommandLine = strCommandLine.Right(strCommandLine.GetLength() - strLastFlag.GetLength());
  134. strLastFlag.MakeLower();
  135. if (strLastFlag == CString(_T("pch")))
  136. {
  137. fShowPCH = TRUE;
  138. strLastFlag.Empty();
  139. }
  140. else if (strLastFlag == CString(_T("?")) || strLastFlag == CString(_T("h")))
  141. {
  142. fShowHelp = TRUE;
  143. strLastFlag.Empty();
  144. }
  145. else if (strLastFlag == CString(_T("showcategories")))
  146. {
  147. fShowCategories = TRUE;
  148. strLastFlag.Empty();
  149. }
  150. continue;
  151. }
  152. // Otherwise, this is either a filename to open, or a parameter from the
  153. // previous command line flag. This might have quotes around it.
  154. if (strCommandLine[0] != _T('"'))
  155. {
  156. strTemp = strCommandLine.SpanExcluding(_T(" \t"));
  157. strCommandLine = strCommandLine.Right(strCommandLine.GetLength() - strTemp.GetLength());
  158. }
  159. else
  160. {
  161. strCommandLine = strCommandLine.Right(strCommandLine.GetLength() - 1);
  162. strTemp = strCommandLine.SpanExcluding(_T("\""));
  163. strCommandLine = strCommandLine.Right(strCommandLine.GetLength() - strTemp.GetLength() - 1);
  164. }
  165. if (strLastFlag.IsEmpty() || strLastFlag == CString(_T("msinfo_file")))
  166. strOpenFile = strTemp;
  167. else if (strLastFlag == CString(_T("p")))
  168. strPrintFile = strTemp;
  169. else if (strLastFlag == CString(_T("category")))
  170. strCategory = strTemp;
  171. else if (strLastFlag == CString(_T("categories")))
  172. strCategories = strTemp;
  173. else if (strLastFlag == CString(_T("computer")))
  174. strComputer = strTemp;
  175. else if (strLastFlag == CString(_T("report")))
  176. strSilentExport = strTemp;
  177. else if (strLastFlag == CString(_T("nfo")) || strLastFlag == CString(_T("s")))
  178. strSilentNFO = strTemp;
  179. strLastFlag.Empty();
  180. }
  181. if (fShowHelp)
  182. return FALSE;
  183. TCHAR szCurrent[MAX_PATH];
  184. GetCurrentDirectory(MAX_PATH, szCurrent);
  185. CString strCurrent(szCurrent);
  186. if (strCurrent.Right(1) != CString(_T("\\")))
  187. strCurrent += CString(_T("\\"));
  188. HRESULT hrInitialize = CoInitialize(NULL);
  189. if (!strSilentNFO.IsEmpty() || !strSilentExport.IsEmpty())
  190. {
  191. IMSInfo * pMSInfo = NULL;
  192. if (SUCCEEDED(CoCreateInstance(CLSID_MSInfo, NULL, CLSCTX_ALL, IID_IMSInfo, (void **)&pMSInfo)) && pMSInfo != NULL)
  193. {
  194. BSTR computer = strComputer.AllocSysString();
  195. BSTR category = strCategories.AllocSysString();
  196. if (!strSilentNFO.IsEmpty())
  197. {
  198. if (strSilentNFO.Find(_T('\\')) == -1)
  199. strSilentNFO = strCurrent + strSilentNFO;
  200. if (strSilentNFO.Right(4).CompareNoCase(CString(_T(".nfo"))) != 0)
  201. strSilentNFO += CString(_T(".nfo"));
  202. BSTR filename = strSilentNFO.AllocSysString();
  203. pMSInfo->SaveFile(filename, computer, category);
  204. SysFreeString(filename);
  205. }
  206. if (!strSilentExport.IsEmpty())
  207. {
  208. if (strSilentExport.Find(_T('\\')) == -1)
  209. strSilentExport = strCurrent + strSilentExport;
  210. BSTR filename = strSilentExport.AllocSysString();
  211. pMSInfo->SaveFile(filename, computer, category);
  212. SysFreeString(filename);
  213. }
  214. SysFreeString(computer);
  215. SysFreeString(category);
  216. pMSInfo->Release();
  217. }
  218. if (SUCCEEDED(hrInitialize))
  219. CoUninitialize();
  220. return TRUE;
  221. }
  222. CString strURLParam;
  223. if (fShowPCH)
  224. strURLParam += _T("pch");
  225. if (fShowCategories)
  226. strURLParam += _T(",showcategories");
  227. if (!strComputer.IsEmpty())
  228. strURLParam += _T(",computer=") + strComputer;
  229. if (!strCategory.IsEmpty())
  230. strURLParam += _T(",category=") + strCategory;
  231. if (!strCategories.IsEmpty())
  232. strURLParam += _T(",categories=") + strCategories;
  233. if (!strPrintFile.IsEmpty())
  234. {
  235. if (strPrintFile.Find(_T('\\')) == -1)
  236. strPrintFile = strCurrent + strPrintFile;
  237. strURLParam += _T(",print=") + strPrintFile;
  238. }
  239. if (!strOpenFile.IsEmpty())
  240. {
  241. if (strOpenFile.Find(_T('\\')) == -1)
  242. strOpenFile = strCurrent + strOpenFile;
  243. strURLParam += _T(",open=") + strOpenFile;
  244. }
  245. if (!strURLParam.IsEmpty())
  246. {
  247. strURLParam.TrimLeft(_T(","));
  248. strURLParam = CString(_T("?")) + strURLParam;
  249. }
  250. CString strURLAddress(_T("hcp://system/sysinfo/msinfo.htm"));
  251. CString strURL = strURLAddress + strURLParam;
  252. //-------------------------------------------------------------------------
  253. // Check to see if we can run MSInfo in HelpCtr. We need the HTM file
  254. // to be present.
  255. //-------------------------------------------------------------------------
  256. BOOL fRunVersion6 = TRUE;
  257. TCHAR szPath[MAX_PATH];
  258. if (ExpandEnvironmentStrings(_T("%windir%\\pchealth\\helpctr\\system\\sysinfo\\msinfo.htm"), szPath, MAX_PATH))
  259. {
  260. WIN32_FIND_DATA finddata;
  261. HANDLE h = FindFirstFile(szPath, &finddata);
  262. if (INVALID_HANDLE_VALUE != h)
  263. FindClose(h);
  264. else
  265. fRunVersion6 = FALSE;
  266. }
  267. // This would be used to check if the control is registered. Turns out we want to run anyway.
  268. //
  269. // IUnknown * pUnknown;
  270. // if (fRunVersion6 && SUCCEEDED(CoCreateInstance(CLSID_MSInfo, NULL, CLSCTX_ALL, IID_IUnknown, (void **) &pUnknown)))
  271. // pUnknown->Release();
  272. // else
  273. // fRunVersion6 = FALSE;
  274. StringReplace(strURL, _T(" "), _T("%20"));
  275. if (fRunVersion6)
  276. {
  277. // HelpCtr now supports running MSInfo in its own window. We need to
  278. // execute the following:
  279. //
  280. // helpctr -mode hcp://system/sysinfo/msinfo.xml
  281. //
  282. // Additionally, we can pass parameters in the URL using the
  283. // following flag:
  284. //
  285. // -url hcp://system/sysinfo/msinfo.htm?open=c:\savedfile.nfo
  286. //
  287. // First, find out of the XML file is present.
  288. BOOL fXMLPresent = TRUE;
  289. if (ExpandEnvironmentStrings(_T("%windir%\\pchealth\\helpctr\\system\\sysinfo\\msinfo.xml"), szPath, MAX_PATH))
  290. {
  291. WIN32_FIND_DATA finddata;
  292. HANDLE h = FindFirstFile(szPath, &finddata);
  293. if (INVALID_HANDLE_VALUE != h)
  294. FindClose(h);
  295. else
  296. fXMLPresent = FALSE;
  297. }
  298. // If the XML file is present and we can get the path for helpctr.exe, we
  299. // should launch it the new way.
  300. TCHAR szHelpCtrPath[MAX_PATH];
  301. if (fXMLPresent && ExpandEnvironmentStrings(_T("%windir%\\pchealth\\helpctr\\binaries\\helpctr.exe"), szHelpCtrPath, MAX_PATH))
  302. {
  303. CString strParams(_T("-mode hcp://system/sysinfo/msinfo.xml"));
  304. if (!strURLParam.IsEmpty())
  305. strParams += CString(_T(" -url ")) + strURL;
  306. ShellExecute(NULL, NULL, szHelpCtrPath, strParams, NULL, SW_SHOWNORMAL);
  307. }
  308. else
  309. ShellExecute(NULL, NULL, strURL, NULL, NULL, SW_SHOWNORMAL);
  310. }
  311. else
  312. ShellExecute(NULL, NULL, _T("hcp://system"), NULL, NULL, SW_SHOWNORMAL);
  313. if (SUCCEEDED(hrInitialize))
  314. CoUninitialize();
  315. return TRUE;
  316. }
  317. //-----------------------------------------------------------------------------
  318. // This was used originally to replace some MFC functionality not in the ME
  319. // build tree.
  320. //-----------------------------------------------------------------------------
  321. void StringReplace(CString & str, LPCTSTR szLookFor, LPCTSTR szReplaceWith)
  322. {
  323. CString strWorking(str);
  324. CString strReturn;
  325. CString strLookFor(szLookFor);
  326. CString strReplaceWith(szReplaceWith);
  327. int iLookFor = strLookFor.GetLength();
  328. int iNext;
  329. while (!strWorking.IsEmpty())
  330. {
  331. iNext = strWorking.Find(strLookFor);
  332. if (iNext == -1)
  333. {
  334. strReturn += strWorking;
  335. strWorking.Empty();
  336. }
  337. else
  338. {
  339. strReturn += strWorking.Left(iNext);
  340. strReturn += strReplaceWith;
  341. strWorking = strWorking.Right(strWorking.GetLength() - (iNext + iLookFor));
  342. }
  343. }
  344. str = strReturn;
  345. }