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.

91 lines
2.6 KiB

  1. //***********************************************************************************
  2. //
  3. // Copyright (c) 2002 Microsoft Corporation. All Rights Reserved.
  4. //
  5. // File: BinarySubSystem.cpp
  6. // Module: util.lib
  7. //
  8. //***********************************************************************************
  9. #pragma once
  10. #include <windows.h>
  11. #include <tchar.h>
  12. #include <iucommon.h>
  13. #include <fileutil.h>
  14. HRESULT IsBinaryCompatible(LPCTSTR lpszFile)
  15. {
  16. DWORD cbRead;
  17. IMAGE_DOS_HEADER img_dos_hdr;
  18. PIMAGE_OS2_HEADER pimg_os2_hdr;
  19. IMAGE_NT_HEADERS img_nt_hdrs;
  20. HRESULT hr = BIN_E_MACHINE_MISMATCH;
  21. HANDLE hFile = INVALID_HANDLE_VALUE;
  22. if((hFile = CreateFile(lpszFile, GENERIC_READ, FILE_SHARE_READ, NULL,
  23. OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0)) == INVALID_HANDLE_VALUE)
  24. {
  25. goto done;
  26. }
  27. //Read the MS-DOS header (all windows executables start with an MS-DOS stub)
  28. if(!ReadFile(hFile, &img_dos_hdr, sizeof(img_dos_hdr), &cbRead, NULL) ||
  29. cbRead != sizeof(img_dos_hdr))
  30. {
  31. goto done;
  32. }
  33. //Verify that the executable has the MS-DOS header
  34. if(img_dos_hdr.e_magic != IMAGE_DOS_SIGNATURE)
  35. {
  36. hr = BIN_E_BAD_FORMAT;
  37. goto done;
  38. }
  39. //Move file pointer to the actual PE header (NT header)
  40. if(SetFilePointer(hFile, img_dos_hdr.e_lfanew, 0, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
  41. {
  42. goto done;
  43. }
  44. //Read the NT header
  45. if(!ReadFile(hFile, &img_nt_hdrs, sizeof(img_nt_hdrs), &cbRead, NULL) ||
  46. cbRead != sizeof(img_nt_hdrs))
  47. {
  48. goto done;
  49. }
  50. //Check for NT signature in the header (we dont support OS2)
  51. if(img_nt_hdrs.Signature != IMAGE_NT_SIGNATURE)
  52. {
  53. goto done;
  54. }
  55. //Check to see if the executable belongs to the correct subsystem
  56. switch(img_nt_hdrs.OptionalHeader.Subsystem)
  57. {
  58. case IMAGE_SUBSYSTEM_NATIVE:
  59. case IMAGE_SUBSYSTEM_WINDOWS_GUI:
  60. case IMAGE_SUBSYSTEM_WINDOWS_CUI:
  61. //If it is a supported subsystem, check CPU architecture
  62. if ( img_nt_hdrs.FileHeader.Machine ==
  63. #ifdef _IA64_
  64. IMAGE_FILE_MACHINE_IA64)
  65. #elif defined _X86_
  66. IMAGE_FILE_MACHINE_I386)
  67. #elif defined _AMD64_
  68. IMAGE_FILE_MACHINE_AMD64)
  69. #else
  70. #pragma message( "Windows Update : Automatic Updates does not support this processor." )
  71. IMAGE_FILE_MACHINE_I386)
  72. #endif
  73. {
  74. hr = S_OK;
  75. }
  76. break;
  77. default:
  78. break;
  79. }
  80. done:
  81. SafeCloseFileHandle(hFile);
  82. return hr;
  83. }