| 1 |
using System; |
| 2 |
using System.Reflection; |
| 3 |
using System.Runtime.InteropServices; |
| 4 |
using System.Diagnostics; |
| 5 |
|
| 6 |
namespace nft.win32util |
| 7 |
{ |
| 8 |
/// <summary> |
| 9 |
/// ConfigureService ĚTvĚŕžĹˇB |
| 10 |
/// </summary> |
| 11 |
public class Win32Version |
| 12 |
{ |
| 13 |
[DllImport("version.dll")] |
| 14 |
static private extern bool GetFileVersionInfo (string sFileName, |
| 15 |
int handle, int size, byte[] infoBuffer); |
| 16 |
[DllImport("version.dll")] |
| 17 |
static private extern int GetFileVersionInfoSize (string sFileName, |
| 18 |
out int handle); |
| 19 |
// The third parameter - "out string pValue" - is automatically |
| 20 |
// marshaled from ANSI to Unicode: |
| 21 |
[DllImport("version.dll")] |
| 22 |
unsafe static private extern bool VerQueryValue (byte[] pBlock, |
| 23 |
string pSubBlock, out string pValue, out uint len); |
| 24 |
// This VerQueryValue overload is marked with 'unsafe' because |
| 25 |
// it uses a short*: |
| 26 |
[DllImport("version.dll")] |
| 27 |
unsafe static private extern bool VerQueryValue (byte[] pBlock, |
| 28 |
string pSubBlock, out short *pValue, out uint len); |
| 29 |
|
| 30 |
private Win32Version() |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
unsafe static public string GetAssemblyFileVersion(Assembly target) |
| 35 |
{ |
| 36 |
string fname = (new Uri(target.CodeBase)).LocalPath; |
| 37 |
int handle = 0; |
| 38 |
// Figure out how much version info there is: |
| 39 |
int size = |
| 40 |
GetFileVersionInfoSize(fname, out handle); |
| 41 |
|
| 42 |
if (size == 0) return "N/A"; |
| 43 |
|
| 44 |
byte[] buffer = new byte[size]; |
| 45 |
|
| 46 |
if (!GetFileVersionInfo(fname, handle, size, buffer)) |
| 47 |
{ |
| 48 |
Debug.WriteLine("Failed to query file version information."); |
| 49 |
return "N/A"; |
| 50 |
} |
| 51 |
short *subBlock = null; |
| 52 |
uint len = 0; |
| 53 |
// Get the locale info from the version info: |
| 54 |
if (!VerQueryValue (buffer, @"\VarFileInfo\Translation", out subBlock, out len)) |
| 55 |
{ |
| 56 |
Debug.WriteLine("Failed to query version information."); |
| 57 |
return "N/A"; |
| 58 |
} |
| 59 |
|
| 60 |
string spv = @"\StringFileInfo\" + subBlock[0].ToString("X4") + subBlock[1].ToString("X4") + @"\ProductVersion"; |
| 61 |
|
| 62 |
byte *pVersion = null; |
| 63 |
// Get the ProductVersion value for this program: |
| 64 |
string versionInfo; |
| 65 |
|
| 66 |
if (!VerQueryValue (buffer, spv, out versionInfo, out len)) |
| 67 |
{ |
| 68 |
Debug.WriteLine("Failed to query version information."); |
| 69 |
return "N/A"; |
| 70 |
} |
| 71 |
return versionInfo; |
| 72 |
} |
| 73 |
} |
| 74 |
} |