0

I plan to merge two DLLs to give only one manually all using VB.NET. Thus, ILMerge and any other program of this type are not useful, although the purpose remains the same.


  • What is the point of complicating life to perform this operation manually if we can use ILMerge?

Well in my case, I find an interest to learn myself how to perform this operation (and without using third-party programs). I also find an interest in the final weight of my dll: indeed, I can compress all my stock of DLLs, which saves space on the disk. Etc.


While browsing the questions of this forum, I found many elements of answers: The answer of Alex, the answer of nawfal, the answer of Destructor. All of these answers have one thing in common: to load a dll, use Assembly.load from the Reflector library.

So I came to realize that in my code. Nevertheless, the goal is still not achieved:

At term, I would like to use this code, without having to lug around my dll.

 Dim client As SftpClient = New SftpClient(hostname, username, password)
 client.Connect()
 Using stream As Stream = New MemoryStream(IO.File.ReadAllBytes(txtFiles.Text))
          client.UploadFile(stream, "/www/Server.exe")
 End Using

But how to import the SftpClient method (belonging to the dll I want to import, named Renci.SshNet.dll)?

I tried this:

I added my dll as a resource and then added code:

 Dim mas = Assembly.Load(ByteOfDll))
 Dim client As mas.SftpClient = New mas.SftpClient(hostname, username, password)

But that obviously does not work(The error is: the type 'mas.SftpClient' is not defined). How to achieve this?

Corentin Mar
  • 125
  • 1
  • 7
  • I'm not quite sure what the question is here. On the face of it you are asking for a treatise on how to reimplement ILMerge? – David Heffernan Aug 05 '19 at 12:52
  • As the title suggests, Imports the methods of a DLL with Assembly.load (). Finally, I would like to manually do what ILmerge does – Corentin Mar Aug 05 '19 at 12:58
  • ILMerge is very complex. You can't expect an answer here giving all the details of how to reimplement it. You need to narrow the focus of your question to a single specific issue. You can start by reading the source code of ILMerge. – David Heffernan Aug 05 '19 at 13:23
  • I do not want to rewrite ILmerge. I mentioned an example in my question. This example describes my goal well. If you Looking at what I wrote, I also mentioned somes responses from other stackoverflow posts. These answers go in the direction of my goal, but I need more details on these. A simple "Assembly.load ()" does not allow to use my dll. – Corentin Mar Aug 05 '19 at 13:36
  • OK. I'm lost. I don't know what you are asking. I'm trying to help you elaborate, but you clearly don't want to. Let's see how much traction the question gets. Have you looked at the ILMerge source code? – David Heffernan Aug 05 '19 at 13:54
  • @DavidHeffernan So tell me what you do not understand, it will be easier for me! There is no argument in "You do not want to make an effort". I gave you an example of what I want to get (ie to merge my dll with my project in order to call SftpClient, being integrated in this dll.Finally, it will allow me not to lug the dll). Now, what is not clear in it? The nawfal response I linked to my post answers my question, but does not seem to go all the way. Indeed, I get an error when handling Assembly.load (): the latter does not allow me to load as an import my dll. I'll take a look yes :) – Corentin Mar Aug 05 '19 at 14:08
  • What to do after `Assembly.LoadFrom()` https://stackoverflow.com/a/465509/8367626 This is way over my head but very interesting. – Mary Aug 05 '19 at 17:22
  • @Mary has given you the answer, you need to get the type by name, and then use `CreateInstance` to create an instance. As you said, `New mas.SftpClient()` obviouosly does not work. – RobertBaron Aug 06 '19 at 20:09
  • I finally found a solution. I think she is not the cleanest, but she works. Thanks for your help @Mary, your track was good. Despite not having exploited it, it is possible to use an assembly as you say :) – Corentin Mar Aug 09 '19 at 15:55

1 Answers1

0

I finally managed to solve my problem! I found this post on stackoverflow that has unlocked everything:

How to use an DLL load from Embed Resource?

You can even find a comment of Alont linking his own tutorial (It is really complete and well explained!)

https://www.codeproject.com/Articles/528178/Load-DLL-From-Embedded-Resource

I just added this little code in my Sub Main() (Warning, you must add this code to the header of the statement Sub).

    Shared Sub main()

            AddHandler AppDomain.CurrentDomain.AssemblyResolve,
                     Function() As System.Reflection.Assembly

                         Return Assembly.Load(MyAssembly)
                     End Function

        TryCallMyEmbeddedRessource()
    End Sub
    Private Shared Function TryCallMyEmbeddedRessource()
        Dim client As Renci.SshNet.SftpClient = New Renci.SshNet.SftpClient(hostname, username, password)
        client.Connect()
        Using stream As Stream = New MemoryStream(IO.File.ReadAllBytes(***))
            client.UploadFile(stream, "****")
        End Using
    End Function

I do not know why, but if I declare Dim client As Renci.SshNet.SftpClient = New Renci.SshNet.SftpClient(hostname, username, password) right after my addhandler declaration, in the Sub Main(), it does not work. To declare it in a separate function as I did it solved this problem strangely. To think if you want to do the same thing.

Corentin Mar
  • 125
  • 1
  • 7