2013-05-21 29 views
0
[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; 
    } 
} 

這是我想要執行解壓縮的類。試圖讀取或寫入受保護的內存。這通常表明其他內存在c#中已損壞(訪問衝突)

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

但它給錯誤的「嘗試讀取或寫入受保護的內存。這通常是指示其他內存已損壞。」

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

是我想要的PInvoke功能。 希望能找到最好的解決方案,正如我總是在這裏找到的。提前致謝。

+0

我對這些聲明爲'ref'的數組持懷疑態度。 – Medinoc

+1

是的,陣列是問題。我無法理解本地函數的文檔。 –

+0

但那麼什麼是解決方案,因爲我應該實現這個任何如何? – Nikki

回答

1

爲什麼你沒有使用Xceed的CSharp Lib或其他Zip庫?
你應該定義你的委託作爲

公共委託INT DecompressMCX(INT HCOMP,IntPtr的中,UINT in_len,IntPtr的了,裁判UINT out_len,布爾EOD);

當生成in IntPrt時,修復是很重要的,這樣垃圾收集器在壓縮運行時不會移動in數據。

+0

,因爲我想解壓其中一個證券交易所的實況多播數據字節,其API說使用xceedzip解壓縮。作爲一個小項目,購買如此昂貴的.net組件對我來說是不可能的。 – Nikki

+0

謝謝,你的soln改變了委託移除上面的錯誤,但仍然沒有實現解壓縮。 – Nikki

+0

我希望xceedzip基本上是zlib。你也可以嘗試爲流(IO.Streams)使用.NET框架類。哪個交易所提供這樣一個奇怪的建議。 – weismat

相關問題