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.

409 lines
9.8 KiB

  1. // Test.cpp : Defines the entry point for the console application.
  2. //
  3. #include <stdio.h>
  4. #include <windows.h>
  5. #include <ole2.h>
  6. #include <inseng.h>
  7. #define IsDigit(c) ((c) >= '0' && (c) <= '9')
  8. //--------------------------------------------------------------------------
  9. // ConvertDotVersionStrToDwords
  10. //--------------------------------------------------------------------------
  11. bool fConvertDotVersionStrToDwords(LPSTR pszVer, LPDWORD pdwVer, LPDWORD pdwBuild)
  12. {
  13. DWORD grVerFields[4] = {0,0,0,0};
  14. char *pch = pszVer;
  15. grVerFields[0] = atol(pch);
  16. for ( int index = 1; index < 4; index++ )
  17. {
  18. while ( IsDigit(*pch) && (*pch != '\0') )
  19. pch++;
  20. if ( *pch == '\0' )
  21. break;
  22. pch++;
  23. grVerFields[index] = atol(pch);
  24. }
  25. *pdwVer = (grVerFields[0] << 16) + grVerFields[1];
  26. *pdwBuild = (grVerFields[2] << 16) + grVerFields[3];
  27. return true;
  28. }
  29. //--------------------------------------------------------------------------
  30. // GetStringField2
  31. //--------------------------------------------------------------------------
  32. #define WHITESPACE " \t"
  33. DWORD GetStringField2(LPSTR szStr, UINT uField, LPSTR szBuf, UINT cBufSize)
  34. {
  35. LPSTR pszBegin = szStr;
  36. LPSTR pszEnd;
  37. UINT i = 0;
  38. DWORD dwToCopy;
  39. if ( (cBufSize == 0) || (szStr == NULL) || (szBuf == NULL) )
  40. {
  41. return 0;
  42. }
  43. szBuf[0] = '\0';
  44. // look for fields based on commas but handle quotes.
  45. for ( ;i < uField; i++ )
  46. {
  47. // skip spaces
  48. pszBegin += strspn(pszBegin, WHITESPACE);
  49. // handle quotes
  50. if ( *pszBegin == '"' )
  51. {
  52. pszBegin = strchr(++pszBegin, '"');
  53. if ( pszBegin == NULL )
  54. {
  55. return 0; // invalid string
  56. }
  57. pszBegin++; // skip trailing quote
  58. // find start of next string
  59. pszBegin += strspn(pszBegin, WHITESPACE);
  60. if ( *pszBegin != ',' )
  61. {
  62. return 0;
  63. }
  64. }
  65. else
  66. {
  67. pszBegin = strchr(++pszBegin, ',');
  68. if ( pszBegin == NULL )
  69. {
  70. return 0; // field isn't here
  71. }
  72. }
  73. pszBegin++;
  74. }
  75. // pszBegin points to the start of the desired string.
  76. // skip spaces
  77. pszBegin += strspn(pszBegin, WHITESPACE);
  78. // handle quotes
  79. if ( *pszBegin == '"' )
  80. {
  81. pszEnd = strchr(++pszBegin, '"');
  82. if ( pszEnd == NULL )
  83. {
  84. return 0; // invalid string
  85. }
  86. }
  87. else
  88. {
  89. pszEnd = pszBegin + 1 + strcspn(pszBegin + 1, ",");
  90. while ( (pszEnd > pszBegin) &&
  91. ((*(pszEnd - 1) == ' ') || (*(pszEnd - 1) == '\t')) )
  92. {
  93. pszEnd--;
  94. }
  95. }
  96. dwToCopy = pszEnd - pszBegin + 1;
  97. if ( dwToCopy > cBufSize )
  98. {
  99. dwToCopy = cBufSize;
  100. }
  101. lstrcpynA(szBuf, pszBegin, dwToCopy);
  102. return dwToCopy - 1;
  103. }
  104. void Usage(char *pszExeName);
  105. class FakeICifComponent : public ICifComponent
  106. {
  107. public:
  108. FakeICifComponent(LPSTR pszCIFFile, LPSTR pszID)
  109. : m_pszCIFFile(pszCIFFile),
  110. m_pszID(pszID)
  111. {}
  112. STDMETHOD(GetID)(THIS_ LPSTR pszID, DWORD dwSize)
  113. { return E_NOTIMPL; }
  114. STDMETHOD(GetGUID)(THIS_ LPSTR pszGUID, DWORD dwSize)
  115. { return (GetPrivateProfileString(m_pszID, "GUID", "", pszGUID, dwSize, m_pszCIFFile) != 0 ) ? S_OK : E_FAIL; }
  116. STDMETHOD(GetDescription)(THIS_ LPSTR pszDesc, DWORD dwSize)
  117. { return E_NOTIMPL; }
  118. STDMETHOD(GetDetails)(THIS_ LPSTR pszDetails, DWORD dwSize)
  119. { return E_NOTIMPL; }
  120. STDMETHOD(GetUrl)(THIS_ UINT uUrlNum, LPSTR pszUrl, DWORD dwSize, LPDWORD pdwUrlFlags)
  121. { return E_NOTIMPL; }
  122. STDMETHOD(GetFileExtractList)(THIS_ UINT uUrlNum, LPSTR pszExtract, DWORD dwSize)
  123. { return E_NOTIMPL; }
  124. STDMETHOD(GetUrlCheckRange)(THIS_ UINT uUrlNum, LPDWORD pdwMin, LPDWORD pdwMax)
  125. { return E_NOTIMPL; }
  126. STDMETHOD(GetCommand)(THIS_ UINT uCmdNum, LPSTR pszCmd, DWORD dwCmdSize, LPSTR pszSwitches,
  127. DWORD dwSwitchSize, LPDWORD pdwType)
  128. { return E_NOTIMPL; }
  129. STDMETHOD(GetVersion)(THIS_ LPDWORD pdwVersion, LPDWORD pdwBuild)
  130. {
  131. char szBuf[100];
  132. if ( GetPrivateProfileString(m_pszID, "VERSION", "", szBuf, sizeof(szBuf), m_pszCIFFile) == 0 )
  133. return E_FAIL;
  134. fConvertDotVersionStrToDwords(szBuf, pdwVersion, pdwBuild);
  135. return S_OK;
  136. }
  137. STDMETHOD(GetLocale)(THIS_ LPSTR pszLocale, DWORD dwSize)
  138. { return (GetPrivateProfileString(m_pszID, "LOCALE", "", pszLocale, dwSize, m_pszCIFFile) != 0 ) ? S_OK : E_FAIL; }
  139. STDMETHOD(GetUninstallKey)(THIS_ LPSTR pszKey, DWORD dwSize)
  140. { return E_NOTIMPL; }
  141. STDMETHOD(GetInstalledSize)(THIS_ LPDWORD pdwWin, LPDWORD pdwApp)
  142. { return E_NOTIMPL; }
  143. STDMETHOD_(DWORD, GetDownloadSize)(THIS)
  144. { return E_NOTIMPL; }
  145. STDMETHOD_(DWORD, GetExtractSize)(THIS)
  146. { return E_NOTIMPL; }
  147. STDMETHOD(GetSuccessKey)(THIS_ LPSTR pszKey, DWORD dwSize)
  148. { return E_NOTIMPL; }
  149. STDMETHOD(GetProgressKeys)(THIS_ LPSTR pszProgress, DWORD dwProgSize,
  150. LPSTR pszCancel, DWORD dwCancelSize)
  151. { return E_NOTIMPL; }
  152. STDMETHOD(IsActiveSetupAware)(THIS)
  153. { return E_NOTIMPL; }
  154. STDMETHOD(IsRebootRequired)(THIS)
  155. { return E_NOTIMPL; }
  156. STDMETHOD(RequiresAdminRights)(THIS)
  157. { return E_NOTIMPL; }
  158. STDMETHOD_(DWORD, GetPriority)(THIS)
  159. { return E_NOTIMPL; }
  160. STDMETHOD(GetDependency)(THIS_ UINT uDepNum, LPSTR pszID, DWORD dwBuf, char *pchType, LPDWORD pdwVer, LPDWORD pdwBuild)
  161. { return E_NOTIMPL; }
  162. STDMETHOD_(DWORD, GetPlatform)(THIS)
  163. { return E_NOTIMPL; }
  164. STDMETHOD(GetMode)(THIS_ UINT uModeNum, LPSTR pszMode, DWORD dwSize)
  165. { return E_NOTIMPL; }
  166. STDMETHOD(GetGroup)(THIS_ LPSTR pszID, DWORD dwSize)
  167. { return E_NOTIMPL; }
  168. STDMETHOD(IsUIVisible)(THIS)
  169. { return E_NOTIMPL; }
  170. STDMETHOD(GetPatchID)(THIS_ LPSTR pszID, DWORD dwSize)
  171. { return E_NOTIMPL; }
  172. STDMETHOD(GetDetVersion)(THIS_ LPSTR pszDLL, DWORD dwdllSize, LPSTR pszEntry, DWORD dwentSize)
  173. {
  174. HRESULT hr = S_OK;
  175. char szBuf[100];
  176. if ( (GetPrivateProfileString(m_pszID, "DetectVersion", "", szBuf, sizeof(szBuf), m_pszCIFFile) == 0 ) ||
  177. (GetStringField2(szBuf, 0, pszDLL, dwdllSize) == 0) ||
  178. (GetStringField2(szBuf, 1, pszEntry, dwentSize) == 0) )
  179. {
  180. hr = E_FAIL;
  181. }
  182. return hr;
  183. }
  184. STDMETHOD(GetTreatAsOneComponents)(THIS_ UINT uNum, LPSTR pszID, DWORD dwBuf)
  185. { return E_NOTIMPL; }
  186. STDMETHOD(GetCustomData)(LPSTR pszKey, LPSTR pszData, DWORD dwSize)
  187. {
  188. char szBuf[100];
  189. lstrcpy(szBuf, "_");
  190. lstrcat(szBuf, pszKey);
  191. return (GetPrivateProfileString(m_pszID, szBuf, "", pszData, dwSize, m_pszCIFFile) != 0 ) ? S_OK : E_FAIL;
  192. }
  193. // access to state
  194. STDMETHOD_(DWORD, IsComponentInstalled)(THIS)
  195. { return E_NOTIMPL; }
  196. STDMETHOD(IsComponentDownloaded)(THIS)
  197. { return E_NOTIMPL; }
  198. STDMETHOD_(DWORD, IsThisVersionInstalled)(THIS_ DWORD dwAskVer, DWORD dwAskBld, LPDWORD pdwVersion, LPDWORD pdwBuild)
  199. { return E_NOTIMPL; }
  200. STDMETHOD_(DWORD, GetInstallQueueState)(THIS)
  201. { return E_NOTIMPL; }
  202. STDMETHOD(SetInstallQueueState)(THIS_ DWORD dwState)
  203. { return E_NOTIMPL; }
  204. STDMETHOD_(DWORD, GetActualDownloadSize)(THIS)
  205. { return E_NOTIMPL; }
  206. STDMETHOD_(DWORD, GetCurrentPriority)(THIS)
  207. { return E_NOTIMPL; }
  208. STDMETHOD(SetCurrentPriority)(THIS_ DWORD dwPriority)
  209. { return E_NOTIMPL; }
  210. private:
  211. LPSTR m_pszCIFFile;
  212. LPSTR m_pszID;
  213. };
  214. //--------------------------------------------------------------------------
  215. // main
  216. //--------------------------------------------------------------------------
  217. int __cdecl main(int argc, char* argv[])
  218. {
  219. HRESULT hr = E_FAIL;
  220. HINSTANCE hLib;
  221. DETECTVERSION fpDetVer;
  222. DETECTION_STRUCT Det;
  223. DWORD dwInstalledVer = 1;
  224. DWORD dwInstalledBuild = 1;
  225. char szDll[MAX_PATH];
  226. char szEntryPoint[MAX_PATH];
  227. char szCIFFile[MAX_PATH];
  228. LPSTR pszCIFFile;
  229. LPSTR pszID;
  230. char szGUID[100];
  231. char szLocale[100];
  232. // check args
  233. if ( argc != 3 )
  234. {
  235. Usage(argv[0]);
  236. return 1;
  237. }
  238. pszCIFFile = _fullpath(szCIFFile, argv[1], sizeof(szCIFFile));
  239. if ( pszCIFFile == NULL )
  240. {
  241. printf("Cannot find file %s.\n", argv[1]);
  242. return 1;
  243. }
  244. pszID = argv[2];
  245. memset(&Det, 0, sizeof(Det));
  246. Det.dwSize = sizeof(DETECTION_STRUCT);
  247. Det.pdwInstalledVer = &dwInstalledVer;
  248. Det.pdwInstalledBuild = &dwInstalledBuild;
  249. // load this information from the CIF file
  250. FakeICifComponent FakeCifComp(pszCIFFile, pszID);
  251. /*
  252. if ( FAILED(FakeCifComp.GetGUID(szGUID, sizeof(szGUID))) )
  253. {
  254. printf("Cannot find GUID in CIF file.\n");
  255. return 1;
  256. }
  257. Det.pszGUID = szGUID;
  258. if ( FAILED(FakeCifComp.GetLocale(szLocale, sizeof(szLocale))) )
  259. {
  260. printf("Cannot find Locale in CIF file.\n");
  261. return 1;
  262. }
  263. Det.pszLocale = szLocale;
  264. if ( FAILED(FakeCifComp.GetVersion(&Det.dwAskVer, &Det.dwAskBuild)) )
  265. {
  266. printf("Cannot find Version in CIF file.\n");
  267. return 1;
  268. }
  269. */
  270. if ( FAILED(FakeCifComp.GetDetVersion(szDll, sizeof(szDll), szEntryPoint, sizeof(szEntryPoint))) )
  271. {
  272. printf("Cannot find DetectVersion in CIF file.\n");
  273. return 1;
  274. }
  275. Det.pCifComp = &FakeCifComp;
  276. hLib = LoadLibrary(szDll);
  277. if ( hLib == NULL )
  278. {
  279. printf("Could not load detection library.\n");
  280. }
  281. else
  282. {
  283. fpDetVer = (DETECTVERSION)GetProcAddress(hLib, szEntryPoint);
  284. if ( fpDetVer != NULL )
  285. {
  286. DWORD dwStatus = fpDetVer(&Det);
  287. switch ( dwStatus )
  288. {
  289. case DET_NOTINSTALLED:
  290. printf("Detection Status : ICI_NOTINSTALLED\n");
  291. break;
  292. case DET_INSTALLED:
  293. printf("Detection Status : ICI_INSTALLED\n");
  294. break;
  295. case DET_NEWVERSIONINSTALLED:
  296. printf("Detection Status : ICI_OLDVERSIONAVAILABLE\n");
  297. break;
  298. case DET_OLDVERSIONINSTALLED:
  299. printf("Detection Status : ICI_NEWVERSIONAVAILABLE\n");
  300. break;
  301. default:
  302. printf("Detection Status : unknown status %d\n", dwStatus);
  303. break;
  304. }
  305. }
  306. FreeLibrary(hLib);
  307. }
  308. return hr;
  309. }
  310. //--------------------------------------------------------------------------
  311. // Usage
  312. //--------------------------------------------------------------------------
  313. void Usage(char *pszExeName)
  314. {
  315. printf("Usage:\n\n");
  316. printf("%s <CIF file> <CIF section>\n", pszExeName);
  317. printf("\n");
  318. printf("e.g. %s wu.cif chrome\n", pszExeName);
  319. }