Hi,
I need to know how to load dll( or assembly) to use it like another .net class. With vb6, we used createobject("MyDll.MyObj") to load an object from a dll...
How to do that in vb.net in a way that I can set in a ini file the name of the object that i want to load ?
Thanks
Ray Ban
Comments
As far as I know, CreateObject in VB.NET can be used only for COM dlls.
If u have created a dll in VB.NET and want to use it, then u need to refer it and create an instance of it.
like, eg., ur dll name is MYDLL, then
dim obj as new Mydll.
...
regards,
karthik.
:
: I need to know how to load dll( or assembly) to use it like another .net class. With vb6, we used createobject("MyDll.MyObj") to load an object from a dll...
:
: How to do that in vb.net in a way that I can set in a ini file the name of the object that i want to load ?
:
: Thanks
:
: Ray Ban
:
If it's a .Net assembly, try this: Put it in the bin directory of the application you're working on and add a refrenece to it in your project. Then in the class or module that needs it just put Imports statement at the top that refers to its namespace. I think that ought to work.
If it's a COM dll then create a .Net type library for it using the type library importing tool at the command prompt[code]tblimp MyComDll.dll /out:MyDotNetDll.dll[/code]
Then right click the References in Solution Explorer and add it just like a .Net dll.
But I need something else. I need to be able to load any Assembly (dll) without using any direct reference. My applications, should be able to load assembly like Plug-Ins, in order to develop only plug-in when my application is ok.
I looked at a documentation about "Activator.createinstancefrom", but I have some trouble with it. It seems that I have some trouble with interfaces and their implementation.
Any ideas ?
Ray
: : Hi,
: :
: : I need to know how to load dll( or assembly) to use it like another .net class. With vb6, we used createobject("MyDll.MyObj") to load an object from a dll...
: :
: : How to do that in vb.net in a way that I can set in a ini file the name of the object that i want to load ?
: :
: : Thanks
: :
: : Ray Ban
: :
: If it's a .Net assembly, try this: Put it in the bin directory of the application you're working on and add a refrenece to it in your project. Then in the class or module that needs it just put Imports statement at the top that refers to its namespace. I think that ought to work.
:
: If it's a COM dll then create a .Net type library for it using the type library importing tool at the command prompt[code]tblimp MyComDll.dll /out:MyDotNetDll.dll[/code]
: Then right click the References in Solution Explorer and add it just like a .Net dll.
:
I think there is also a DllImport attribute that you can put in your code before a function that describes a dll, an entry point and some other stuff that is for unmanaged dll code. You put it in . (I think this is a way to get to the API.)
Post what works out because I'm trying to learn this stuff myself.
Good luck!
James
: Thanks for your reply...
:
: But I need something else. I need to be able to load any Assembly (dll) without using any direct reference. My applications, should be able to load assembly like Plug-Ins, in order to develop only plug-in when my application is ok.
:
: I looked at a documentation about "Activator.createinstancefrom", but I have some trouble with it. It seems that I have some trouble with interfaces and their implementation.
:
: Any ideas ?
:
: Ray
:
: : : Hi,
: : :
: : : I need to know how to load dll( or assembly) to use it like another .net class. With vb6, we used createobject("MyDll.MyObj") to load an object from a dll...
: : :
: : : How to do that in vb.net in a way that I can set in a ini file the name of the object that i want to load ?
: : :
: : : Thanks
: : :
: : : Ray Ban
: : :
: : If it's a .Net assembly, try this: Put it in the bin directory of the application you're working on and add a refrenece to it in your project. Then in the class or module that needs it just put Imports statement at the top that refers to its namespace. I think that ought to work.
: :
: : If it's a COM dll then create a .Net type library for it using the type library importing tool at the command prompt[code]tblimp MyComDll.dll /out:MyDotNetDll.dll[/code]
: : Then right click the References in Solution Explorer and add it just like a .Net dll.
: :
:
:
:
: I need to know how to load dll( or assembly) to use it like another .net class. With vb6, we used createobject("MyDll.MyObj") to load an object from a dll...
:
: How to do that in vb.net in a way that I can set in a ini file the name of the object that i want to load ?
:
: Thanks
:
: Ray Ban
:
First I created overloaded user-defined functions below ...
[code]
Public Function CreateObject(ByVal assemblyName As String, ByVal objID As String) As Object
Return CreateObject(assemblyName, objID, Nothing)
End Function
Public Function CreateObject(ByVal assemblyName As String, ByVal objID As String, ByRef args() As Object) As Object
Dim asm As System.Reflection.Assembly
asm = System.Reflection.Assembly.Load(assemblyName)
If Not args Is Nothing Then
Return asm.CreateInstance(objID, True, _
System.Reflection.BindingFlags.CreateInstance, _
Nothing, args, Nothing, Nothing)
Else
Return asm.CreateInstance(objID, True)
End If
End Function
[/code]
My hypothetical set-up:
.NET DLL: MyDll.dll
Namespace: MyNamespace
Creating an object, called MyObject, that has only a default constructor:
[code]
Dim obj As Object = CreateObject("MyDll", "MyNamespace.MyObject")
[/code]
Creating an object, called AnotherObject, that has a constructor that takes a string argument for instance:
[code]
Dim arg() As Object = {"SomeArgValue"}
Dim obj As Object = CreateObject("MyDll", "MyNamespace.MyObject", arg)
[/code]
your sample work fine, but how to ctype the obj instance into the real type of the object ?
Thanks,,
David
: : Hi,
: :
: : I need to know how to load dll( or assembly) to use it like another .net class. With vb6, we used createobject("MyDll.MyObj") to load an object from a dll...
: :
: : How to do that in vb.net in a way that I can set in a ini file the name of the object that i want to load ?
: :
: : Thanks
: :
: : Ray Ban
: :
:
: First I created overloaded user-defined functions below ...
:
: [code]
: Public Function CreateObject(ByVal assemblyName As String, ByVal objID As String) As Object
: Return CreateObject(assemblyName, objID, Nothing)
: End Function
:
: Public Function CreateObject(ByVal assemblyName As String, ByVal objID As String, ByRef args() As Object) As Object
: Dim asm As System.Reflection.Assembly
: asm = System.Reflection.Assembly.Load(assemblyName)
:
: If Not args Is Nothing Then
: Return asm.CreateInstance(objID, True, _
: System.Reflection.BindingFlags.CreateInstance, _
: Nothing, args, Nothing, Nothing)
: Else
: Return asm.CreateInstance(objID, True)
: End If
: End Function
: [/code]
:
: My hypothetical set-up:
:
: .NET DLL: MyDll.dll
: Namespace: MyNamespace
:
: Creating an object, called MyObject, that has only a default constructor:
:
: [code]
: Dim obj As Object = CreateObject("MyDll", "MyNamespace.MyObject")
: [/code]
:
: Creating an object, called AnotherObject, that has a constructor that takes a string argument for instance:
:
: [code]
: Dim arg() As Object = {"SomeArgValue"}
: Dim obj As Object = CreateObject("MyDll", "MyNamespace.MyObject", arg)
: [/code]
:
: