下面是我从在线资源中获得的代码示例,但它假定可以与全框架一起使用,但是当我尝试使用C#智能设备进行构建时,它抛出异常,表明它的内存不足。有人知道如何修复它以便在紧凑型计算机上使用吗?当我第二次调用VerQueryValue时,内存不足异常是最后一个。

谢谢,

    [DllImport("coredll.dll")]
    public static extern bool VerQueryValue(byte[] buffer, string subblock, out IntPtr blockbuffer, out uint len);

    [DllImport("coredll.dll")]
    public static extern bool VerQueryValue(byte[] pBlock, string pSubBlock, out string pValue, out uint len);
    //
    private static void GetAssemblyVersion()
    {

        string filename = @"\Windows\MyLibrary.dll";
        if (File.Exists(filename))
        {
            try                {

                int handle = 0;
                Int32 size = 0;
                size = GetFileVersionInfoSize(filename, out handle);

                if (size > 0)
                {
                    bool retValue;
                    byte[] buffer = new byte[size];
                    retValue = GetFileVersionInfo(filename, handle, size, buffer);
                    if (retValue == true)
                    {
                        bool success = false;
                        IntPtr blockbuffer = IntPtr.Zero;
                        uint len = 0;
                        //success = VerQueryValue(buffer, "\\", out blockbuffer, out len);
                        success = VerQueryValue(buffer, @"\VarFileInfo\Translation", out blockbuffer, out len);

                        if(success)
                        {
                            int p = (int)blockbuffer;

                            //Reads a 16-bit signed integer from unmanaged memory
                            int j = Marshal.ReadInt16((IntPtr)p);
                            p += 2;

                            //Reads a 16-bit signed integer from unmanaged memory
                            int k = Marshal.ReadInt16((IntPtr)p);

                            string sb = string.Format("{0:X4}{1:X4}", j, k);
                            string spv = @"\StringFileInfo\" + sb + @"\ProductVersion";

                            string versionInfo;
                            VerQueryValue(buffer, spv, out versionInfo, out len);
                        }
                    }
                }
            }
            catch (Exception err)
            {
                string error = err.Message;
            }
        }
    }

最佳答案

添加以下两个语句后:

                            Int32 dwVerMinor = j & 0xffff;
                            Int32 dwVerBuild = k & 0xffff;


它能够检索DLL版本。

关于c# - C#PInvoke VerQueryValue返回OutOfMemoryException吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/1247249/

10-11 02:36