[英]c# : DLLImport for a particular dll not working, although an example app shows it working, How can i resolve this?
我正在嘗試使用相對路徑導入dll
private const string LzoDll32Bit = @"lib32\lzo_32.dll";
#region Dll-Imports
[DllImport(LzoDll32Bit, EntryPoint = "lzo_version_string")]
private static extern IntPtr lzo_version_string32();
[DllImport(LzoDll32Bit, EntryPoint = "lzo1x_1_compress", CallingConvention = CallingConvention.Cdecl)]
private static extern int lzo1x_1_compress32(byte[] src, int src_len, byte[] dst, ref int dst_len, byte[] wrkmem);
[DllImport(LzoDll32Bit, EntryPoint = "lzo1x_decompress", CallingConvention = CallingConvention.Cdecl)]
private static extern int lzo1x_decompress32(byte[] src, int src_len, byte[] dst, ref int dst_len, byte[] wrkmem);
[DllImport(LzoDll32Bit, EntryPoint = "__lzo_init_v2", CallingConvention = CallingConvention.Cdecl)]
private static extern int __lzo_init_v2_32(uint v, int s1, int s2, int s3, int s4, int s5, int s6, int s7, int s8, int s9);
#endregion
{“無法加載DLL'lib32 \\ lzo_32.dll':找不到指定的模塊。(HRESULT的異常:0x8007007E)“}
現在這是奇怪的東西, http://wallaceturner.com/lzo-for-c ,我下載示例項目的站點的鏈接,以了解如何在c#.net中使用lzo壓縮器。 當我運行該項目時,它會成功運行。 除了項目中的內容,我沒有看到其他依賴項。 我在兩個都使用.Net4.5.2。
這是我該課程的完整源代碼
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace TradeAlgo
{
public class LZOCompressor
{
private static TraceSwitch _traceSwitch = new TraceSwitch("Simplicit.Net.Lzo", "Switch for tracing of the LZOCompressor-Class");
private const string LzoDll32Bit = @"lib32\lzo_32.dll";
#region Dll-Imports
[DllImport(LzoDll32Bit, EntryPoint = "lzo_version_string")]
private static extern IntPtr lzo_version_string32();
[DllImport(LzoDll32Bit, EntryPoint = "lzo1x_1_compress", CallingConvention = CallingConvention.Cdecl)]
private static extern int lzo1x_1_compress32(byte[] src, int src_len, byte[] dst, ref int dst_len, byte[] wrkmem);
[DllImport(LzoDll32Bit, EntryPoint = "lzo1x_decompress", CallingConvention = CallingConvention.Cdecl)]
private static extern int lzo1x_decompress32(byte[] src, int src_len, byte[] dst, ref int dst_len, byte[] wrkmem);
[DllImport(LzoDll32Bit, EntryPoint = "__lzo_init_v2", CallingConvention = CallingConvention.Cdecl)]
private static extern int __lzo_init_v2_32(uint v, int s1, int s2, int s3, int s4, int s5, int s6, int s7, int s8, int s9);
#endregion
private byte[] _workMemory = new byte[16384L * 4];
public LZOCompressor()
{
int init = 0;
init = __lzo_init_v2_32(1, -1, -1, -1, -1, -1, -1, -1, -1, -1);
if (init != 0)
{
throw new Exception("Initialization of LZO-Compressor failed !");
}
}
public byte[] Compress(byte[] src)
{
if (_traceSwitch.TraceVerbose)
{
Trace.WriteLine(String.Format("LZOCompressor: trying to compress {0}", src.Length));
}
byte[] dst = new byte[src.Length + src.Length / 64 + 16 + 3 + 4];
int outlen = 0;
lzo1x_1_compress32(src, src.Length, dst, ref outlen, _workMemory);
if (_traceSwitch.TraceVerbose)
{
Trace.WriteLine(String.Format("LZOCompressor: compressed {0} to {1} bytes", src.Length, outlen));
}
byte[] ret = new byte[outlen + 4];
Array.Copy(dst, 0, ret, 0, outlen);
byte[] outlenarr = BitConverter.GetBytes(src.Length);
Array.Copy(outlenarr, 0, ret, outlen, 4);
return ret;
}
public byte[] Decompress(byte[] src)
{
if (_traceSwitch.TraceVerbose)
{
Trace.WriteLine(String.Format("LZOCompressor: trying to decompress {0}", src.Length));
}
int origlen = BitConverter.ToInt32(src, src.Length - 4);
byte[] dst = new byte[origlen];
int outlen = origlen;
lzo1x_decompress32(src, src.Length - 4, dst, ref outlen, _workMemory);
if (_traceSwitch.TraceVerbose)
{
Trace.WriteLine(String.Format("LZOCompressor: decompressed {0} to {1} bytes", src.Length, origlen));
}
return dst;
}
}
}
如果我缺少任何詳細信息,請告訴我,我們很樂意提供所需的任何信息。 謝謝。
我看到該項目中包含dll,請確保將“復制到輸出目錄”的dll屬性設置為“如果更新則復制”。 可能是他們沒有在“發布”文件夾中創建包含dll的文件夾。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.