dcsimg
Results 1 to 4 of 4

Thread: [RESOLVED] Embed and load custom DLL

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2018
    Location
    Slovakia
    Posts
    82

    Resolved [RESOLVED] Embed and load custom DLL

    Iīve encountered problems when I have tried to load custom DLL at app startup which is embedded as a resource file. Here is my code:

    Code:
    Partial Friend Class MyApplication
            Private WithEvents Domain As AppDomain = AppDomain.CurrentDomain
    
            Private Function DomainCheck(ByVal SENDER As Object, ByVal E As System.ResolveEventArgs) As System.Reflection.Assembly Handles Domain.AssemblyResolve
                If E.Name.Contains("Ionic_Zip") Then
                    Return System.Reflection.Assembly.Load(My.Resources.Ionic_Zip)
                Else
                    Return Nothing
                End If
            End Function
        End Class
    Iīve got the following error when I click on a button in my app:
    Ionic.Zip.ZipException: Cannot read that as a ZipFile ---> Ionic.Zip.BadReadException: Bad signature (0x7C5D0BDB) at position 0x000A6453
    Thanks.
    IonicZIP_DLL.zip

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Oct 2018
    Location
    Slovakia
    Posts
    82

    Re: Embed and load custom DLL

    I tried:

    1.Rename DLL to Ionic.ZIP.dll, because I suppose that underline causing problems. It does not work even without any character - IonicZIP.dll
    2.All items in My.Resources Access Modifiers are currently set to Public, since I thought that credentials causing problems.
    3.Set my app to force admin credentials even when I am admin.

    Itīs of course possible to distribute neccessary DLL alongside with my app and load it a startup, but I donīt want that.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2018
    Location
    Slovakia
    Posts
    82

    Re: Embed and load custom DLL

    Hereīs the error:

    Name:  ioniczip_cannotread_error.png
Views: 62
Size:  18.6 KB

    I tried this code (Button1_Click) - export DLL from references as a file and load it:

    Code:
    Imports System.IO
    Imports System.Reflection
    
    Private m_Assembly As Assembly
    
     System.IO.File.WriteAllBytes("C:\test1\IonicZIP.dll", My.Resources.Ionic_Zip_CF)
            m_Assembly = Assembly.LoadFrom("C:\test1\IonicZip.dll")
            Using zip As Ionic.Zip.ZipFile = Ionic.Zip.ZipFile.Read("C:\test1\file.zip")
                zip.ExtractAll(target, Ionic.Zip.ExtractExistingFileAction.OverwriteSilently)
            End Using
    I also tried to check the namespace in the "imported namespaces" checkedlistbox in VB, but it does not work.

    Further, I tried this code in app events (I replaced MyApplication with custom assembly name both in code and also in app properties), but it does not work.

    Code:
    Partial Friend Class TEST_APP
            Private WithEvents Domain As AppDomain = AppDomain.CurrentDomain
            Private Function DomainCheck(ByVal sender As Object, ByVal E As System.ResolveEventArgs) As System.Reflection.Assembly Handles Domain.AssemblyResolve
                If E.Name.Contains("Ionic_ZIP_CF") Then
                    Return System.Reflection.Assembly.Load(My.Resources.Ionic_Zip_CF)
                Else
                    Return Nothing
                End If
            End Function
        End Class
    Next, I tried to run this code at Form_Load instead of Button1 event, because I supposed that there is a problem with other lines in my code:

    Code:
    Private m_Assembly As Assembly
    System.IO.File.WriteAllBytes("C:\test1\IonicZIP.dll", My.Resources.Ionic_Zip_CF)
    m_Assembly = Assembly.LoadFrom("C:\test1\IonicZip.dll")
    DLL library is created in the desired folder, but it cannot be used - error "Cannot read that as a ZIP file." It seems to be an issue with loading DLLs, but I donīt know how it can be resolved.

    Can you help me? Thanks a lot.
    Last edited by VB.NET Developer; Aug 26th, 2019 at 11:00 AM.

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Oct 2018
    Location
    Slovakia
    Posts
    82

    Re: Embed and load custom DLL

    I tried to isolate possible error at DLL loading process via msgbox ("Error!") and it worked - no error was throwned. So DLL library was succesfully loaded at runtime.

    Problem was - ZIP archive wasnīt really ZIP archive - its properties telling me that it has SFX module embedded - I donīt know why. I only applies compression setting, nothing more. But now it works perfectly!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width