开发者

Cannot copy pointer (IntPtr) to byte[] from method CryptGetHashParam

Ive been working on this the whole day, and im still stuck i ported this code from c/c++ to c# im so close but i get these exceptions

Exception of type 'System.ExecutionEngineException' was thrown. and Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

here is the code, the code is not cleaned up/optimized yet cause im still testing it

    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);开发者_StackOverflow

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

hope anybody can help me out here,

Thnx in advance

JB


I fixed it without the use of unsafe or the fixed statement, what i did was 2 simple like most of the codings tmp issues

i have this class Crypto where i have all advapi.dll functions in and the function returned a pointer to the byte array in memory and this is what the function needed before my change.

        [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

i changed the function to

        [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

and that solved my corrupt memory issue Hope this issue helps some body else working with CryptGetHashParam

i ported this code from c/c++ cause there where no c# sample on the net, so here is one of the first.

thnx all for trying to help me out, but i fixed it myself

JB


I'm not certain, but it's likely because your .Net objects aren't pinned in memory. See this: http://dotnet.dzone.com/news/net-memory-control-use-gchandl. The gist of it is that .Net objects can be moving around in memory after you've passed them through interop, and when that happens stuff starts getting crazy.

Unfortunately I'm on a netbook at the moment and can't try it myself. Does that help?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜