Dushyant Joshi
Dushyant Joshi

Reputation: 171

Attempted to read or write protected memory. This is often an indication that other memory is corrupt in c# (Access violation)

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate int DecompressMCX(object hComp,ref byte[] @in, uint @in_len, ref byte[] @out, ref uint out_len, bool eod);

public class XceedCompressor
{

    [DllImport("kernel32.dll")]
    public static extern IntPtr LoadLibrary(string dllToLoad);

    [DllImport("kernel32.dll")]
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);       

    byte[] OutRec = new byte[1024 * 100];
    uint outlen;
    DecompressMCX DecompressDelegate;
    int b ;
    unsafe int l;

    public XceedCompressor()
    {
        IntPtr pDll = LoadLibrary(@"xceedzip.dll");
        IntPtr pAddressOfFunctionToCall = GetProcAddress(pDll, "XcUncompress");
        DecompressDelegate = (DecompressMCX)Marshal.GetDelegateForFunctionPointer(pAddressOfFunctionToCall, typeof(DecompressMCX));
    }

    public byte[] Decompress(byte[] InRecArr)
    {
        outlen = 0;
        l = DecompressDelegate(b, ref InRecArr, (uint)InRecArr.Length, ref OutRec, ref outlen, true);
        return OutRec;
    }
}

This is my class where I want to perform decompression.

XceedCompressor xcd = new XceedCompressor ();
xcd.Decompress(some data already compressed with the same library);

But its giving error as "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

http://doc.xceedsoft.com/products/Xceedzip/Uncompress_method.html

is the function which I want to pinvoke. Hope for the best solution, as I always find here. Thanks in advance.

Upvotes: 0

Views: 1451

Answers (1)

weismat
weismat

Reputation: 7411

Any reason why you are not using Xceed's CSharp Lib or an alternative Zip library?
You should define your delegate as

public delegate int DecompressMCX(int hComp,IntPtr in, uint in_len, IntPtr out, ref uint out_len, bool eod);

When generating the in IntPtr, it is important to fix it, so that the Garbage collector does not move the in data while the compression is running.

Upvotes: 1

Related Questions