[C#] C# 中使用非托管代码(使用指针的代码) →→→→→进入此内容的聊天室

来自 , 2020-11-24, 写在 C#, 查看 144 次.
URL http://www.code666.cn/view/3bc3e78c
  1. // printversion.cs
  2. // 编译时使用:/unsafe
  3. using System;
  4. using System.Reflection;
  5. using System.Runtime.InteropServices;
  6.  
  7. // 为此程序集指定一个版本号:
  8. [assembly:AssemblyVersion("4.3.2.1")]
  9.  
  10. public class Win32Imports
  11. {
  12.         [DllImport("version.dll")]
  13.         public static extern bool GetFileVersionInfo (string sFileName,
  14.                 int handle, int size, byte[] infoBuffer);
  15.         [DllImport("version.dll")]
  16.         public static extern int GetFileVersionInfoSize (string sFileName,
  17.                 out int handle);
  18.    
  19.         // 自动将第三个参数“out string pValue”从 Ansi
  20.         // 封送处理为 Unicode:
  21.         [DllImport("version.dll")]
  22.         unsafe public static extern bool VerQueryValue (byte[] pBlock,
  23.                 string pSubBlock, out string pValue, out uint len);
  24.         // 此 VerQueryValue 重载被标记为“unsafe”,因为
  25.         // 它使用 short*:
  26.         [DllImport("version.dll")]
  27.         unsafe public static extern bool VerQueryValue (byte[] pBlock,
  28.                 string pSubBlock, out short *pValue, out uint len);
  29. }
  30.  
  31. public class C
  32. {
  33.         // Main 被标记为“unsafe”,因为它使用指针:
  34.         unsafe public static int Main ()
  35.         {
  36.                 try
  37.                 {
  38.                         int handle = 0;
  39.                         // 确定有多少版本信息:
  40.                         int size =
  41.                                 Win32Imports.GetFileVersionInfoSize("printversion.exe",
  42.                                 out handle);
  43.  
  44.                         if (size == 0) return -1;
  45.  
  46.                         byte[] buffer = new byte[size];
  47.  
  48.                         if (!Win32Imports.GetFileVersionInfo("printversion.exe", handle, size, buffer))
  49.                         {
  50.                                 Console.WriteLine("Failed to query file version information.");
  51.                                 return 1;
  52.                         }
  53.  
  54.                         short *subBlock = null;
  55.                         uint len = 0;
  56.                         // 从版本信息获取区域设置信息:
  57.                         if (!Win32Imports.VerQueryValue (buffer, @"\VarFileInfo\Translation", out subBlock, out len))
  58.                         {
  59.                                 Console.WriteLine("Failed to query version information.");
  60.                                 return 1;
  61.                         }
  62.  
  63.                         string spv = @"\StringFileInfo\" + subBlock[0].ToString("X4") + subBlock[1].ToString("X4") + @"\ProductVersion";
  64.  
  65.                         byte *pVersion = null;
  66.                         // 获取此程序的 ProductVersion 值:
  67.                         string versionInfo;
  68.                        
  69.                         if (!Win32Imports.VerQueryValue (buffer, spv, out versionInfo, out len))
  70.                         {
  71.                                 Console.WriteLine("Failed to query version information.");
  72.                                 return 1;
  73.                         }
  74.  
  75.                         Console.WriteLine ("ProductVersion == {0}", versionInfo);
  76.                 }
  77.                 catch (Exception e)
  78.                 {
  79.                         Console.WriteLine ("Caught unexpected exception " + e.Message);
  80.                 }
  81.      
  82.                 return 0;
  83.         }
  84. }
  85. //csharp/4927

回复 "C# 中使用非托管代码(使用指针的代码)"

这儿你可以回复上面这条便签

captcha