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.

1218 lines
40 KiB

  1. #include "stdafx.h"
  2. #include "lzexpand.h"
  3. #include <loadperf.h>
  4. #include "setupapi.h"
  5. #include <ole2.h>
  6. #include <iis64.h>
  7. #include "iadmw.h"
  8. #include "iadmw.h"
  9. #include "iiscnfg.h"
  10. #include "mdkey.h"
  11. #include "dcomperm.h"
  12. #include "ocmanage.h"
  13. #include "log.h"
  14. #include "kill.h"
  15. #include "svc.h"
  16. #include "other.h"
  17. extern OCMANAGER_ROUTINES gHelperRoutines;
  18. extern int g_GlobalDebugLevelFlag;
  19. extern int g_GlobalDebugCallValidateHeap;
  20. // stuff for finding out architecture type of a file
  21. #define IMAGE_BASE_TO_DOS_HEADER(b) ((PIMAGE_DOS_HEADER)(b))
  22. #define IMAGE_BASE_TO_NT_HEADERS(b) ((PIMAGE_NT_HEADERS)( (DWORD_PTR)(b) + ((PIMAGE_DOS_HEADER)(b))->e_lfanew ))
  23. #define IMAGE_BASE_TO_FILE_HEADER(b) ((PIMAGE_FILE_HEADER)( &IMAGE_BASE_TO_NT_HEADERS(b)->FileHeader ))
  24. //
  25. // PSAPI.DLL
  26. //
  27. HINSTANCE g_hInstLib_PSAPI = NULL;
  28. // PSAPI.DLL "EnumProcessModules"
  29. typedef BOOL (WINAPI *PfnEnumProcessModules)(HANDLE hProcess, HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded);
  30. BOOL (WINAPI *g_lpfEnumProcessModules)(HANDLE hProcess, HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded);
  31. // PSAPI.DLL "GetModuleFileNameExA","GetModuleFileNameExW"
  32. typedef BOOL (WINAPI *PfnGetModuleFileNameEx)(HANDLE hProcess, HMODULE lphModule, LPTSTR lpFileName, DWORD dwSize);
  33. BOOL (WINAPI *g_lpfGetModuleFileNameEx)(HANDLE hProcess, HMODULE lphModule, LPTSTR lpFileName, DWORD dwSize);
  34. DWORD LogHeapState(BOOL bLogSuccessStateToo, char *szFileName, int iLineNumber)
  35. {
  36. DWORD dwReturn = E_FAIL;
  37. if (!g_GlobalDebugCallValidateHeap)
  38. {
  39. // don't even call RtlValidateHeap
  40. dwReturn = ERROR_SUCCESS;
  41. return dwReturn;
  42. }
  43. #ifndef _CHICAGO_
  44. //iisDebugOut((LOG_TYPE_TRACE_WIN32_API, _T("ntdll:RtlProcessHeap().Start.")));
  45. if ( RtlValidateHeap( RtlProcessHeap(), 0, NULL ) )
  46. {
  47. // HEAP IS GOOD
  48. dwReturn = ERROR_SUCCESS;
  49. if (bLogSuccessStateToo) {iisDebugOut((LOG_TYPE_TRACE, _T("RtlValidateHeap(): Good.\n")));}
  50. }
  51. else
  52. {
  53. #if defined(UNICODE) || defined(_UNICODE)
  54. LPWSTR pwsz = NULL;
  55. pwsz = MakeWideStrFromAnsi(szFileName);
  56. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("RtlValidateHeap(): Corrupt!!! %1!s!:Line %2!d!. FAILURE!\n"), pwsz, iLineNumber));
  57. if (pwsz)
  58. {
  59. //iisDebugOut((LOG_TYPE_TRACE_WIN32_API, _T("ntdll:CoTaskMemFree().Start.")));
  60. CoTaskMemFree(pwsz);
  61. //iisDebugOut((LOG_TYPE_TRACE_WIN32_API, _T("ntdll:CoTaskMemFree().End.")));
  62. }
  63. #else
  64. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("RtlValidateHeap(): Corrupt!!! %1!s!:Line %2!d!. FAILURE!\n"), szFileName, iLineNumber));
  65. #endif
  66. }
  67. #endif
  68. //iisDebugOut((LOG_TYPE_TRACE_WIN32_API, _T("ntdll:RtlProcessHeap().End.")));
  69. return dwReturn;
  70. }
  71. DWORD LogPendingReBootOperations(void)
  72. // Returns !ERROR_SUCCESS if there are reboot operations which
  73. // need to get taken are of before we can run setup.
  74. {
  75. DWORD dwReturn = ERROR_SUCCESS;
  76. CString csFormat, csMsg;
  77. // If any of the services that we install
  78. // is in the funky state = ERROR_SERVICE_MARKED_FOR_DELETE
  79. // That means that the user needs to reboot before we can
  80. // reinstall the service! otherwise setup will be hosed!
  81. int iSaveOld_AllowMessageBoxPopups = g_pTheApp->m_bAllowMessageBoxPopups;
  82. g_pTheApp->m_bAllowMessageBoxPopups = TRUE;
  83. #ifndef _CHICAGO_
  84. // Check if the HTTP drive is marked for deletion
  85. if (TRUE == CheckifServiceMarkedForDeletion(_T("HTTP")))
  86. {
  87. MyMessageBox(NULL, IDS_SERVICE_IN_DELETE_STATE, _T("SPUD"),ERROR_SERVICE_MARKED_FOR_DELETE, MB_OK | MB_SETFOREGROUND);
  88. dwReturn = !ERROR_SUCCESS;
  89. }
  90. // Check if the spud driver is marked for deletion
  91. if (TRUE == CheckifServiceMarkedForDeletion(_T("SPUD")))
  92. {
  93. MyMessageBox(NULL, IDS_SERVICE_IN_DELETE_STATE, _T("SPUD"),ERROR_SERVICE_MARKED_FOR_DELETE, MB_OK | MB_SETFOREGROUND);
  94. dwReturn = !ERROR_SUCCESS;
  95. }
  96. // Check if the iisadmin service is marked for deletion
  97. if (TRUE == CheckifServiceMarkedForDeletion(_T("IISADMIN")))
  98. {
  99. MyMessageBox(NULL, IDS_SERVICE_IN_DELETE_STATE, _T("IISADMIN"),ERROR_SERVICE_MARKED_FOR_DELETE, MB_OK | MB_SETFOREGROUND);
  100. dwReturn = !ERROR_SUCCESS;
  101. }
  102. // Check if the W3SVC service is marked for deletion
  103. if (TRUE == CheckifServiceMarkedForDeletion(_T("W3SVC")))
  104. {
  105. MyMessageBox(NULL, IDS_SERVICE_IN_DELETE_STATE, _T("W3SVC"),ERROR_SERVICE_MARKED_FOR_DELETE, MB_OK | MB_SETFOREGROUND);
  106. dwReturn = !ERROR_SUCCESS;
  107. }
  108. // Check if the MSFTPSVC service is marked for deletion
  109. if (TRUE == CheckifServiceMarkedForDeletion(_T("MSFTPSVC")))
  110. {
  111. MyMessageBox(NULL, IDS_SERVICE_IN_DELETE_STATE, _T("MSFTPSVC"),ERROR_SERVICE_MARKED_FOR_DELETE, MB_OK | MB_SETFOREGROUND);
  112. dwReturn = !ERROR_SUCCESS;
  113. }
  114. #endif //_CHICAGO_
  115. g_pTheApp->m_bAllowMessageBoxPopups = iSaveOld_AllowMessageBoxPopups;
  116. return dwReturn;
  117. }
  118. // Get the .inf section. which has the file names
  119. // get the corresponding directory
  120. // print out the file date and versions of these files.
  121. DWORD LogFileVersionsForThisINFSection(IN HINF hFile, IN LPCTSTR szSection)
  122. {
  123. DWORD dwReturn = ERROR_SUCCESS;
  124. LPTSTR szLine = NULL;
  125. DWORD dwRequiredSize;
  126. BOOL b = FALSE;
  127. CString csFile;
  128. DWORD dwMSVer, dwLSVer;
  129. INFCONTEXT Context;
  130. TCHAR buf[_MAX_PATH];
  131. GetSystemDirectory( buf, _MAX_PATH);
  132. // go to the beginning of the section in the INF file
  133. b = SetupFindFirstLine_Wrapped(hFile, szSection, NULL, &Context);
  134. if (!b)
  135. {
  136. dwReturn = !ERROR_SUCCESS;
  137. goto LogFileVersionsForThisINFSection_Exit;
  138. }
  139. // loop through the items in the section.
  140. while (b) {
  141. // get the size of the memory we need for this
  142. b = SetupGetLineText(&Context, NULL, NULL, NULL, NULL, 0, &dwRequiredSize);
  143. // prepare the buffer to receive the line
  144. szLine = (LPTSTR)GlobalAlloc( GPTR, dwRequiredSize * sizeof(TCHAR) );
  145. if ( !szLine )
  146. {
  147. goto LogFileVersionsForThisINFSection_Exit;
  148. }
  149. // get the line from the inf file1
  150. if (SetupGetLineText(&Context, NULL, NULL, NULL, szLine, dwRequiredSize, NULL) == FALSE)
  151. {
  152. goto LogFileVersionsForThisINFSection_Exit;
  153. }
  154. // Attach the path to the from of this...
  155. // check in this directory:
  156. // 1. winnt\system32
  157. // --------------------------------------
  158. // may look like this "iisrtl.dll,,4"
  159. // so get rid of the ',,4'
  160. LPTSTR pch = NULL;
  161. pch = _tcschr(szLine, _T(','));
  162. if (pch) {_tcscpy(pch, _T(" "));}
  163. // Remove any trailing spaces.
  164. StripLastBackSlash(szLine);
  165. // Get the system dir
  166. csFile = buf;
  167. csFile = AddPath(csFile, szLine);
  168. LogFileVersion(csFile, TRUE);
  169. // find the next line in the section. If there is no next line it should return false
  170. b = SetupFindNextLine(&Context, &Context);
  171. // free the temporary buffer
  172. GlobalFree( szLine );
  173. szLine = NULL;
  174. }
  175. if (szLine) {GlobalFree(szLine);szLine=NULL;}
  176. LogFileVersionsForThisINFSection_Exit:
  177. return dwReturn;
  178. }
  179. int LogFileVersion(IN LPCTSTR lpszFullFilePath, INT bShowArchType)
  180. {
  181. int iReturn = FALSE;
  182. DWORD dwMSVer, dwLSVer;
  183. WIN32_FIND_DATA FindFileData;
  184. HANDLE hFile = INVALID_HANDLE_VALUE;
  185. SYSTEMTIME st;
  186. TCHAR szDate[40];
  187. TCHAR szTime[20];
  188. TCHAR szLocalizedVersion[100] = _T("");
  189. TCHAR szFileAttributes[20] = _T("----");
  190. DWORD dwFileSize = 0;
  191. BOOL bThisIsABinary = FALSE;
  192. BOOL bGotTime = FALSE;
  193. BOOL bGotFileSize = FALSE;
  194. if (!(lpszFullFilePath))
  195. {
  196. iisDebugOut((LOG_TYPE_WARN, _T("LogFileVersion(string fullfilepath, int showarchtype). Invalid Parameter.")));
  197. return iReturn;
  198. }
  199. __try
  200. {
  201. if (IsFileExist(lpszFullFilePath))
  202. {
  203. TCHAR szExtensionOnly[_MAX_EXT] = _T("");
  204. _tsplitpath(lpszFullFilePath, NULL, NULL, NULL, szExtensionOnly);
  205. // Get version info for dll,exe,ocx only
  206. if (_tcsicmp(szExtensionOnly, _T(".exe")) == 0){bThisIsABinary=TRUE;}
  207. if (_tcsicmp(szExtensionOnly, _T(".dll")) == 0){bThisIsABinary=TRUE;}
  208. if (_tcsicmp(szExtensionOnly, _T(".ocx")) == 0){bThisIsABinary=TRUE;}
  209. // If this is the metabase.bin file then show the filesize!
  210. if (_tcsicmp(szExtensionOnly, _T(".bin")) == 0)
  211. {
  212. dwFileSize = ReturnFileSize(lpszFullFilePath);
  213. if (dwFileSize != 0xFFFFFFFF)
  214. {
  215. // If we were able to get the file size.
  216. bGotFileSize = TRUE;
  217. }
  218. }
  219. // If this is the metabase.xml file then show the filesize!
  220. if (_tcsicmp(szExtensionOnly, _T(".xml")) == 0)
  221. {
  222. dwFileSize = ReturnFileSize(lpszFullFilePath);
  223. if (dwFileSize != 0xFFFFFFFF)
  224. {
  225. // If we were able to get the file size.
  226. bGotFileSize = TRUE;
  227. }
  228. }
  229. // get the fileinformation
  230. // includes version and localizedversion
  231. MyGetVersionFromFile(lpszFullFilePath, &dwMSVer, &dwLSVer, szLocalizedVersion);
  232. hFile = FindFirstFile(lpszFullFilePath, &FindFileData);
  233. if (hFile != INVALID_HANDLE_VALUE)
  234. {
  235. // Try to get the systemtime.
  236. if ( FileTimeToSystemTime( &FindFileData.ftCreationTime, &st) )
  237. {
  238. GetDateFormat(LOCALE_USER_DEFAULT, DATE_SHORTDATE, &st, NULL, szDate, 64);
  239. GetTimeFormat(LOCALE_USER_DEFAULT, TIME_FORCE24HOURFORMAT | TIME_NOTIMEMARKER, &st, NULL, szTime, 64);
  240. bGotTime = TRUE;
  241. }
  242. // Get the file attributes.
  243. _stprintf(szFileAttributes, _T("%s%s%s%s%s%s%s%s"),
  244. FindFileData.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE ? _T("A") : _T("_"),
  245. FindFileData.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED ? _T("C") : _T("_"),
  246. FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ? _T("D") : _T("_"),
  247. FindFileData.dwFileAttributes & FILE_ATTRIBUTE_ENCRYPTED ? _T("E") : _T("_"),
  248. FindFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN ? _T("H") : _T("_"),
  249. FindFileData.dwFileAttributes & FILE_ATTRIBUTE_NORMAL ? _T("N") : _T("_"),
  250. FindFileData.dwFileAttributes & FILE_ATTRIBUTE_READONLY ? _T("R") : _T("_"),
  251. FindFileData.dwFileAttributes & FILE_ATTRIBUTE_SYSTEM ? _T("S") : _T("_")
  252. );
  253. if (bGotTime)
  254. {
  255. if (bThisIsABinary)
  256. {
  257. if (bShowArchType)
  258. {
  259. TCHAR szFileArchType[30] = _T("");
  260. LogFileArchType(lpszFullFilePath, szFileArchType);
  261. if (szFileArchType)
  262. {
  263. // show everything
  264. if (bGotFileSize)
  265. {
  266. iisDebugOut((LOG_TYPE_TRACE, _T("%s %s %s %d.%d.%d.%d: %s: %s: %s: %d"), szDate, szTime, szFileAttributes, HIWORD(dwMSVer), LOWORD(dwMSVer), HIWORD(dwLSVer), LOWORD(dwLSVer), szLocalizedVersion, szFileArchType, lpszFullFilePath, dwFileSize));
  267. }
  268. else
  269. {
  270. iisDebugOut((LOG_TYPE_TRACE, _T("%s %s %s %d.%d.%d.%d: %s: %s: %s"), szDate, szTime, szFileAttributes, HIWORD(dwMSVer), LOWORD(dwMSVer), HIWORD(dwLSVer), LOWORD(dwLSVer), szLocalizedVersion, szFileArchType, lpszFullFilePath));
  271. }
  272. }
  273. else
  274. {
  275. // show without arch type
  276. if (bGotFileSize)
  277. {
  278. iisDebugOut((LOG_TYPE_TRACE, _T("%s %s %s %d.%d.%d.%d: %s: %s: %d"), szDate, szTime, szFileAttributes, HIWORD(dwMSVer), LOWORD(dwMSVer), HIWORD(dwLSVer), LOWORD(dwLSVer), szLocalizedVersion, lpszFullFilePath, dwFileSize));
  279. }
  280. else
  281. {
  282. iisDebugOut((LOG_TYPE_TRACE, _T("%s %s %s %d.%d.%d.%d: %s: %s"), szDate, szTime, szFileAttributes, HIWORD(dwMSVer), LOWORD(dwMSVer), HIWORD(dwLSVer), LOWORD(dwLSVer), szLocalizedVersion, lpszFullFilePath));
  283. }
  284. }
  285. }
  286. else
  287. {
  288. // show without arch type
  289. if (bGotFileSize)
  290. {
  291. iisDebugOut((LOG_TYPE_TRACE, _T("%s %s %s %d.%d.%d.%d: %s: %s: %d"), szDate, szTime, szFileAttributes, HIWORD(dwMSVer), LOWORD(dwMSVer), HIWORD(dwLSVer), LOWORD(dwLSVer), szLocalizedVersion, lpszFullFilePath, dwFileSize));
  292. }
  293. else
  294. {
  295. iisDebugOut((LOG_TYPE_TRACE, _T("%s %s %s %d.%d.%d.%d: %s: %s"), szDate, szTime, szFileAttributes, HIWORD(dwMSVer), LOWORD(dwMSVer), HIWORD(dwLSVer), LOWORD(dwLSVer), szLocalizedVersion, lpszFullFilePath));
  296. }
  297. }
  298. }
  299. else
  300. {
  301. // This is not a binary file, must be like a text file.
  302. if (bGotFileSize)
  303. {
  304. iisDebugOut((LOG_TYPE_TRACE, _T("%s %s %s %s: %d"), szDate, szTime, szFileAttributes, lpszFullFilePath, dwFileSize));
  305. }
  306. else
  307. {
  308. iisDebugOut((LOG_TYPE_TRACE, _T("%s %s %s %s"), szDate, szTime, szFileAttributes, lpszFullFilePath));
  309. }
  310. }
  311. }
  312. else
  313. {
  314. // Show without filetime, since we couldn't get it
  315. iisDebugOut((LOG_TYPE_TRACE, _T("%s %d.%d.%d.%d: %s: %s"), szFileAttributes, HIWORD(dwMSVer), LOWORD(dwMSVer), HIWORD(dwLSVer), LOWORD(dwLSVer), szLocalizedVersion, lpszFullFilePath));
  316. }
  317. FindClose(hFile);
  318. }
  319. iReturn = TRUE;
  320. }
  321. }
  322. __except(EXCEPTION_EXECUTE_HANDLER)
  323. {
  324. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("\r\nException Caught in LogFileVersion(%1!s!). GetExceptionCode()=0x%2!x!\r\n"), lpszFullFilePath, GetExceptionCode()));
  325. }
  326. return iReturn;
  327. }
  328. BOOL LogFilesInThisDir(LPCTSTR szDirName)
  329. {
  330. BOOL bReturn = FALSE;
  331. DWORD retCode;
  332. WIN32_FIND_DATA FindFileData;
  333. HANDLE hFile = INVALID_HANDLE_VALUE;
  334. TCHAR szTempFileName[_MAX_PATH] = _T("");
  335. TCHAR szDirNameCopy[_MAX_PATH] = _T("");
  336. TCHAR szDirName2[_MAX_PATH] = _T("");
  337. if (szDirName)
  338. {
  339. _tcscpy(szDirNameCopy, szDirName);
  340. }
  341. else
  342. {
  343. // get currentdir
  344. GetCurrentDirectory(_MAX_PATH, szDirNameCopy);
  345. iisDebugOutSafeParams((LOG_TYPE_TRACE, _T("LogFilesInThisDir()=%1!s!. No parameter specified, so using current dir.\n"), szDirNameCopy));
  346. }
  347. retCode = GetFileAttributes(szDirNameCopy);
  348. if (retCode == 0xFFFFFFFF){goto LogFilesInThisDir_Exit;}
  349. // if this is a file, then
  350. // do this for only this one file.
  351. if (!(retCode & FILE_ATTRIBUTE_DIRECTORY))
  352. {
  353. bReturn = LogFileVersion(szDirNameCopy, TRUE);
  354. goto LogFilesInThisDir_Exit;
  355. }
  356. // ok, this is a directory,
  357. // so tack on the *.* deal
  358. _stprintf(szDirName2, _T("%s\\*.*"), szDirNameCopy);
  359. hFile = FindFirstFile(szDirName2, &FindFileData);
  360. if (hFile != INVALID_HANDLE_VALUE)
  361. {
  362. do {
  363. // display the filename, if it is not a directory.
  364. if ( _tcsicmp(FindFileData.cFileName, _T(".")) != 0 && _tcsicmp(FindFileData.cFileName, _T("..")) != 0 )
  365. {
  366. if (FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  367. {
  368. // this is a directory, so let's skip it
  369. }
  370. else
  371. {
  372. // this is a file, so let's output the info.
  373. _stprintf(szTempFileName, _T("%s\\%s"), szDirNameCopy, FindFileData.cFileName);
  374. if (LogFileVersion(szTempFileName, TRUE) == TRUE) {bReturn = TRUE;}
  375. }
  376. }
  377. // get the next file
  378. if ( !FindNextFile(hFile, &FindFileData) )
  379. {
  380. FindClose(hFile);
  381. break;
  382. }
  383. } while (TRUE);
  384. }
  385. LogFilesInThisDir_Exit:
  386. return bReturn;
  387. }
  388. /*----------------------------------------------------------------------------*\
  389. Function: StripLastBackSlash (TCHAR *)
  390. ----------------------------------------------------------------------------
  391. Description: StripLastBackSlash strips the last backslash in a path string
  392. NOTE: this code can be very easily broken as it lives under the assumption
  393. that the input string is a valid path (i.e. string of length two or greater)
  394. \*----------------------------------------------------------------------------*/
  395. TCHAR *StripLastBackSlash(TCHAR * i_szDir)
  396. {
  397. TCHAR * iszDir;
  398. iszDir = i_szDir + lstrlen(i_szDir);
  399. do
  400. {
  401. iszDir = CharPrev(i_szDir , iszDir);
  402. }
  403. while (((*iszDir == _T(' ')) || (*iszDir == _T('\\'))) && (iszDir != i_szDir));
  404. // If we came out of the loop and the current pointer still points to
  405. // a space or a backslash then all the string contains is some combination
  406. // of spaces and backspaces
  407. if ((*iszDir == _T(' ')) || (*iszDir == _T('\\')))
  408. {
  409. *i_szDir = _T('\0');
  410. return(i_szDir);
  411. }
  412. iszDir = CharNext(iszDir);
  413. *iszDir = _T('\0');
  414. return(i_szDir);
  415. }
  416. void LogCurrentProcessIDs(void)
  417. {
  418. DWORD numTasks = 0;
  419. PTASK_LIST The_TList = NULL;
  420. // Allocate the TASK_LIST in the heap and not on the stack!
  421. The_TList = (PTASK_LIST) HeapAlloc(GetProcessHeap(), 0, sizeof(TASK_LIST) * MAX_TASKS);
  422. if (NULL == The_TList){goto LogCurrentProcessIDs_Exit;}
  423. // Get the task list for the system, store it in The_TList
  424. numTasks = GetTaskList( The_TList, MAX_TASKS);
  425. for (DWORD i=0; i<numTasks; i++)
  426. {
  427. TCHAR szTempString[_MAX_PATH];
  428. #if defined(UNICODE) || defined(_UNICODE)
  429. MultiByteToWideChar( CP_ACP, 0, (char*) The_TList[i].ProcessName, -1, szTempString, _MAX_PATH);
  430. #else
  431. _tcscpy(szTempString, The_TList[i].ProcessName);
  432. #endif
  433. iisDebugOut((LOG_TYPE_PROGRAM_FLOW, _T("[%4d] %s\n"), The_TList[i].dwProcessId, szTempString));
  434. }
  435. LogCurrentProcessIDs_Exit:
  436. if (The_TList){HeapFree(GetProcessHeap(), 0, The_TList);The_TList = NULL;}
  437. return;
  438. }
  439. VOID LogFileArchType(LPCTSTR Filename, TCHAR * ReturnMachineType)
  440. {
  441. HANDLE fileHandle;
  442. HANDLE mapHandle;
  443. DWORD fileLength;
  444. PVOID view;
  445. TCHAR szReturnedString[30] = _T("");
  446. //
  447. // Open the file.
  448. //
  449. fileHandle = CreateFile(Filename,GENERIC_READ,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  450. if( fileHandle == INVALID_HANDLE_VALUE )
  451. {
  452. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("LogFileArchType: FAILURE: Cannot open %1!s!.\n"), Filename));
  453. return;
  454. }
  455. //
  456. // Get its size.
  457. //
  458. fileLength = GetFileSize(fileHandle,NULL);
  459. if( ( fileLength == (DWORD)-1L ) &&( GetLastError() != NO_ERROR ) )
  460. {
  461. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("LogFileArchType: failure. cannot get size of %1!s!.\n"), Filename));
  462. CloseHandle( fileHandle );
  463. return;
  464. }
  465. if( fileLength < sizeof(IMAGE_DOS_HEADER) )
  466. {
  467. iisDebugOutSafeParams((LOG_TYPE_WARN, _T("LogFileArchType: failure. %1!s! is an invalid image.\n"), Filename));
  468. CloseHandle( fileHandle );
  469. return;
  470. }
  471. //
  472. // Create the mapping.
  473. //
  474. mapHandle = CreateFileMapping(fileHandle,NULL,PAGE_READONLY,0,0,NULL);
  475. if( mapHandle == NULL )
  476. {
  477. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("LogFileArchType: failure. Cannot create mapping for %1!s!.\n"), Filename));
  478. CloseHandle( fileHandle );
  479. return;
  480. }
  481. //
  482. // Map it in.
  483. //
  484. view = MapViewOfFile(mapHandle,FILE_MAP_READ,0,0,0);
  485. if( view == NULL )
  486. {
  487. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("LogFileArchType: failure. Cannot map %1!s!.\n"), Filename));
  488. CloseHandle( mapHandle );
  489. CloseHandle( fileHandle );
  490. return;
  491. }
  492. //
  493. // Dump the image info.
  494. //
  495. _tcscpy(ReturnMachineType, _T(""));
  496. DumpFileArchInfo(Filename,view,fileLength, szReturnedString);
  497. _tcscpy(ReturnMachineType, szReturnedString);
  498. //
  499. // Cleanup.
  500. //
  501. UnmapViewOfFile( view );
  502. CloseHandle( mapHandle );
  503. CloseHandle( fileHandle );
  504. return;
  505. }
  506. TCHAR *MachineToString(DWORD Machine)
  507. {
  508. switch( Machine )
  509. {
  510. case IMAGE_FILE_MACHINE_UNKNOWN :
  511. return _T("Unknown");
  512. case IMAGE_FILE_MACHINE_I386 :
  513. return _T("x86");
  514. case IMAGE_FILE_MACHINE_AMD64 :
  515. return _T("AMD64");
  516. case IMAGE_FILE_MACHINE_IA64 :
  517. return _T("IA64");
  518. }
  519. return _T("INVALID");
  520. }
  521. VOID DumpFileArchInfo(LPCTSTR Filename,PVOID View,DWORD Length,TCHAR *ReturnString)
  522. {
  523. PIMAGE_DOS_HEADER dosHeader;
  524. PIMAGE_NT_HEADERS ntHeaders;
  525. PIMAGE_FILE_HEADER fileHeader;
  526. //
  527. // Validate the DOS header.
  528. //
  529. dosHeader = IMAGE_BASE_TO_DOS_HEADER( View );
  530. if( dosHeader->e_magic != IMAGE_DOS_SIGNATURE )
  531. {
  532. return;
  533. }
  534. //
  535. // Validate the NT headers.
  536. //
  537. ntHeaders = IMAGE_BASE_TO_NT_HEADERS( View );
  538. if( ntHeaders->Signature != IMAGE_NT_SIGNATURE )
  539. {
  540. return;
  541. }
  542. fileHeader = IMAGE_BASE_TO_FILE_HEADER( View );
  543. //
  544. // Dump the info.
  545. //
  546. // dump machine type
  547. _tcscpy(ReturnString, MachineToString( fileHeader->Machine ));
  548. return;
  549. }
  550. void LogCheckIfTempDirWriteable(void)
  551. {
  552. // attempt get the temp directory
  553. // and write to it.
  554. // we have had occurences where the tempdir was locked so,
  555. // some regsvr things failed.
  556. HANDLE hFile = NULL;
  557. TCHAR szTempFileName[_MAX_PATH+1];
  558. TCHAR szTempDir[_MAX_PATH+1];
  559. if (GetTempPath(_MAX_PATH,szTempDir) == 0)
  560. {
  561. // failed.
  562. iisDebugOut((LOG_TYPE_WARN, _T("LogCheckIfTempDirWriteable:GetTempPath() Failed. POTENTIAL PROBLEM. FAILURE.\n")));
  563. }
  564. else
  565. {
  566. // nope we got the temp dir
  567. // now let's get a tempfilename, write to it and
  568. // delete it.
  569. // trim off the last backslash...
  570. LPTSTR ptszTemp = _tcsrchr(szTempDir, _T('\\'));
  571. if (ptszTemp)
  572. {
  573. *ptszTemp = _T('\0');
  574. }
  575. if (GetTempFileName(szTempDir, _T("IIS"), 0, szTempFileName) != 0)
  576. {
  577. // Write to this file, and
  578. DeleteFile(szTempFileName);
  579. // Open existing file or create a new one.
  580. hFile = CreateFile(szTempFileName,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  581. if (hFile == INVALID_HANDLE_VALUE)
  582. {
  583. hFile = NULL;
  584. iisDebugOutSafeParams((LOG_TYPE_WARN, _T("LogCheckIfTempDirWriteable:LogTempDirLockedCheck() failed to CreateFile %1!s!. POTENTIAL PROBLEM. FAILURE.\n"), szTempFileName));
  585. }
  586. else
  587. {
  588. // write to the file
  589. if (hFile)
  590. {
  591. DWORD dwBytesWritten = 0;
  592. char szTestData[30];
  593. strcpy(szTestData, "Test");
  594. if (WriteFile(hFile,szTestData,strlen(szTestData),&dwBytesWritten,NULL))
  595. {
  596. // everything is hunky dory. don't print anything
  597. }
  598. else
  599. {
  600. // error writing to the file.
  601. iisDebugOutSafeParams((LOG_TYPE_WARN, _T("LogCheckIfTempDirWriteable:WriteFile(%1!s!) Failed. POTENTIAL PROBLEM. FAILURE. Error=0x%2!x!.\n"), szTempFileName, GetLastError()));
  602. }
  603. }
  604. }
  605. DeleteFile(szTempFileName);
  606. }
  607. else
  608. {
  609. iisDebugOutSafeParams((LOG_TYPE_WARN, _T("LogCheckIfTempDirWriteable:GetTempFileName(%1!s!, %2!s!) Failed. POTENTIAL PROBLEM. FAILURE.\n"), szTempDir, _T("IIS")));
  610. }
  611. }
  612. if (hFile)
  613. {
  614. CloseHandle(hFile);
  615. DeleteFile(szTempFileName);
  616. }
  617. return;
  618. }
  619. #ifndef _CHICAGO_
  620. BOOL EnumProcessModules(HANDLE hProcess, HMODULE *lphModule, DWORD cb, LPDWORD lpcbNeeded)
  621. {
  622. if (!g_lpfEnumProcessModules)
  623. {
  624. iisDebugOut((LOG_TYPE_TRACE, _T("EnumProcessModules: unable to work\n")));
  625. return FALSE;
  626. }
  627. return g_lpfEnumProcessModules(hProcess, lphModule, cb, lpcbNeeded);
  628. }
  629. BOOL Init_Lib_PSAPI(void)
  630. {
  631. BOOL bReturn = FALSE;
  632. // load the library
  633. if (!g_hInstLib_PSAPI){g_hInstLib_PSAPI = LoadLibrary( _T("PSAPI.DLL") ) ;}
  634. if( g_hInstLib_PSAPI == NULL ){goto Init_Library_PSAPI_Exit;}
  635. // get entry point
  636. if (!g_lpfEnumProcessModules)
  637. {g_lpfEnumProcessModules = (PfnEnumProcessModules) GetProcAddress( g_hInstLib_PSAPI, "EnumProcessModules");}
  638. if( g_lpfEnumProcessModules == NULL ){goto Init_Library_PSAPI_Exit;}
  639. // get entry point
  640. #if defined(UNICODE) || defined(_UNICODE)
  641. if (!g_lpfGetModuleFileNameEx)
  642. {g_lpfGetModuleFileNameEx = (PfnGetModuleFileNameEx) GetProcAddress( g_hInstLib_PSAPI, "GetModuleFileNameExW");}
  643. #else
  644. if (!g_lpfGetModuleFileNameEx)
  645. {g_lpfGetModuleFileNameEx = (PfnGetModuleFileNameEx) GetProcAddress( g_hInstLib_PSAPI, "GetModuleFileNameExA");}
  646. #endif
  647. if( g_lpfGetModuleFileNameEx == NULL ){goto Init_Library_PSAPI_Exit;}
  648. bReturn = TRUE;
  649. Init_Library_PSAPI_Exit:
  650. if (FALSE == bReturn)
  651. {
  652. iisDebugOut((LOG_TYPE_WARN, _T("non fatal error initing lib:PSAPI.DLL\n")));
  653. }
  654. return bReturn;
  655. }
  656. #define MAX_MODULES 256
  657. BOOL IsProcessUsingThisModule(LPWSTR lpwsProcessName,DWORD dwProcessId,LPWSTR ModuleName)
  658. {
  659. BOOL bReturn = FALSE;
  660. HANDLE hRealProcess = NULL;
  661. DWORD cbNeeded = 0;
  662. int iNumberOfModules = 0;
  663. bool fProcessNameFound = FALSE;
  664. HMODULE hMod[MAX_MODULES];
  665. TCHAR szFileName[_MAX_PATH] ;
  666. szFileName[0] = 0;
  667. if (FALSE == Init_Lib_PSAPI())
  668. {
  669. goto IsProcessUsingThisModule_Exit;
  670. }
  671. // if we don't have a dwProcessId, then get one from the filename!
  672. if (dwProcessId == 0)
  673. {
  674. __try
  675. {
  676. dwProcessId = FindProcessByNameW(lpwsProcessName);
  677. }
  678. __except(EXCEPTION_EXECUTE_HANDLER)
  679. {
  680. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("\r\nException Caught in FindProcessByNameW(%1!s!). GetExceptionCode()=0x%2!x!\r\n"), lpwsProcessName, GetExceptionCode()));
  681. }
  682. if( dwProcessId == 0 )
  683. {
  684. goto IsProcessUsingThisModule_Exit;
  685. }
  686. }
  687. hRealProcess = OpenProcess( MAXIMUM_ALLOWED,FALSE, dwProcessId );
  688. if( hRealProcess == NULL )
  689. {
  690. //iisDebugOutSafeParams((LOG_TYPE_TRACE, _T("IsProcessUsingThisModule: OpenProcess failed!\n")));
  691. goto IsProcessUsingThisModule_Exit;
  692. }
  693. if (!EnumProcessModules(hRealProcess,hMod,MAX_MODULES * sizeof(HMODULE),&cbNeeded))
  694. {goto IsProcessUsingThisModule_Exit;}
  695. // loop thru the modules in this .exe file
  696. // and see if it matches the one we are looking for!
  697. iNumberOfModules = cbNeeded / sizeof(HMODULE);
  698. fProcessNameFound = false;
  699. for(int i=0; i<iNumberOfModules; i++)
  700. {
  701. szFileName[0] = 0 ;
  702. // Get Full pathname!
  703. if(g_lpfGetModuleFileNameEx(hRealProcess, (HMODULE) hMod[i], szFileName, sizeof( szFileName )))
  704. {
  705. // if the szFileName is equal to the file we are looking for then Viola,
  706. // we've found it in this certain process!
  707. //[lsass.exe] C:\WINNT4\System32\ntdll.dll
  708. //iisDebugOut((LOG_TYPE_TRACE, _T("IsProcessUsingThisModule:[%s] %s\n"),lpwsProcessName,szFileName));
  709. if (_tcsicmp(szFileName,ModuleName) == 0)
  710. {
  711. // we've found it so
  712. // now add it to the list
  713. bReturn = TRUE;
  714. goto IsProcessUsingThisModule_Exit;
  715. }
  716. }
  717. }
  718. IsProcessUsingThisModule_Exit:
  719. if (hRealProcess) {CloseHandle( hRealProcess );}
  720. return bReturn;
  721. }
  722. BOOL DumpProcessModules(DWORD dwProcessId)
  723. {
  724. BOOL bReturn = FALSE;
  725. HANDLE hRealProcess = NULL;
  726. DWORD cbNeeded = 0;
  727. int iNumberOfModules = 0;
  728. HMODULE hMod[MAX_MODULES];
  729. TCHAR szFileName[_MAX_PATH] ;
  730. szFileName[0] = 0;
  731. if (FALSE == Init_Lib_PSAPI())
  732. {goto DumpProcessModules_Exit;}
  733. hRealProcess = OpenProcess( MAXIMUM_ALLOWED,FALSE, dwProcessId );
  734. if( hRealProcess == NULL )
  735. {
  736. iisDebugOutSafeParams((LOG_TYPE_TRACE, _T("DumpProcessModules: OpenProcess failed!\n")));
  737. goto DumpProcessModules_Exit;
  738. }
  739. if (!EnumProcessModules(hRealProcess,hMod,MAX_MODULES * sizeof(HMODULE),&cbNeeded))
  740. {goto DumpProcessModules_Exit;}
  741. // loop thru the modules in this .exe file
  742. // and see if it matches the one we are looking for!
  743. iNumberOfModules = cbNeeded / sizeof(HMODULE);
  744. for(int i=0; i<iNumberOfModules; i++)
  745. {
  746. bReturn = TRUE;
  747. // Get Full pathname!
  748. if(g_lpfGetModuleFileNameEx(hRealProcess, (HMODULE) hMod[i], szFileName, sizeof( szFileName )))
  749. {
  750. // if the szFileName is equal to the file we are looking for then Viola,
  751. // we've found it in this certain process!
  752. //[lsass.exe] C:\WINNT4\System32\ntdll.dll
  753. iisDebugOut((LOG_TYPE_TRACE, _T("[%d] %s\n"),dwProcessId,szFileName));
  754. }
  755. }
  756. DumpProcessModules_Exit:
  757. if (hRealProcess) {CloseHandle( hRealProcess );}
  758. return bReturn;
  759. }
  760. DWORD WINAPI FindProcessByNameW(const WCHAR * pszImageName)
  761. {
  762. DWORD Result = 0;
  763. DWORD numTasks = 0;
  764. PTASK_LIST The_TList = NULL;
  765. // Allocate the TASK_LIST in the heap and not on the stack!
  766. The_TList = (PTASK_LIST) HeapAlloc(GetProcessHeap(), 0, sizeof(TASK_LIST) * MAX_TASKS);
  767. if (NULL == The_TList){goto FindProcessByNameW_Exit;}
  768. // Get the task list for the system, store it in The_TList
  769. numTasks = GetTaskList( The_TList, MAX_TASKS);
  770. for (DWORD i=0; i<numTasks; i++)
  771. {
  772. TCHAR szTempString[_MAX_PATH];
  773. #if defined(UNICODE) || defined(_UNICODE)
  774. MultiByteToWideChar( CP_ACP, 0, (char*) The_TList[i].ProcessName, -1, szTempString, _MAX_PATH);
  775. #else
  776. _tcscpy(szTempString, The_TList[i].ProcessName);
  777. #endif
  778. // compare this process name with what they want
  779. // if we found the fully pathed process name in our list of processes
  780. // then return back the ProcessID
  781. if( _tcsicmp( szTempString, pszImageName ) == 0)
  782. {
  783. Result = The_TList[i].dwProcessId;
  784. goto FindProcessByNameW_Exit;
  785. }
  786. }
  787. FindProcessByNameW_Exit:
  788. if (The_TList){HeapFree(GetProcessHeap(), 0, The_TList);The_TList = NULL;}
  789. return Result;
  790. }
  791. void LogProcessesUsingThisModuleW(LPCTSTR szModuleNameToLookup, CStringList &strList)
  792. {
  793. DWORD numTasks = 0;
  794. PTASK_LIST The_TList = NULL;
  795. // return if nothing to lookup
  796. if (!(szModuleNameToLookup)) {return;}
  797. // Allocate the TASK_LIST in the heap and not on the stack!
  798. The_TList = (PTASK_LIST) HeapAlloc(GetProcessHeap(), 0, sizeof(TASK_LIST) * MAX_TASKS);
  799. if (NULL == The_TList){goto LogProcessesUsingThisModuleW_Exit;}
  800. // Get the task list for the system, store it in The_TList
  801. numTasks = GetTaskList( The_TList, MAX_TASKS);
  802. for (DWORD i=0; i<numTasks; i++)
  803. {
  804. TCHAR szTempString[_MAX_PATH];
  805. #if defined(UNICODE) || defined(_UNICODE)
  806. MultiByteToWideChar( CP_ACP, 0, (char*) The_TList[i].ProcessName, -1, szTempString, _MAX_PATH);
  807. #else
  808. _tcscpy(szTempString, The_TList[i].ProcessName);
  809. #endif
  810. if (TRUE == IsProcessUsingThisModule(szTempString,(DWORD) (DWORD_PTR) The_TList[i].dwProcessId,(TCHAR *) szModuleNameToLookup))
  811. {
  812. // Print out the .exe name
  813. //iisDebugOut((LOG_TYPE_TRACE_WIN32_API, _T("LogProcessesUsingThisModuleW:[%s] using %s\n"),szTempString,szModuleNameToLookup));
  814. // Add it the list of processes which are using this certain .dll
  815. //
  816. // something1.exe
  817. // something2.exe
  818. // something3.exe <----
  819. //
  820. // Add it to the strList if not already there!
  821. if (TRUE != IsThisStringInThisCStringList(strList, szTempString))
  822. {
  823. strList.AddTail(szTempString);
  824. }
  825. }
  826. }
  827. LogProcessesUsingThisModuleW_Exit:
  828. if (The_TList){HeapFree(GetProcessHeap(), 0, The_TList);The_TList = NULL;}
  829. return;
  830. }
  831. #endif
  832. void UnInit_Lib_PSAPI(void)
  833. {
  834. // Free entry points and library
  835. if (g_lpfGetModuleFileNameEx){g_lpfGetModuleFileNameEx = NULL;}
  836. if (g_lpfEnumProcessModules){g_lpfEnumProcessModules = NULL;}
  837. if (g_hInstLib_PSAPI)
  838. {
  839. FreeLibrary(g_hInstLib_PSAPI) ;
  840. g_hInstLib_PSAPI = NULL;
  841. }
  842. return;
  843. }
  844. #ifdef _CHICAGO_
  845. void LogProcessesUsingThisModuleA(LPCTSTR szModuleNameToLookup, CStringList &strList)
  846. {
  847. return;
  848. }
  849. #endif
  850. void LogProcessesUsingThisModule(LPCTSTR szModuleNameToLookup, CStringList &strList)
  851. {
  852. #ifndef _CHICAGO_
  853. __try
  854. {
  855. LogProcessesUsingThisModuleW(szModuleNameToLookup, strList);
  856. }
  857. __except(EXCEPTION_EXECUTE_HANDLER)
  858. {
  859. iisDebugOutSafeParams((LOG_TYPE_ERROR, _T("ExceptionCaught!:LogProcessesUsingThisModule(): File:%1!s!\n"), szModuleNameToLookup));
  860. }
  861. #else
  862. LogProcessesUsingThisModuleA(szModuleNameToLookup, strList);
  863. #endif
  864. return;
  865. }
  866. #ifdef _CHICAGO_
  867. void LogThisProcessesDLLsA(void)
  868. {
  869. return;
  870. }
  871. #else
  872. void LogThisProcessesDLLsW(void)
  873. {
  874. DWORD numTasks = 0;
  875. PTASK_LIST The_TList = NULL;
  876. DWORD ThisPid = GetCurrentProcessId();;
  877. // Allocate the TASK_LIST in the heap and not on the stack!
  878. The_TList = (PTASK_LIST) HeapAlloc(GetProcessHeap(), 0, sizeof(TASK_LIST) * MAX_TASKS);
  879. if (NULL == The_TList){goto LogThisProcessesDLLsW_Exit;}
  880. // Get the task list for the system, store it in The_TList
  881. numTasks = GetTaskList( The_TList, MAX_TASKS);
  882. for (DWORD i=0; i<numTasks; i++)
  883. {
  884. if (ThisPid == (DWORD) (DWORD_PTR) The_TList[i].dwProcessId)
  885. {
  886. TCHAR szTempString[512];
  887. #if defined(UNICODE) || defined(_UNICODE)
  888. MultiByteToWideChar( CP_ACP, 0, (char*) The_TList[i].ProcessName, -1, szTempString, _MAX_PATH);
  889. #else
  890. _tcscpy(szTempString, The_TList[i].ProcessName);
  891. #endif
  892. // display the used .dll files for this process. (our process)
  893. DumpProcessModules((DWORD) (DWORD_PTR) The_TList[i].dwProcessId);
  894. goto LogThisProcessesDLLsW_Exit;
  895. }
  896. }
  897. LogThisProcessesDLLsW_Exit:
  898. if (The_TList){HeapFree(GetProcessHeap(), 0, The_TList);The_TList = NULL;}
  899. return;
  900. }
  901. #endif
  902. void LogThisProcessesDLLs(void)
  903. {
  904. #ifdef _CHICAGO_
  905. LogThisProcessesDLLsA();
  906. #else
  907. __try
  908. {
  909. LogThisProcessesDLLsW();
  910. }
  911. __except(EXCEPTION_EXECUTE_HANDLER)
  912. {
  913. iisDebugOut((LOG_TYPE_WARN, _T("ExceptionCaught!:LogThisProcessesDLLs().\n")));
  914. }
  915. #endif
  916. return;
  917. }
  918. void LogFileVersions_System32(void)
  919. {
  920. TCHAR buf[_MAX_PATH];
  921. GetSystemDirectory( buf, _MAX_PATH);
  922. CString csTempPath = buf;
  923. LogFilesInThisDir(csTempPath);
  924. return;
  925. }
  926. void LogFileVersions_Inetsrv(void)
  927. {
  928. CString csTempPath = g_pTheApp->m_csPathInetsrv;
  929. LogFilesInThisDir(csTempPath);
  930. return;
  931. }
  932. DWORD LogFileVersionsForCopyFiles(IN HINF hFile, IN LPCTSTR szSection)
  933. {
  934. DWORD dwReturn = ERROR_SUCCESS;
  935. return dwReturn;
  936. }
  937. int LoadExeFromResource(int iWhichExeToGet, LPTSTR szReturnPath)
  938. {
  939. TCHAR szResourceNumString[10];
  940. TCHAR szSaveFileNameAs[_MAX_PATH];
  941. HANDLE hFile;
  942. LPTSTR szPointerToAllExeData = NULL;
  943. DWORD dwSize = 0;
  944. int iReturn = E_FAIL;
  945. _tcscpy(szReturnPath, _T(""));
  946. // The Binaries stored in the resource is x86 only
  947. // so... exit if this is not an x86
  948. SYSTEM_INFO si;
  949. GetSystemInfo( &si );
  950. if (si.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_INTEL)
  951. {
  952. iReturn = ERROR_NOT_SUPPORTED;
  953. goto LoadExeFromResource_Exit;
  954. }
  955. // get the resource id from the resource
  956. _stprintf(szResourceNumString, _T("#%d"), iWhichExeToGet);
  957. GetWindowsDirectory( szSaveFileNameAs, _MAX_PATH);
  958. TCHAR szResourceFileName[_MAX_FNAME];
  959. _stprintf(szResourceFileName, _T("Res%d.bin"), iWhichExeToGet);
  960. AddPath(szSaveFileNameAs, szResourceFileName);
  961. iisDebugOutSafeParams((LOG_TYPE_TRACE, _T("LoadExeFromResource: '%1!s!' Start.\n"), szSaveFileNameAs));
  962. // Check if the filename already exists...if it does, then don't overwrite it!
  963. if (IsFileExist(szSaveFileNameAs))
  964. {
  965. iReturn = ERROR_FILE_EXISTS;
  966. goto LoadExeFromResource_Exit;
  967. }
  968. HRSRC hrscReg;
  969. hrscReg = FindResource((HMODULE) g_MyModuleHandle, szResourceNumString, _T("EXE"));
  970. if (NULL == hrscReg)
  971. {
  972. iReturn = GetLastError();
  973. goto LoadExeFromResource_Exit;
  974. }
  975. HGLOBAL hResourceHandle;
  976. hResourceHandle = LoadResource((HMODULE)g_MyModuleHandle, hrscReg);
  977. if (NULL == hResourceHandle)
  978. {
  979. iReturn = GetLastError();
  980. goto LoadExeFromResource_Exit;
  981. }
  982. dwSize = SizeofResource((HMODULE)g_MyModuleHandle, hrscReg);
  983. // szPointerToAllExeData is a pointer to the whole thing
  984. szPointerToAllExeData = (LPTSTR) hResourceHandle;
  985. // Write all this data out to the file.
  986. __try
  987. {
  988. hFile = CreateFile(szSaveFileNameAs,GENERIC_READ | GENERIC_WRITE,FILE_SHARE_READ | FILE_SHARE_WRITE,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  989. if (hFile != INVALID_HANDLE_VALUE)
  990. {
  991. // save everything into the file
  992. DWORD dwBytesWritten = 0;
  993. if (WriteFile(hFile,szPointerToAllExeData,dwSize,&dwBytesWritten,NULL))
  994. {
  995. _tcscpy(szReturnPath, szSaveFileNameAs);
  996. iReturn = ERROR_SUCCESS;
  997. }
  998. else
  999. {
  1000. iReturn = GetLastError();
  1001. }
  1002. }
  1003. else
  1004. {
  1005. iReturn = ERROR_INVALID_HANDLE;
  1006. }
  1007. }
  1008. __except(EXCEPTION_EXECUTE_HANDLER)
  1009. {
  1010. TCHAR szErrorString[100];
  1011. _stprintf(szErrorString, _T("\r\n\r\nException Caught in LoadExeFromResource(). GetExceptionCode()=0x%x.\r\n\r\n"), GetExceptionCode());
  1012. OutputDebugString(szErrorString);
  1013. g_MyLogFile.LogFileWrite(szErrorString);
  1014. }
  1015. LoadExeFromResource_Exit:
  1016. iisDebugOut_End(_T("LoadExeFromResource"),LOG_TYPE_TRACE);
  1017. //if (szPointerToAllExeData) {LocalFree(szPointerToAllExeData);}
  1018. if (hFile) {CloseHandle(hFile);}
  1019. return iReturn;
  1020. }
  1021. void LogFileVersionsForGroupOfSections(IN HINF hFile)
  1022. {
  1023. CStringList strList;
  1024. CString csTheSection = _T("VerifyFileSections");
  1025. if (GetSectionNameToDo(hFile, csTheSection))
  1026. {
  1027. if (ERROR_SUCCESS == FillStrListWithListOfSections(hFile, strList, csTheSection))
  1028. {
  1029. // loop thru the list returned back
  1030. if (strList.IsEmpty() == FALSE)
  1031. {
  1032. POSITION pos;
  1033. CString csEntry;
  1034. pos = strList.GetHeadPosition();
  1035. while (pos)
  1036. {
  1037. csEntry = strList.GetAt(pos);
  1038. LogFileVersionsForThisINFSection(hFile, csEntry);
  1039. strList.GetNext(pos);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. return;
  1045. }