This project is read-only.

Can not load 7-zip library or internal COM error! Message: failed to load library.

Apr 23, 2009 at 4:08 AM
I have a problem while using SevenZipSharp. When I use it in a console application, it works fine. But when it is used in a website, the code will raise a SevenZipLibraryException.

Detail
SevenZip.SevenZipLibraryException was caught
Message="Can not load 7-zip library or internal COM error! Message: failed to load library."
Source="SevenZipSharp"
StackTrace:
at SevenZip.SevenZipLibraryManager.LoadLibrary(Object user, Enum format)
at SevenZip.SevenZipExtractor.Init(Stream stream, InArchiveFormat format)
at SevenZip.SevenZipExtractor..ctor(Stream archiveStream, InArchiveFormat format)

Thanks.
Apr 23, 2009 at 12:31 PM
Set the library path before any work with Compressor/Extractor, for example SeveZipExtractor.SetLibraryPath(@"path to 7z.dll");
Apr 23, 2009 at 1:33 PM

I do set the library path before using SevenZipCompress or SevenZipExtractor, but the exception is still there.

    protected void Button1_Click(object sender, EventArgs e)

    {

        SevenZipCompressor.SetLibraryPath(@"F:\MyTestCode\TestWebSite\Bin\7z.dll");

        SevenZipCompressor compressor = new SevenZipCompressor();

        using (MemoryStream ms1 = new MemoryStream(Encoding.ASCII.GetBytes("abcdefgh")), ms2 = new MemoryStream())

        {

            compressor.CompressStream(ms1, ms2);// Get SevenZip.SevenZipLibraryException here: Can not load 7-zip library or internal COM error! Message: failed to load library.

            Response.Write(Encoding.ASCII.GetString(ms2.ToArray()));

        }

    }

Apr 23, 2009 at 9:39 PM
Try
MessageBox.Show(File.Exists(@"F:\MyTestCode\TestWebSite\Bin\7z.dll") ? "Ok, this is a bug in SevenZipSharp" : "It seems i don't understand something");

before the SevenZipCompressor.SetLibraryPath(@"F:\MyTestCode\TestWebSite\Bin\7z.dll"); line.
Apr 24, 2009 at 2:27 AM

Only in a website project it doesn’t work correctly, when it is used in a console application or windows forms application it works fine.

I’ve downloaded the source code and stepped into the code.

                if (!File.Exists(LibraryFileName))// pass, no Exception thrown

                {

                    throw new SevenZipLibraryException("DLL file does not exist.");

                }

                if ((_ModulePtr = NativeMethods.LoadLibrary(LibraryFileName))

                        == IntPtr.Zero)// this statement is true and SevenZipLibraryException is thrown

                {

                    throw new SevenZipLibraryException("failed to load library.");

                }

I wonder if there is some points I’ve missed.

Thanks.

Apr 24, 2009 at 10:38 AM
Edited Apr 24, 2009 at 10:42 AM
As for ASP.NET, consider this article:
http://blogs.msdn.com/eldar/archive/2006/09/22/how-to-call-c-dll-from-asp-net-c-code.aspx
This solves your issue.

User gregshutdown uses SevenZipSharp in his ASP.NET site. He asked me to include this code:
LibraryManager.cs, line 44:
private static string LibraryFileName = ConfigurationManager.AppSettings["7zLocation"]?? Path.Combine(Path.GetDirectoryName(
                Assembly.GetExecutingAssembly().Location),  "7z.dll");

I guess he used it somehow.
Jun 4, 2009 at 12:50 PM

ginnivanj, for x64 apps, you should use the x64 version of 7z.dll (native 7-zip library) instead of 32 bit.

Jun 8, 2009 at 8:12 AM

Umm... I'm not sure if I stated this before, but I was pretty sure I did O_o

Anyway, for a website to be able to use the 7zip library from a 64 bit .NET app, you have to use the 64 bit 7z.dll. Just download the full 64 bit 7zip app, and take the dll from there :)

Alternatively, compile your app with only x86 support (not "All Platforms") in which case you would be able to use the 32 bit one..

 

P.S. Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),  "7z.dll") in a website will return a path somewhere deep inside the windows dir, not your website's bin directory. That's why I asked markhor to add the AppSettings part, so that I don't have to specify the dll path EVERY single time I create the extractor or the compressor :P

 

Best regards,

/me

Dec 30, 2010 at 11:15 PM
Edited Dec 31, 2010 at 12:15 AM

hello
Could you tell me what you have done for not having the following bug: Can not load 7-zip library or internal COM error! Message: failed to load library.
for now I blocked the above
Here is an part of my code in vb.net 2010 :
Imports SevenZip

sub extract(destindl as string)
If File.Exists(Application.StartupPath & "\7z.dll") = True Then
                SevenZipExtractor.SetLibraryPath(Application.StartupPath & "\7z.dll")
            Else
                Principal.voirinfo("Not Found 7z.dll")

                exit sub
            End If
Dim ext As SevenZipExtractor = New SevenZipExtractor(destindl) 'here error

end sub
excuse me for my bad english and thanks you for your help

 

my answer is my project for x86 and my version I used was of 7z.dll x64

 

Mar 19, 2012 at 3:10 PM

I think I found the problem after a couple of hours of trying.

If your application/website is running in x64 and you try to SetLibraryPath to the x86, it will fail.

Solution?

1. Make sure you have both 7z.dll and 7z64.dll in your bin folder. I did that by adding the file to the project, using add existing file. Then, on properties, "Copy to outpu directory: always".

2. Now, you need to figure out which version to include:

 

if (IntPtr.Size == 8) //x64
    SevenZip.SevenZipExtractor.SetLibraryPath(Path.Combine(HttpContext.Current.Server.MapPath("~"), "bin", "7z64.dll"));
else //x86
    SevenZip.SevenZipExtractor.SetLibraryPath(Path.Combine(HttpContext.Current.Server.MapPath("~"), "bin", "7z.dll"));

 

 

Jun 27, 2012 at 9:29 PM

I solve my problem granting access to "network service" user to the 7z.dll file. 

 

sorry for the english. 

Jul 10, 2012 at 6:19 PM
tshh wrote:
I have a problem while using SevenZipSharp. When I use it in a console application, it works fine. But when it is used in a website, the code will raise a SevenZipLibraryException.

Detail
SevenZip.SevenZipLibraryException was caught
Message="Can not load 7-zip library or internal COM error! Message: failed to load library."
Source="SevenZipSharp"
StackTrace:
at SevenZip.SevenZipLibraryManager.LoadLibrary(Object user, Enum format)
at SevenZip.SevenZipExtractor.Init(Stream stream, InArchiveFormat format)
at SevenZip.SevenZipExtractor..ctor(Stream archiveStream, InArchiveFormat format)

Thanks.


See my blog post for the solution: http://blog.jongallant.com/2011/10/7-zip-dll-file-does-not-exist.html

Jul 16, 2012 at 4:28 PM

I don't have any issue in local but in live web server I am also facing same error : Can not load 7-zip library or internal COM error! Message: failed to load library. 

we are using the 32 bit server. and here is code

SevenZip.SevenZipExtractor.SetLibraryPath(Path.Combine(HttpContext.Current.Server.MapPath("~"), "bin", "7z.dll"));

please help me to resolve this issue.

Thanks 

Mahesh

Jul 16, 2012 at 4:30 PM

How did you do that could you please explain me with step by steps.

Thanks,

Mahesh

Jul 24, 2012 at 8:30 PM

Explain... ¿who?, ¿what of all the things?

Feb 26, 2013 at 5:26 PM
I am getting the dreaded:

Can not load 7-zip library or internal COM error! Message: failed to load library.

This error is starting to drive me crazy. I have been all over the internet and have read every post on this error. I have tried implementing every 'fix' suggested...at least twice. NOTHING gets rid of this error.

I have added SevenZipSharp as a reference and have placed 7z.dll and 7z64.dll in my projects bin directory (also tried loading from Program Files). My code has no problem finding the library(s) at the paths I've provided. The error occurs as soon as I issue a call to CompressStream().

I also made sure my project platform is set to ALL CPUs.

Nothing I do gets rid of this problem. Does ANYONE have any suggestions other than what I've been reading on these sites.

Here is my test code:
            try
            {
                if (IntPtr.Size == 8) //x64
                {
                    SevenZip.SevenZipCompressor.SetLibraryPath(Path.Combine(HttpContext.Current.Server.MapPath("~"), "bin", "7z64.dll"));
                }
                else //x86
                {
                    SevenZip.SevenZipCompressor.SetLibraryPath(Path.Combine(HttpContext.Current.Server.MapPath("~"), "bin", "7z.dll"));
                }

                SevenZipCompressor comp = new SevenZipCompressor();

                comp.ArchiveFormat     = OutArchiveFormat.Tar;
                comp.CompressionLevel  = CompressionLevel.Normal;
                comp.CompressionMode   = CompressionMode.Create;
                comp.CompressionMethod = CompressionMethod.Copy;
                comp.DefaultItemName   = fileName;

                inStream.Position = 0;  // created external to this routine

                MemoryStream outStream = new MemoryStream();

                //bool canRead = memStreamCompany.CanRead;    // verified read/write capabilities on the streams
                //bool canWrite = outStream.CanWrite;

                comp.CompressStream(inStream, outStream);    <--- error occurs here

                outStream.Flush();
                outStream.Close();
              }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
Help anyone?!?
Feb 26, 2013 at 6:46 PM
you gave the user permission to the folder where the library?
with what user do you test the Web application?
Feb 27, 2013 at 12:24 PM
Yes. In the properties/security tab for SevenZipSharp.dll, 7z.dll, and 7z64.dll I have made sure all users have full control rights to these files. I also added a Network Service entry to the security group list and made sure it had full control rights as well. I just verified that all users of these three files have full control rights and tried running my app again. Still can't load the library.

More ideas?
Feb 27, 2013 at 2:16 PM
the folder where you create the compressed file?, has permissions?
Feb 27, 2013 at 2:49 PM
As you can see from the code I posted, I am not trying to compress to disk. I am trying to compress an input stream to an output stream. Both streams are MemoryStreams. But, I've tested both streams for read/write capability and they are fine. The problem is that the SevenZip library cannot be loaded for some reason. That is the crux of my problem I believe. The only error I receive is the old "Can not load 7-zip library or internal COM error! Message: failed to load library."

Since the path to the files themselves seems clear to the SetLibraryPath method, I'm thinking that the problem may have more to do with the second half of the error message..."or internal COM error". There is not enough detail in the error for me to guess at what the COM error issue might be.
Feb 27, 2013 at 3:55 PM
Looking in the debugger at the Compressor object I create (comp in the code above) I notice that the compressor does associate itself with a temp directory on disk. So I checked the permissions on the temp file disk location and found that all entries in the security list had full access to this directory. The files in the directory all appear to be set to read-only, which I tried to clear as a test but the read-only attribute re-asserted itself.

I am still at a loss for the cause of the failure to load the library. I will keep poking around in the debugger and examining the SevenZipSharp objects but I'm hoping someone can save me the time and come up with an answer I haven't seen yet.

Thanks to all who might be noodling on this for me,
Feb 27, 2013 at 4:36 PM
test in stackoverflow.com they respond faster. I have not programmed anything for about 1 year and I do not remember much.
sorry.
Feb 27, 2013 at 4:39 PM

Thanks for the pointer to the other site. I will check it out.

From: nnopazo [email removed]
Sent: Wednesday, February 27, 2013 11:36 AM
To: Robert Alan
Subject: Re: Can not load 7-zip library or internal COM error! Message: failed to load library. [sevenzipsharp:54207]

From: nnopazo

test in stackoverflow.com they respond faster. I have not programmed anything for about 1 year and I do not remember much.
sorry.




Disclaimer: This e-mail and any attachments may contain confidential information belonging to the sender which is legally privileged. This information is intended only for the use of the individual or entity named above. If you are not the intended recipient, you are hereby notified that any disclosure, copying, distribution or the taking of any action in reliance on the contents of this information is strictly prohibited. If you have received this e-mail in error, please immediately notify us by telephone at (617) 600-3240, via our website at www.subpay.com, or by replying to this message.
Feb 27, 2013 at 7:51 PM
One poster above claimed that the problem only exists for web applications. So I have built a WinForms app that does nothing but try to compress an input stream into an output stream. I have done everything suggested in these posts and yet......I am still getting the can't load library error. Here is the code.

Please note that I have added the SevenZipSharp dll as a reference in my project. Intellisense works perfectly so I know my ref is working. Also, the 7z.dll can be found exactly where I'm telling the SetLibraryPath to look for it AND it has full control privileges.

So....what the heck am I missing? Why do other people seem to think this so easy and yet I can't get it to load the library under apparently any circumstance?
        try
        {
            MemoryStream inStream = new MemoryStream();
            MemoryStream outStream = new MemoryStream();
            StreamWriter sw = new StreamWriter(inStream);
            sw.WriteLine(txtInputStream.Text);
            sw.Flush();

            SevenZipBase.SetLibraryPath(@"C:\Program Files (x86)\7-Zip\7z.dll");

            SevenZipCompressor comp = new SevenZipCompressor();

            //comp.ArchiveFormat = OutArchiveFormat.Tar;
            //comp.CompressionLevel = CompressionLevel.Normal;
            //comp.CompressionMode = CompressionMode.Create;
            //comp.CompressionMethod = CompressionMethod.Copy;

            comp.CompressionMethod = SevenZip.CompressionMethod.Lzma2;
            comp.CompressionLevel = SevenZip.CompressionLevel.Normal;
         //   comp.DefaultItemName = "pr-123456";

            inStream.Position = 0;

            //bool canRead = memStreamCompany.CanRead;
            //bool canWrite = outStream.CanWrite;

            comp.CompressStream(inStream, outStream);

            outStream.Flush();
            outStream.Close();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
Feb 27, 2013 at 9:20 PM
Try setting the following ... You may specify the custom path to 7-zip dll at SevenZipLibraryManager.LibraryFileName or call SevenZipExtractor.SetLibraryPath(@"c:\Program Files\7-Zip\7z.dll"); or call SevenZipCompressor.SetLibraryPath(@"c:\Program Files\7-Zip\7z.dll"); > ralan88 > Thursday, 28 February 2013 6:52 AM > > From: ralan88 > > One poster above claimed that the problem only exists for web > applications. So I have built a WinForms app that does nothing but try > to compress an input stream into an output stream. I have done > everything suggested in these posts and yet......I am still getting > the can't load library error. Here is the code. > > Please note that I have added the SevenZipSharp dll as a reference in > my project. Intellisense works perfectly so I know my ref is working. > Also, the 7z.dll can be found exactly where I'm telling the > SetLibraryPath to look for it AND it has full control privileges. > > So....what the heck am I missing? Why do other people seem to think > this so easy and yet I can't get it to load the library under > apparently any circumstance? > |try > { > MemoryStream inStream = new MemoryStream(); > MemoryStream outStream = new MemoryStream(); > StreamWriter sw = new StreamWriter(inStream); > sw.WriteLine(txtInputStream.Text); > sw.Flush(); > > SevenZipBase.SetLibraryPath(@"C:\Program Files (x86)\7-Zip\7z.dll"); > > SevenZipCompressor comp = new SevenZipCompressor(); > > //comp.ArchiveFormat = OutArchiveFormat.Tar; > //comp.CompressionLevel = CompressionLevel.Normal; > //comp.CompressionMode = CompressionMode.Create; > //comp.CompressionMethod = CompressionMethod.Copy; > > comp.CompressionMethod = SevenZip.CompressionMethod.Lzma2; > comp.CompressionLevel = SevenZip.CompressionLevel.Normal; > // comp.DefaultItemName = "pr-123456"; > > inStream.Position = 0; > > //bool canRead = memStreamCompany.CanRead; > //bool canWrite = outStream.CanWrite; > > comp.CompressStream(inStream, outStream); > > outStream.Flush(); > outStream.Close(); > } > catch (Exception ex) > { > Console.WriteLine(ex.Message); > }| > > Read the full discussion online > . > > To add a post to this discussion, reply to this email > (sevenzipsharp@discussions.codeplex.com > ) > > To start a new discussion for this project, email > sevenzipsharp@discussions.codeplex.com > > > You are receiving this email because you subscribed to this discussion > on CodePlex. You can unsubscribe or change your settings > > on codePlex.com. > > Please note: Images and attachments will be removed from emails. Any > posts to this discussion will also be available online at codeplex.com >
Try setting the following ...

You may specify the custom path to 7-zip dll at SevenZipLibraryManager.LibraryFileName
or call SevenZipExtractor.SetLibraryPath(@"c:\Program Files\7-Zip\7z.dll");
or call SevenZipCompressor.SetLibraryPath(@"c:\Program Files\7-Zip\7z.dll");




Thursday, 28 February 2013 6:52 AM

From: ralan88

One poster above claimed that the problem only exists for web applications. So I have built a WinForms app that does nothing but try to compress an input stream into an output stream. I have done everything suggested in these posts and yet......I am still getting the can't load library error. Here is the code.

Please note that I have added the SevenZipSharp dll as a reference in my project. Intellisense works perfectly so I know my ref is working. Also, the 7z.dll can be found exactly where I'm telling the SetLibraryPath to look for it AND it has full control privileges.

So....what the heck am I missing? Why do other people seem to think this so easy and yet I can't get it to load the library under apparently any circumstance?
try
{
MemoryStream inStream = new MemoryStream();
MemoryStream outStream = new MemoryStream();
StreamWriter sw = new StreamWriter(inStream);
sw.WriteLine(txtInputStream.Text);
sw.Flush();

SevenZipBase.SetLibraryPath(@"C:\Program Files (x86)\7-Zip\7z.dll");

SevenZipCompressor comp = new SevenZipCompressor();

//comp.ArchiveFormat = OutArchiveFormat.Tar;
//comp.CompressionLevel = CompressionLevel.Normal;
//comp.CompressionMode = CompressionMode.Create;
//comp.CompressionMethod = CompressionMethod.Copy;

comp.CompressionMethod = SevenZip.CompressionMethod.Lzma2;
comp.CompressionLevel = SevenZip.CompressionLevel.Normal;
// comp.DefaultItemName = "pr-123456";

inStream.Position = 0;

//bool canRead = memStreamCompany.CanRead;
//bool canWrite = outStream.CanWrite;

comp.CompressStream(inStream, outStream);

outStream.Flush();
outStream.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Feb 28, 2013 at 12:59 PM
Does anyone know what the WINCE and MONO flags are about in the SevenZipSharp code?
Feb 28, 2013 at 3:08 PM
Ok, after tracing the SevenZipSharp code within the context of a very simple WinForms app, it became apparent the kernel32 LoadLibrary method was where the failure was occurring. On a whim I re-downloaded the 7z.dll file and guess what...the problem disappeared. So it appears my problem the entire time was a corrupted dll. Glad I found it but I sure wish it didn't take me a day and a half to get to the bottom of it.

So anyone else running in to this. If you've done everything suggested in these posts and it doesn't work...try getting a fresh copy of the dll.
Mar 22, 2013 at 6:35 AM
I encountered exactly the same issue today.

Test code was running great from the Console application, but I got "the load library" error when running the code from my Outlook plugin.
The plugin is a x86 app written in C++ (unmanaged) that uses a mixture of unmanaged and managed code components.

Procmon utility showed that the LoadImage call on 7z.dll was successful. But still I was getting the LoadLibrary error.

I was able to get it to work by re-targeting SevenZip .Net Library from any CPU to x86.

I guess for x64 apps the SevenZip library should be compiled as x64 assembly


Regards,

Michael
Feb 4, 2015 at 2:04 PM
Can I suggest the following fix in SevenZipLibraryManager for automatically switching between 32/64 bit versions of the DLL?
private static string _libraryFileName = ConfigurationManager.AppSettings["7zLocation"] ??
    Path.Combine(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), 
        IntPtr.Size == 8 ? "7z64.dll" : "7z.dll");
This will then automatically pick the correct DLL by default without having to resort to using AppSettings or having to determine the correct DLL from user code outside the library. I have this fix in use on an application that is currently deployed on both an old legacy Windows XP 32 bit machine that we can't decommission, 64 bit Windows 7, and on the server side of the application running on Windows 2012 (64bit) server. Same version is deployed on all computers, with just a copy of 7z.dll and 7z64.dll in the same folder as the application.

Regards

Colin