简体   繁体   English

C#:一个特定的DLL的DLLImport无法正常工作,尽管一个示例应用程序显示它正常工作,我该如何解决呢?

[英]c# : DLLImport for a particular dll not working, although an example app shows it working, How can i resolve this?

I am trying to import a dll using relative paths 我正在尝试使用相对路径导入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

这是项目结构

{"Unable to load DLL 'lib32\\lzo_32.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"} {“无法加载DLL'lib32 \\ lzo_32.dll':找不到指定的模块。(HRESULT的异常:0x8007007E)“}

在此处输入图片说明

Now here is the weird thing, http://wallaceturner.com/lzo-for-c , the link for the site where i downloaded a sample project to see how to work with the lzo compressor in c#.net. 现在这是奇怪的东西, http://wallaceturner.com/lzo-for-c ,我下载示例项目的站点的链接,以了解如何在c#.net中使用lzo压缩器。 And when i run the project it successfully works. 当我运行该项目时,它会成功运行。 I see no other dependencies apart from what i have in my project. 除了项目中的内容,我没有看到其他依赖项。 I am using .Net4.5.2 in both of them. 我在两个都使用.Net4.5.2。

Here is my full source code for that class 这是我该课程的完整源代码

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

Please let me know if i am missing any details, would gladly provide anything required. 如果我缺少任何详细信息,请告诉我,我们很乐意提供所需的任何信息。 Thanks. 谢谢。

I see the dll's are include in the project, make sure you set the dll's Properties for 'Copy To Output Directory' to 'Copy if newer'. 我看到该项目中包含dll,请确保将“复制到输出目录”的dll属性设置为“如果更新则复制”。 Possibly they are not creating the folders with the dll's in them in your 'Release' folder. 可能是他们没有在“发布”文件夹中创建包含dll的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM