Задача: Утилиты
Исходник: Определение сборки, язык: C++ [code #555, hits: 5590]
автор: - [добавлен: 02.03.2008]
  1. // СПОСОБ 1 >>
  2.  
  3. // mcppfaq_isManaged.cpp
  4. // compile with: /clr
  5. // arguments: mcppfaq_delnative.dll
  6. #using <mscorlib.dll>
  7. using namespace System;
  8. using namespace System::IO;
  9. static bool isManaged(String __gc* sFilename)
  10. {
  11. try
  12. {
  13. Byte Data __gc[] = new Byte __gc[4096];
  14. FileInfo __gc* file = new FileInfo(sFilename);
  15. Stream __gc* fin = file -> Open(FileMode::Open, FileAccess::Read);
  16. Int32 iRead = fin -> Read(Data, 0, 4096);
  17. fin -> Close();
  18. // Verify this is a executable/dll
  19. if ((Data[1] << 8 | Data[0]) != 0x5a4d)
  20. return false;
  21. // This will get the address for the WinNT header
  22. Int32 iWinNTHdr = Data[63]<<24 | Data[62]<<16 | Data[61] << 8 | Data[60];
  23. // Verify this is an NT address
  24. if ((Data[iWinNTHdr+3] << 24 | Data[iWinNTHdr+2] << 16
  25. | Data[iWinNTHdr+1] << 8
  26. | Data[iWinNTHdr]) != 0x00004550)
  27. return false;
  28. Int32 iLightningAddr = iWinNTHdr + 24 + 208;
  29. Int32 iSum=0;
  30. Int32 iTop = iLightningAddr + 8;
  31. for (int i = iLightningAddr; i < iTop; i++)
  32. iSum|=Data[i];
  33. if (iSum == 0)
  34. return false;
  35. else
  36. return true;
  37. }
  38.  
  39. catch(Exception __gc* e)
  40. {
  41. throw (e);
  42. }
  43. }
  44.  
  45. int main(int argc, char * argv[])
  46. {
  47. System::Console::WriteLine(isManaged(argv[1]));
  48. }
  49.  
  50. // СПОСОБ 2 >>
  51. static bool isManaged( String^ sFilename )
  52. {
  53. try
  54. {
  55. array<Byte>^ Data = gcnew array<Byte>(4096);
  56. FileInfo^ file = gcnew FileInfo(sFilename);
  57. if( !file->Exists )
  58. {
  59. return false;
  60. }
  61. Stream^ fin = file->Open( FileMode::Open, FileAccess::Read );
  62. __int32 iRead = fin->Read(Data, 0, 4096);
  63. fin->Close();
  64. // Verify this is a executable/dll
  65. if( (Data[1] << 8 | Data[0]) != 0x5a4d )
  66. {
  67. return false;
  68. }
  69. // This will get the address for the WinNT header
  70. __int32 iWinNTHdr = Data[63]<<24 | Data[62]<<16 | Data[61] << 8 | Data[60];
  71. // Verify this is an NT address
  72. if ((Data[iWinNTHdr+3] << 24 | Data[iWinNTHdr+2] << 16
  73. | Data[iWinNTHdr+1] << 8
  74. | Data[iWinNTHdr]) != 0x00004550)
  75. {
  76. return false;
  77. }
  78. __int32 iLightningAddr = iWinNTHdr + 24 + 208;
  79. __int32 iSum=0;
  80. __int32 iTop = iLightningAddr + 8;
  81. for (int i = iLightningAddr; i < iTop; i++)
  82. {
  83. iSum|=Data[i];
  84. }
  85. return (iSum == 0 ? false : true);
  86. }catch(Exception^ e)
  87. {
  88. throw (e);
  89. }
  90. return false;
  91. }
источник: http://rsdn.ru/Forum/Message.aspx?mid=508204#508204

+добавить реализацию