2

安全でないコードを使用してファイルのバージョン情報を取得する単純なユーティリティがあります。これを混合プラットフォーム (vs2008/.net 3.5) としてコンパイルし、64 ビット マシンに展開すると、ヒープ破損エラーが発生します。x86として再コンパイルすると、すべてが機能します....

.NET Common Type System を理解していた私にとって、これは驚くべきことでした。私の安全でないコードは、short へのポインタと byte へのポインタを使用しています。これらの一般的なタイプは、CTS のためにどのプラットフォームでも同じサイズですか? ここで何が欠けていますか

using System;
using System.Reflection;
using System.Runtime.InteropServices;


public class Win32Imports
{
    [DllImport("version.dll")]
    public static extern bool GetFileVersionInfo(string sFileName,
          int handle, int size, byte[] infoBuffer);

    [DllImport("version.dll")]
    public 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 public 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 public static extern bool VerQueryValue(byte[] pBlock,
          string pSubBlock, out short* pValue, out uint len);
}

public class FileInformation
{
    // Main is marked with 'unsafe' because it uses pointers:
    unsafe public static string GetVersionInformation(string path)
    {

        // Figure out how much version info there is:
        int handle = 0;
        int size =
              Win32Imports.GetFileVersionInfoSize(path,
              out handle);
        if (size == 0) return "";

        byte[] buffer = new byte[size];
        if (!Win32Imports.GetFileVersionInfo(path, handle, size, buffer)) return "";

        // Get the locale info from the version info:
        short* subBlock = null;
        uint len = 0;           
        if (!Win32Imports.VerQueryValue(buffer, @"\VarFileInfo\Translation", out subBlock, out len)) return "";


        // Get the ProductVersion value for this program:
        string spv = @"\StringFileInfo\" + subBlock[0].ToString("X4") + subBlock[1].ToString("X4") + @"\ProductVersion";
        byte* pVersion = null;
        string versionInfo;
        if (!Win32Imports.VerQueryValue(buffer, spv, out versionInfo, out len)) return "";
        return versionInfo;

    }
}

すべてのゾンビのキラーに感謝し、ゾンビの黙示録を待っています....... -ジョナサン

4

2 に答える 2

2

このためにFileVersionInfo マネージドクラスを使用できなかった理由はありますか?32ビットと64ビットの両方のプラットフォームで正しく動作すると思います。

于 2010-08-11T14:51:39.993 に答える
1

おそらく、ポインターはプラットフォームで64ビットですが、安全でないものを使用しているため、これは正しく変換されませんか?また、p/invokeのインポートがどのように見えるかを示してください。

于 2010-08-11T14:55:37.160 に答える