Prechádzať zdrojové kódy

More code to debug .exe version reading + Displaying .exe SHA-256 hash in hex

(cherry picked from commit 0a94b40638c07d0ddf65a78faba75443ce8297af)

Source commit: a0d0ca2fd720931909ffe811360626f189d57c85
Martin Prikryl 4 rokov pred
rodič
commit
69dfca18ac
1 zmenil súbory, kde vykonal 52 pridanie a 1 odobranie
  1. 52 1
      dotnet/internal/ExeSessionProcess.cs

+ 52 - 1
dotnet/internal/ExeSessionProcess.cs

@@ -1020,6 +1020,19 @@ namespace WinSCP
         [DllImport("version.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false)]
         public static extern int GetFileVersionInfoSize(string lptstrFilename, out int handle);
 
+        [DllImport("kernel32.dll", SetLastError = true)]
+        static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, uint dwFlags);
+        
+        [DllImport("kernel32.dll", SetLastError = true)]
+        [return: MarshalAs(UnmanagedType.Bool)]
+        static extern bool FreeLibrary(IntPtr hModule);
+
+        [DllImport("kernel32.dll", SetLastError = true)]
+        static extern IntPtr FindResource(IntPtr hModule, string lpName, string lpType);
+
+        [DllImport("kernel32.dll", SetLastError = true)]
+        static extern uint SizeofResource(IntPtr hModule, IntPtr hResInfo);
+
         private void CheckVersion(string exePath, FileVersionInfo assemblyVersion)
         {
             using (_logger.CreateCallstack())
@@ -1075,7 +1088,7 @@ namespace WinSCP
                             using (SHA256 SHA256 = SHA256.Create())
                             using (FileStream stream = File.OpenRead(exePath))
                             {
-                                string sha256 = Convert.ToBase64String(SHA256.ComputeHash(stream));
+                                string sha256 = string.Concat(Array.ConvertAll(SHA256.ComputeHash(stream), b => b.ToString("x2")));
                                 _logger.WriteLine($"SHA-256 of the executable file is {sha256}");
                             }
                         }
@@ -1085,6 +1098,44 @@ namespace WinSCP
                             _logger.WriteException(e);
                         }
 
+                        try
+                        {
+                            IntPtr library = LoadLibraryEx(exePath, IntPtr.Zero, 0x00000002); // LOAD_LIBRARY_AS_DATAFILE
+                            if (library == IntPtr.Zero)
+                            {
+                                _logger.WriteLine("Cannot load");
+                                _logger.WriteException(new Win32Exception());
+                            }
+                            else
+                            {
+                                IntPtr resource = FindResource(library, "#1", "#16");
+                                if (resource == IntPtr.Zero)
+                                {
+                                    _logger.WriteLine("Cannot find version resource");
+                                    _logger.WriteException(new Win32Exception());
+                                }
+                                else
+                                {
+                                    uint resourceSize = SizeofResource(library, resource);
+                                    if (resourceSize == 0)
+                                    {
+                                        _logger.WriteLine("Cannot find size of version resource");
+                                        _logger.WriteException(new Win32Exception());
+                                    }
+                                    else
+                                    {
+                                        _logger.WriteLine($"Version resource size is {resourceSize}");
+                                    }
+                                }
+                                FreeLibrary(library);
+                            }
+                        }
+                        catch (Exception e)
+                        {
+                            _logger.WriteLine("Querying version resource failed");
+                            _logger.WriteException(e);
+                        }
+
                         string message;
                         if (string.IsNullOrEmpty(version.ProductVersion) && (accessException != null))
                         {