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.

1230 lines
41 KiB

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