Loading an assembly at runtime 
Author Message
 Loading an assembly at runtime

I am trying to upgrade a VB6 project that was loading
ActiveX dlls at runtime like this:
<<
Dim Object as IMyInterface

Set Object = CreateObject("AnActivX.DLL")
Object.Use

In VB.Net, I no longer want to load ActiveX dlls.  I want
to load assemblies.

I gather I can either use
System.Reflection.Assembly.Load("DLLName.MainClass")
or
System.Activator.CreateInstance("DLLName","MainClass")

Which one, or either?

The problem I'm having is I get an error when trying
either one.  It says " File or assembly name "DLLName",
or one of its dependencies, was not found."

With ActiveX dlls, VB knows where to find the DLL by
looking in the registry.  How does VB know where to find
the DLL that matches an assembly name?  Am I entering in
my assebly names correctly?



Sat, 08 May 2004 13:00:11 GMT  
 Loading an assembly at runtime
Hi Chris,

You might be interested in the thread above, 'Difficulty creating object
with CreateObject'. I have been having the same difficulty as you creating
dynamically resolved (not late bound since the interface is defined) classes
at run time. Like you, I have observed that this is MUCH more easily
accomplished via COM than via .Net.

In any case, the solution to your dilemna is presented in that thread above.
(Approx. 8 threads previous to this one.)

Hope this helps,
--

Joseph Geretz

(replace nospam with FPSNow to reply by e-mail)


Quote:
> I am trying to upgrade a VB6 project that was loading
> ActiveX dlls at runtime like this:
> <<
> Dim Object as IMyInterface

> Set Object = CreateObject("AnActivX.DLL")
> Object.Use

> In VB.Net, I no longer want to load ActiveX dlls.  I want
> to load assemblies.

> I gather I can either use
> System.Reflection.Assembly.Load("DLLName.MainClass")
> or
> System.Activator.CreateInstance("DLLName","MainClass")

> Which one, or either?

> The problem I'm having is I get an error when trying
> either one.  It says " File or assembly name "DLLName",
> or one of its dependencies, was not found."

> With ActiveX dlls, VB knows where to find the DLL by
> looking in the registry.  How does VB know where to find
> the DLL that matches an assembly name?  Am I entering in
> my assebly names correctly?



Sat, 08 May 2004 14:04:14 GMT  
 Loading an assembly at runtime
I am sure as the documentation becomes better, there will be a better
method, but this is what I am currently doing:

imports System.Reflection
Dim reflecObj As Assembly = Assembly.LoadFrom("<relativepath>DllName.dll")
Dim MyObj = reflecObj.CreateInstance("DllName.ClassName")

The LoadFrom docs says <relativepath> is relative to the current directory,
which works (I did not try an absolute path)


Quote:
> I am trying to upgrade a VB6 project that was loading
> ActiveX dlls at runtime like this:
> <<
> Dim Object as IMyInterface

> Set Object = CreateObject("AnActivX.DLL")
> Object.Use

> In VB.Net, I no longer want to load ActiveX dlls.  I want
> to load assemblies.

> I gather I can either use
> System.Reflection.Assembly.Load("DLLName.MainClass")
> or
> System.Activator.CreateInstance("DLLName","MainClass")

> Which one, or either?

> The problem I'm having is I get an error when trying
> either one.  It says " File or assembly name "DLLName",
> or one of its dependencies, was not found."

> With ActiveX dlls, VB knows where to find the DLL by
> looking in the registry.  How does VB know where to find
> the DLL that matches an assembly name?  Am I entering in
> my assebly names correctly?



Sun, 09 May 2004 00:26:46 GMT  
 Loading an assembly at runtime
Chris is right about everything except this part...

    Dim MyObj = reflecObj.CreateInstance("DllName.ClassName")

CreateInstance requires the fully qualified name, which is the full
namespace and the classname.

So, if the object you are trying to instantiate is in the namespace
Development.Beta and the name of the class is clsEmployee, CreateInstance
will look like this...

    Dim MyObj = reflecObj.CreateInstance("Development.Beta.clsEmployee")

Hope this helps...

--Matt


Quote:
> I am sure as the documentation becomes better, there will be a better
> method, but this is what I am currently doing:

> imports System.Reflection
> Dim reflecObj As Assembly = Assembly.LoadFrom("<relativepath>DllName.dll")
> Dim MyObj = reflecObj.CreateInstance("DllName.ClassName")

> The LoadFrom docs says <relativepath> is relative to the current
directory,
> which works (I did not try an absolute path)



> > I am trying to upgrade a VB6 project that was loading
> > ActiveX dlls at runtime like this:
> > <<
> > Dim Object as IMyInterface

> > Set Object = CreateObject("AnActivX.DLL")
> > Object.Use

> > In VB.Net, I no longer want to load ActiveX dlls.  I want
> > to load assemblies.

> > I gather I can either use
> > System.Reflection.Assembly.Load("DLLName.MainClass")
> > or
> > System.Activator.CreateInstance("DLLName","MainClass")

> > Which one, or either?

> > The problem I'm having is I get an error when trying
> > either one.  It says " File or assembly name "DLLName",
> > or one of its dependencies, was not found."

> > With ActiveX dlls, VB knows where to find the DLL by
> > looking in the registry.  How does VB know where to find
> > the DLL that matches an assembly name?  Am I entering in
> > my assebly names correctly?



Sun, 09 May 2004 16:23:11 GMT  
 Loading an assembly at runtime
You know what, mine actually works as in my example.
Maybe it is because I did not explicitly declare my classes inside a
namespace that it works? (haven't tried it yet either).

Quote:

> Chris is right about everything except this part...

>     Dim MyObj = reflecObj.CreateInstance("DllName.ClassName")

> CreateInstance requires the fully qualified name, which is the full
> namespace and the classname.

> So, if the object you are trying to instantiate is in the namespace
> Development.Beta and the name of the class is clsEmployee, CreateInstance
> will look like this...

>     Dim MyObj = reflecObj.CreateInstance("Development.Beta.clsEmployee")

> Hope this helps...

> --Matt



> > I am sure as the documentation becomes better, there will be a better
> > method, but this is what I am currently doing:

> > imports System.Reflection
> > Dim reflecObj As Assembly =

Assembly.LoadFrom("<relativepath>DllName.dll")

- Show quoted text -

Quote:
> > Dim MyObj = reflecObj.CreateInstance("DllName.ClassName")

> > The LoadFrom docs says <relativepath> is relative to the current
> directory,
> > which works (I did not try an absolute path)



> > > I am trying to upgrade a VB6 project that was loading
> > > ActiveX dlls at runtime like this:
> > > <<
> > > Dim Object as IMyInterface

> > > Set Object = CreateObject("AnActivX.DLL")
> > > Object.Use

> > > In VB.Net, I no longer want to load ActiveX dlls.  I want
> > > to load assemblies.

> > > I gather I can either use
> > > System.Reflection.Assembly.Load("DLLName.MainClass")
> > > or
> > > System.Activator.CreateInstance("DLLName","MainClass")

> > > Which one, or either?

> > > The problem I'm having is I get an error when trying
> > > either one.  It says " File or assembly name "DLLName",
> > > or one of its dependencies, was not found."

> > > With ActiveX dlls, VB knows where to find the DLL by
> > > looking in the registry.  How does VB know where to find
> > > the DLL that matches an assembly name?  Am I entering in
> > > my assebly names correctly?



Sun, 09 May 2004 07:08:54 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Including Assemblies at runtime?

2. Load Assembly /CreateInstance

3. Load Assembly

4. One or more of the types in the assembly unable to load

5. How do I load and run assemblies dynamically

6. Dynamically loading an assembly?

7. Loading control from assembly

8. Trying to Dunamically Load Assembly via Reflection

9. Types in the assembly failed to load

10. VB6: application in runtime environment with error on loading form: ado cannot load object

11. Can not load file or assembly

12. could not load file or assembly

 

 
Powered by phpBB® Forum Software