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.

131 lines
2.7 KiB

  1. // version.cpp : Defines the entry point for the console application.
  2. //
  3. #include <windows.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <ntverp.h>
  7. #include <tchar.h>
  8. static BOOL WriteToFile( HANDLE hFile, const char *psz );
  9. static void PrintUsage();
  10. int __cdecl
  11. main( int argc, char **argv )
  12. {
  13. const char *pszOutFile = NULL;
  14. if( 1 == argc )
  15. {
  16. PrintUsage();
  17. return 1;
  18. }
  19. for( int i = 1; i < argc; i++ )
  20. {
  21. if( 0 == _stricmp( argv[ i ], "-outFile" ) && ( i + 1 < argc ) )
  22. {
  23. pszOutFile = argv[ i + 1 ];
  24. break;
  25. }
  26. else
  27. {
  28. PrintUsage();
  29. return 1;
  30. }
  31. }
  32. const char *pszUsings = "using System.Reflection;\r\nusing System.Security.Permissions;\r\nusing System.Runtime.CompilerServices;\r\n\r\n";
  33. const char *pszVersionTemplate = "[assembly: AssemblyVersion(\"%s\")]\r\n";
  34. char szVersion[ 256 ];
  35. char szBuf[ 32 ];
  36. memset( szBuf, 0, 32 * sizeof( char ) );
  37. memset( szVersion, 0, 256 * sizeof( char ) );
  38. //
  39. // VER_PRODUCTVERSION_STR is a preprocessor symbol, defined in ntverp.h.
  40. // It contains the string-ized Windows version number.
  41. //
  42. char *psz = strncpy( szBuf, VER_PRODUCTVERSION_STR, 32 );
  43. szBuf[ 31 ] = 0x00;
  44. if( NULL == psz )
  45. {
  46. printf( "Preprocessor symbol VER_PRODUCTVERSION_STR is not defined. Giving up.\n" );
  47. return -1;
  48. }
  49. sprintf( szVersion, pszVersionTemplate, szBuf );
  50. HANDLE hFile = CreateFileA( pszOutFile,
  51. GENERIC_WRITE,
  52. 0,
  53. NULL,
  54. CREATE_ALWAYS,
  55. FILE_ATTRIBUTE_NORMAL,
  56. NULL );
  57. if( NULL == hFile )
  58. {
  59. printf( "Could not create the following file: %s. Giving up.\n", pszOutFile );
  60. return -1;
  61. }
  62. BOOL fSuccess = FALSE;
  63. fSuccess = WriteToFile( hFile, pszUsings );
  64. if( !fSuccess )
  65. {
  66. printf( "Error writing to file: %s. Giving up.\n", pszOutFile );
  67. CloseHandle( hFile );
  68. return -1;
  69. }
  70. fSuccess = WriteToFile( hFile, szVersion );
  71. if( !fSuccess )
  72. {
  73. printf( "Error writing to file: %s. Giving up.\n", argv[ 1 ] );
  74. CloseHandle( hFile );
  75. return -1;
  76. }
  77. CloseHandle( hFile );
  78. return 0;
  79. }
  80. BOOL
  81. WriteToFile( HANDLE hFile, const char *psz )
  82. {
  83. if( ( NULL == hFile ) || ( NULL == psz ) )
  84. {
  85. return FALSE;
  86. }
  87. DWORD dwBytesToWrite = strlen( psz );
  88. DWORD dwBytesWritten = 0;
  89. BOOL fSuccess = FALSE;
  90. fSuccess = WriteFile( hFile,
  91. reinterpret_cast<const void *>( psz ),
  92. dwBytesToWrite,
  93. &dwBytesWritten,
  94. NULL );
  95. if( !fSuccess || ( dwBytesToWrite != dwBytesWritten ) )
  96. {
  97. return FALSE;
  98. }
  99. else
  100. {
  101. return TRUE;
  102. }
  103. }
  104. void
  105. PrintUsage()
  106. {
  107. printf( "Usage of version.exe:\r\n\r\n" );
  108. printf( "version.exe <-outFile <path to output file> >\r\n" );
  109. printf( "version.exe <-help>\r\n" );
  110. }