Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Populating an array of locale_time_info 8 46
Need a starter for ETL protocol? 4 55
VB.Net - TypeInitializer Error 25 26
location of a form 2 10
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 …
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

776 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