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.

201 lines
3.5 KiB

  1. #include <nt.h>
  2. #include <ntrtl.h>
  3. #include <nturtl.h>
  4. #include <windows.h>
  5. #include <setupapi.h>
  6. #include "..\inc\spapip.h"
  7. #include <stdio.h>
  8. NTSTATUS
  9. MyGetFileVersion(
  10. IN PVOID ImageBase
  11. );
  12. VOID
  13. __cdecl
  14. wmain(
  15. IN int argc,
  16. IN WCHAR *argv[]
  17. )
  18. {
  19. NTSTATUS Status;
  20. DWORD d;
  21. DWORD FileSize;
  22. HANDLE FileHandle;
  23. HANDLE MappingHandle;
  24. PVOID ImageBase;
  25. //
  26. // Open and map file for read.
  27. //
  28. d = OpenAndMapFileForRead(argv[1],&FileSize,&FileHandle,&MappingHandle,&ImageBase);
  29. if(d == NO_ERROR) {
  30. //
  31. // For some reason you have to set the low bit to make this work
  32. //
  33. MyGetFileVersion((PVOID)((ULONG)ImageBase | 1));
  34. UnmapAndCloseFile(FileHandle,MappingHandle,ImageBase);
  35. } else {
  36. printf("Couldn't open %ws\n",argv[1]);
  37. }
  38. }
  39. NTSTATUS
  40. MyGetFileVersion(
  41. IN PVOID ImageBase
  42. )
  43. {
  44. PIMAGE_RESOURCE_DATA_ENTRY DataEntry;
  45. NTSTATUS Status;
  46. ULONG IdPath[3];
  47. ULONG ResourceSize;
  48. struct {
  49. USHORT TotalSize;
  50. USHORT DataSize;
  51. USHORT Type;
  52. WCHAR Name[16]; // L"VS_VERSION_INFO" + unicode nul
  53. VS_FIXEDFILEINFO FixedFileInfo;
  54. } *Resource;
  55. ULONG VerMS,VerLS;
  56. IdPath[0] = (ULONG)RT_VERSION;
  57. IdPath[1] = (ULONG)MAKEINTRESOURCE(VS_VERSION_INFO);
  58. IdPath[2] = 0;
  59. Status = LdrFindResource_U(ImageBase,IdPath,3,&DataEntry);
  60. if(!NT_SUCCESS(Status)) {
  61. printf("Not a PE image or no version resources\n");
  62. goto c0;
  63. }
  64. Status = LdrAccessResource(ImageBase,DataEntry,&Resource,&ResourceSize);
  65. if(!NT_SUCCESS(Status)) {
  66. printf("Unable to access version resources\n");
  67. goto c0;
  68. }
  69. if((ResourceSize >= sizeof(*Resource)) && !_wcsicmp(Resource->Name,L"VS_VERSION_INFO")) {
  70. VerMS = Resource->FixedFileInfo.dwFileVersionMS;
  71. VerLS = Resource->FixedFileInfo.dwFileVersionLS;
  72. printf(
  73. "%u.%u.%u.%u\n",
  74. VerMS >> 16,
  75. VerMS & 0xffff,
  76. VerLS >> 16,
  77. VerLS & 0xffff
  78. );
  79. } else {
  80. printf("Invalid version resources");
  81. }
  82. c0:
  83. return(Status);
  84. }
  85. #if 0
  86. LPUNKNOWN pUnkOuter;
  87. IShellLink *psl;
  88. IPersistFile *ppf;
  89. CShellLink *this;
  90. BOOL b;
  91. b = FALSE;
  92. //
  93. // Create an IShellLink and query for IPersistFile
  94. //
  95. if(FAILED(SHCoCreateInstance(NULL,&CLSID_ShellLink,pUnkOuter,&IID_IShellLink,&psl))) {
  96. goto c0;
  97. }
  98. if(FAILED(psl->lpVtbl->QueryInterface(psl,&IID_IPersistFile,&ppf))) {
  99. goto c1;
  100. }
  101. //
  102. // Load the link from disk and get a pointer to
  103. // the actual link data.
  104. //
  105. if(FAILED(ppf->lpVtbl->Load(ppf,argv[1],0))) {
  106. goto c2;
  107. }
  108. this = IToClass(CShellLink,sl,psl);
  109. //
  110. // Remove the link tracking data.
  111. //
  112. Link_RemoveExtraDataSection(this,EXP_TRACKER_SIG);
  113. //
  114. // Save the link back out.
  115. //
  116. if(FAILED(ppf->lpVtbl->Save(ppf,argv[1],TRUE))) {
  117. goto c2;
  118. }
  119. //
  120. // Success.
  121. //
  122. b = TRUE;
  123. c2:
  124. //
  125. // Release the IPersistFile object
  126. //
  127. ppf->lpVtbl->Release(ppf);
  128. c1:
  129. //
  130. // Release the IShellLink object
  131. //
  132. psl->lpVtbl->Release(psl);
  133. c0:
  134. return(b);
  135. #endif