2009-08-07 79 views
2

下面是我從在線資源獲取的代碼示例,但它假設使用全框架,但是當我嘗試使用C#智能設備構建它時,它會拋出異常,說它內存不足。有誰知道我怎樣才能解決它使用緊湊?當我第二次調用最後一個VerQueryValue時,出現內存不足異常。C#PInvoke VerQueryValue返回OutOfMemoryException?

感謝,

[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; 
      } 
     } 
    } 

回答

0

加入這兩個語句後:

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

它能夠獲取DLL版本。