Solved

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

Posted on 2004-09-20
8
683 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
ID: 12102339
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
ID: 12103223
Why would you want to add in dynamically?
0
 
LVL 41

Expert Comment

by:graye
ID: 12107515
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
ID: 12109174
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
ID: 12111165
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

896 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

13 Experts available now in Live!

Get 1:1 Help Now