C#でWin32APIを使ってDLL等のファイルバージョンを取得したいです。
有るサイトの以下サンプルソースを使ってみたのですが、
2個目のVerQueryValueでエラーもでずにアプリが終了してしまいます。
どうすればうまく取得できますでしょうか。
よろしくお願いしますm(__)m
public class FileVersionInfo
{
[DllImport("version.dll")]
private static extern bool GetFileVersionInfo(string sFileName,
int handle, int size, byte[] infoBuffer);
[DllImport("version.dll")]
private static extern int GetFileVersionInfoSize(string sFileName,
out int handle);
// The third parameter – “out string pValue” – is automatically
// marshaled from ANSI to Unicode:
[DllImport("version.dll")]
unsafe private static extern bool VerQueryValue(byte[] pBlock,
string pSubBlock, out string pValue, out uint len);
// This VerQueryValue overload is marked with ‘unsafe’ because
// it uses a short*:
[DllImport("version.dll")]
unsafe private static extern bool VerQueryValue(byte[] pBlock,
string pSubBlock, out short* pValue, out uint len);
public static unsafe string GetFileVersionNameInfo(string path)
{
string name = null;
int handle = 0;
int size = GetFileVersionInfoSize(path, out handle);
if (size != 0)
{
byte[] buffer = new byte[size];
if (GetFileVersionInfo(path, handle, size, buffer))
{
uint len = 0;
short* subBlock = null;
if (VerQueryValue(buffer, @"\VarFileInfo\Translation", out subBlock, out len) && len > 2)
{
string spv = @"\StringFileInfo\" + subBlock[0].ToString("X4") + subBlock[1].ToString("X4") + @"\FileDescription";
byte * pVersion = null;
string versionInfo;
if (VerQueryValue(buffer, spv, out versionInfo, out len))
{
name = versionInfo;
}
}
}
}
if (name == null)
{
name = Path.GetFileNameWithoutExtension(path);
}
return name;
}
}