Solved

add the reference of  dll dynamically  and use the dll in VB.NET

Posted on 2004-09-20
8
679 Views
Last Modified: 2011-04-14
Hello..
I am making one application in VB.NET in which I want to  dynamically add tne reference of
one dll in my application.
I have the dll which calls method "createhtm" along with these parameters ..

(path, nFileName, Outputpath)

How will I add the reference and use the dll dynamically.


Pls do let me  know
0
Comment
Question by:rajender123
8 Comments
 
LVL 3

Expert Comment

by:imu79
Comment Utility
As far as I know, you cannot add a reference dynamically.
However, there are some ways you can call a method dynamically. It depends on what type of dll you have. Is it a .NET dll or a COM dll?

0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Why would you want to add in dynamically?
0
 
LVL 41

Expert Comment

by:graye
Comment Utility
Sure you can dynamically bind to a DLL at runtime...   It's not recommended since it violates the entire .Net security structure.   That also means, you won't find any examples on how to do it on the Microsoft web site.

Here is a chunk of a working program that demonstrates how to dynamically load a DLL "plug-in" module at runtime.

        Dim Assmbly As Reflection.Assembly
        Dim PlugInType As Type
        Dim ProbePlugIn As PlugInBase.PlugInBase
        Dim PlugInName, temp As String

        ' load the plug-in DLL
        temp = Split(cbFeature.SelectedItem.ToString, "(")(1)
        PlugInName = Split(temp, ")")(0)
        Try
            ' If running under the IDE, then always get the development version of the
            ' plugin DLL from it's development directory (which is a subdirectory under
            ' this developement directory).
            If System.Diagnostics.Debugger.IsAttached Then
                Assmbly = Reflection.Assembly.LoadFrom(Application.StartupPath & "\..\" & PlugInName & "\bin\" & PlugInName & ".dll")
            Else
                Assmbly = Reflection.Assembly.LoadFrom(Application.StartupPath & "\" & PlugInName & ".dll")
            End If
            PlugInType = Assmbly.GetType(Assmbly.GetName.Name & "." & PlugInName, True, True)
            ProbePlugIn = CType(Activator.CreateInstance(PlugInType), PlugInBase.PlugInBase)
        Catch ex As Exception
            MsgBox("Yikes!, can't find the " & PlugInName & ".dll plug-in" & vbCr & ex.message, MsgBoxStyle.Exclamation, "Internal Program Error")
            Exit Sub
        End Try
0
 

Author Comment

by:rajender123
Comment Utility
Hi graaye..
well i tried your code..but it was not working..

after this line it went to exception line(catch error)
   PlugInType = Assmbly.GetType(Assmbly.GetName.Name & "." & PlugInName, True, True)

can u let me knwo how to use the methods  of the dll...............
well second thing how to add the refrence for PlugInBase.PlugInBase
Psl help me out..
its realyy urgent
0
 
LVL 41

Accepted Solution

by:
graye earned 50 total points
Comment Utility
The chunk of code was only meant to serve as an example... not a complete solution.  Let me explain a bit more about this particular example...  perhaps that will help

This program accepts any number of "plug in" DLL modules to add functionality to the "main" program.  The main program gets its list of available DLL files from the App.config file... and I place the list into a ComboBox.  The user selects one of the DLL plugins, and I load that DLL file.

In order to load a .Net Class DLL file into another program, the recieving program must know something about the "foriegn" DLL.  (Unlike an Active-X DLL of just a few years ago, there aren't any  built-in mechanisms for autodiscovery... the ability to discover the list of methods and properties on the fly).  For a .Net Class DLL... you have have a .Net Reference in the calling program.

Ah... I can see your mind working....  Yes, that does cause a problem...  after the "main" program is compiled and distributed, how on Earth could I later go back and add a Reference to a newly developed DLL?

The answer is... you don't.   Instead of adding a reference to the Class DLL itself, you instead add a reference to a "generic" base Class (from which the real plugin classes are derived).   Now the main program is expecting a base class module... but instead it loads any number of derived class modules.  It doesn't know any different... it thinks it's loading a base class.

So, to answer your questions... if the design of your project is similar to mine (I have no idea)... then you'd develope a base class DLL in a separate VB.Net project.  You'd then add a "Project Reference" to this base class to your main program.   Then you'd create a 2nd "real" class project that derives from the base class.

So, am I at least close?   I thought my example was a bit odd-ball.  What exactly are you trying to do?
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video discusses moving either the default database or any database to a new volume.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now