Click here to Skip to main content
15,881,789 members
Articles / Programming Languages / Visual Basic
Article

Playing around with System.Reflection.Assembly.LoadFrom

Rate me:
Please Sign up or sign in to vote.
4.33/5 (15 votes)
25 Apr 20052 min read 118.7K   50   13
Some more or less cool functionality with late binding of assemblies.

Introduction

There are some very nice areas in the CLR, for example the whole area of reflection. Nowadays, developers use reflection, its functionalities for their projects. But it's nearly impossible to know all about these functions.

So I made some test just on the Assembly.LoadFrom and played around with it...

Basics

VB
Private Sub Test()
 Dim extAssembly As System.Reflection.Assembly = _
      System.Reflection.Assembly.LoadFrom("c:\WindowsApplication2.exe")

 Dim extForm as Form = _
      extAssembly.CreateInstance("WindowsApplication2.Form1", True)
 Me.AddOwnedForm(extForm)
 extForm.Show()

End Sub

For those who use system reflection, there is a common piece of code. We load an external assembly from the file system, create an instance of the form within that assembly, and add it to our Forms collection to display it later on.

Naming of the assemblies

Now, if we use references in our projects, we can only reference DLL files. But with System.Reflection, we can refer to any file name. We can give our files any name and it works. This makes hacker's life a bit more complicated.

VB
Dim extAssembly As System.Reflection.Assembly = _
    System.Reflection.Assembly.LoadFrom("c:\x.yz")

Use a web reference to the assembly

We are able to just use a URL instead of locating the assembly in our own network.

VB
Dim extAssembly As System.Reflection.Assembly = _
     System.Reflection.Assembly.LoadFrom("http://www.myweb.ch/" & _ 
     "myTest/WindowsApplication2.exe")

And it works - yes, it works very fast. Now think about using a password protected feature within your app before you download the assembly from the web. And think about the way it works - there is no local assembly. It is just loaded within your app domain, and after you close your application, nothing will be found on the users hard drive. This way, one is not able to de assemble your code or use it on his own with System.Reflection- as long as he does not know where it is....

Use a webpage

This is the most coolest part of it. Instead of directly referencing the assembly, we are calling a webpage on our Web Server (for test purpose just do with classic ASP) and suddenly, we see a wide range of potential in it.

VB.NET
Dim extAssembly As System.Reflection.Assembly = _
   System.Reflection.Assembly.LoadFrom _
   ("http://www.myweb.ch/myTest/CallMyExe.asp")

The content of our callmyexe.asp is that easy:

VBScript
<%@ Language=VBScript %>
<%response.redirect 
   "http://www.myweb.ch/mytest/windowsapplication2.exe">

But you'll see the following potential within it:

  • Generate a URL that also provides some security information. (i.e. any keys.)
  • Our website can check some security as well. (i.e. check the calling IP.)
  • We use some parameters in our URL so that the webpage decides what kind of assembly we receive.
  • We can store our assemblies within a database and give them back - and our directories do not contain any of the assemblies.
  • At least, with CodeDom, we are able to generate an assembly ad hoc and give it back to the caller.

So, have fun making your own tests.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) http://www.btv-data.ch
Switzerland Switzerland
On software development since 1992, going through the different steps from mainframe computing with VMS/Vax over VB4-6, Access and SQL server, Cold Fusions and ASP programming to my currently status as Chief Solution Developer for Smartclient and Client/Server development with vb.net and SQL Server. MCSD and some other stuff.


Comments and Discussions

 
Generalthanks Pin
Elias Bachaalany21-Jun-07 21:34
Elias Bachaalany21-Jun-07 21:34 
Generalvc++.net 2003- extract image using assembly Pin
minad_78611-Apr-07 3:51
minad_78611-Apr-07 3:51 
GeneralParameters Pin
marco gheri21-Nov-06 9:10
marco gheri21-Nov-06 9:10 
QuestionBut is ofusqued?? Pin
Javea6514-Jun-05 6:03
Javea6514-Jun-05 6:03 
AnswerRe: But is ofusqued?? Pin
anotherbla00114-Jun-05 18:56
anotherbla00114-Jun-05 18:56 
GeneralRe: But is ofusqued?? Pin
Javea6514-Jun-05 21:11
Javea6514-Jun-05 21:11 
GeneralAssembly.LoadFrom from GAC Pin
Member 33395316-Jun-05 12:17
Member 33395316-Jun-05 12:17 
GeneralRe: Assembly.LoadFrom from GAC Pin
Daniel Ch. Bloch (MCSD, MCAD, MCTS)14-Jun-05 19:03
Daniel Ch. Bloch (MCSD, MCAD, MCTS)14-Jun-05 19:03 
GeneralBeautiful work! Pin
Phan Dung4-May-05 16:49
Phan Dung4-May-05 16:49 
GeneralCorrection Pin
J. Dunlap25-Apr-05 21:27
J. Dunlap25-Apr-05 21:27 
GeneralRe: Correction Pin
Daniel Ch. Bloch (MCSD, MCAD, MCTS)25-Apr-05 22:39
Daniel Ch. Bloch (MCSD, MCAD, MCTS)25-Apr-05 22:39 
GeneralRe: Correction Pin
dshorter14-May-05 4:54
dshorter14-May-05 4:54 
GeneralRe: Correction Pin
Daniel Ch. Bloch (MCSD, MCAD, MCTS)5-May-05 20:27
Daniel Ch. Bloch (MCSD, MCAD, MCTS)5-May-05 20:27 
No, it does not. A form or assembly loaded this way has the security settings of the zone it comes from, like local intranet zone, internet zone and so on.

So you have to carefully think about, what you can do, how you need to set up security and thinks like this.

But if you are working with Certification on Client Side, you will have no problem at all.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.