Solved

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

Posted on 2004-09-20
8
696 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 2008 - Windows Protected Your PC 7 41
Help with opening combobox using VB.NET. 3 26
Passing data between Forms 3 20
MYSQL responding very slow 3 26
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

830 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