2011-01-24 27 views
1

我一直都在做這個了整整一天,和IM仍然停留 我移植的C/C++到C#即時如此接近這個代碼,但我得到這些異常無法複製指針(IntPtr的)從方法CryptGetHashParam的byte []

引發了'System.ExecutionEngineException'類型的異常。 和 試圖讀取或寫入受保護的內存。這通常表明其他內存已損壞。

這裏是代碼,代碼不事先

JB清理/優化還導致我還在測試它

public unsafe static void GetHash(string data, byte[] hash) 
    { 
     byte[] input = System.Text.UnicodeEncoding.Unicode.GetBytes(data); 
     hash = new byte[128]; 
     IntPtr hProv = IntPtr.Zero; 
     IntPtr hHash = IntPtr.Zero; 

     Crypto.CryptAcquireContext(ref hProv, string.Empty, string.Empty, Crypto.PROV_RSA_FULL, 0); 

     if (Crypto.CryptCreateHash(hProv, Crypto.CALG_SHA1, IntPtr.Zero, 0, ref hHash)) 
     { 
      if (Crypto.CryptHashData(hHash, input, ((input.Length) + 1) * 2, 0)) 
      { 
       byte[] buffer = new byte[20]; 
       IntPtr pBuffer = IntPtr.Zero; 
       int length = 20; 

       if (Crypto.CryptGetHashParam(hHash, Crypto.HP_HASHVAL, ref pBuffer, ref length, 0)) 
       { 
        Crypto.CryptDestroyHash(hHash); 
        Crypto.CryptReleaseContext(hProv, 0); 
        byte tail = 0; 

        unsafe 
        { 
         //no matter what i do it stops here!!!!! :(
         //one error is "Exception of type 'System.ExecutionEngineException' was thrown." 
         //the other is "System.AccessViolationException crossed a native/managed boundary 
         //Attempted to read or write protected memory. This is often an indication that other memory is corrupt." 

         try 
         { 
          //-------------------------- This is where the exepctions starts 
          //I have commented the code, cause im kinda getting tired of this Exception 
          //I tried 2 ways of getting a byte[] from a pointer 

          //the 1e way, does not work 
          //for (int i = 0; i < length; i++) 
           //buffer[i] = (byte)Marshal.ReadByte(pBuffer, i); 

          //the 2e way does not work 
          //System.Runtime.InteropServices.Marshal.Copy(pBuffer,buffer, 0, 20); 

          //-------------------------- 
         } 
         catch (Exception ex) 
         { 

         } 
        } 

        //there is more code here, but i removed 
        //since i only want till where code goes sofare 
       } 
      } 
     } 
    } 

希望有人能幫助我在這裏,

日Thnx

回答

2

我固定它不使用不安全或固定的說法,我所做的是2個簡單的像極了值編碼TMP問題

我有這個類加密在那裏我都advapi.dll功能和函數返回一個指向內存中字節數組的指針,這是我更改之前所需的函數。

 [DllImport("advapi32.dll", SetLastError = true)] 
    public static extern bool CryptGetHashParam(
     IntPtr hHash, 
     Int32 dwParam, 
     ref IntPtr pbData, // this is where my problem was!!!! 
     ref Int32 pdwDataLen, 
     Int32 dwFlags 

我改變了功能

 [DllImport("advapi32.dll", SetLastError = true)] 
    public static extern bool CryptGetHashParam(
     IntPtr hHash, 
     Int32 dwParam, 
     Byte[] pbData, //i changed it from IntPtr to byte array 
     ref Int32 pdwDataLen, 
     Int32 dwFlags 

而且解決了我的內存破壞問題 希望這個問題可以幫助一些身體其他與CryptGetHashParam

工作我移植的代碼從C/C++的原因那裏沒有網上的C#示例,所以這裏是第一個。

日Thnx所有試圖幫助我,但我固定它自己

JB

+0

你可以發佈最終的工作代碼嗎?我真的很感激。 –

+0

你將pbData緩衝區大小設置爲? –

1

我不確定,但可能是因爲你的.Net對象沒有固定在內存中。看到這個:http://dotnet.dzone.com/news/net-memory-control-use-gchandl。它的要點在於.Net對象可以在你通過interop傳遞它們之後在內存中移動,當這種情況發生時,東西開始變得瘋狂。

不幸的是,我現在在上網本,不能自己嘗試。這有幫助嗎?

+0

日Thnx,我會讀你對我的頁面,然後我會嘗試看看,如果我可以修復它像。至少爲年的迴應thnx。 – JohnnBlade

+0

如果它適合你,非常好 - 我會很高興答案被接受。如果不讓我知道,我會進一步研究你的問題。 – Amy

+0

thnx幫助尤達我欣賞它,但我設法修復它自己! – JohnnBlade