无法从方法 CryptGetHashParam 复制指针 (IntPtr) 到 byte[]

发布于 2024-10-13 10:12:04 字数 2715 浏览 4 评论 0原文

我已经为此工作了一整天,但我仍然卡住了 我将此代码从 c/c++ 移植到 c#,非常接近,但我得到了这些异常

“System.ExecutionEngineException”类型的异常被抛出。 和 尝试读取或写入受保护的内存。这通常表明其他内存已损坏。

这是代码,代码尚未清理/优化,因为我仍在测试它

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

希望任何人都可以在这里帮助我,

提前谢谢

JB

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);

        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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青春有你 2024-10-20 10:12:04

我修复了它,没有使用 unsafe 或 fixed 语句,我所做的很简单,就像大多数编码 tmp 问题一样,

我有这个类 Crypto,其中我有所有 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# 示例,所以这里是第一个。

谢谢大家试图帮助我,但我自己修好了

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

缘字诀 2024-10-20 10:12:04

我不确定,但这可能是因为您的 .Net 对象没有固定在内存中。请参阅:http://dotnet.dzone.com/news/net-内存控制使用-gchandl。其要点是,在通过互操作传递 .Net 对象后,它们可以在内存中移动,当这种情况发生时,事情就会开始变得疯狂。

不幸的是,我现在使用的是上网本,无法亲自尝试。这有帮助吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文