Vb 2005 equivalent to VB6 CreateObject

G

Guest

Based on the documentation that I have found on this subject, I am trying to
use ‘Activator.CreateInstance(string, string)’ for a replacement for
CreateObject, but I don’t seem to be able to get it to work. Let me explain
what I am trying to do. The VB6 project that I am trying to convert has
hundreds of Objects all of which implement the same interface. The objects
differentiate themselves based on the ‘entity’ that they represent and an
effective date. So we have hundreds of entities which change ‘implementation
details’ 1 or more times every year. Instead of maintaining all of these
objects in a single project, I use a ‘catalog’ to fiond the right Object tol
hand back to the client. The catalogs are grouped by year and the logic
works something like this. Note that each of the catalog classes also
imnplement a single interface (ISelectObj).
..
..
..
mCatalog = GetCatalog(Year)
..
..
Private Function GetCatalog(Yr as integer) as ISelectObj
Dim CatName as string

CatName = “gpiCat†& Yr.ToString & “.gpiCatâ€
GetCatalog = CreateObject(CatName)
End Function

This works fine in VB6.

Under VB 2005 I created a gpiCat2006 project and compiled it. I copied the
contents of the Debug directory to a new folder (2006) I created in my test
applications Debug directory. And I try to load as follows.

Private Function GetCatalog(ByVal Yr As Integer) as ISelObj
Dim CatYr As String = Yr.ToString
Dim CatName As String = "gpiCat" & CatYr
Dim CatPath As String = My.Application.Info.DirectoryPath & "\" & CatYr &
"\" & CatName & ".dll"

Try
Return Activator.CreateInstance(CatPath, CatName).Unwrap
Catch ex As Exception
Throw New ArgumentException("Catalog for Requested Year not found.")
End Try

I get the following exception:
System.IO.FileLoadException = {"Could not load file or assembly
'D:\\TestProj\\gpiCatalog\\WindowsApplication1\\bin\\Debug\\2006\\gpiCat2006.dll'
or one of its dependencies. The given assembly name or codebase was invalid.
(Exception from HRESULT: 0x80131047

The Actual path I use is: CatPath =
"D:\TestProj\gpiCatalog\WindowsApplication1\bin\Debug\2006\gpiCat2006.dll"
Why are the ‘\’ replaced with ‘\\’?

I tried an end around and recompiled the Cat2006 project as a com object and
registered it. I was then able to use the following line w/o error:
mCatalog = CreateObject("gpiCat2006.gpiCat2006")

What am I doing wrong?

One other thing. I would like to be able to get to the ‘’gpiCat2006.dll’
w/o having to have it installed in the path of the client application. How
would I go about that and how would I use the ‘CreateInstance’ method to get
to it?

Thanks in advance for any input.
 
P

Peter Huang [MSFT]

Hi Terry,

Based on my understanding, you use the syntax below to create a COM object
in VB6.
mCatalog = CreateObject("gpiCat2006.gpiCat2006")
And the same code works in VB.NET.

Now you want to use the Activator.CreateInstance approach to create the COM
Object.

If I misunderstood, please feel free to post here.

For CreateInstance, it needs a .NET type.
Here is some code snippet for your reference.
[VB6 COM]
Public Function Test() As String
Test = "Hello World!"
End Function

[VB.NET]
Imports System.Runtime.InteropServices
Module Module1
Sub Approach1()
Dim o As Object = CreateObject("TestLib.TestObj")
Console.WriteLine(o.Test)
End Sub
Sub Approach2()
Dim t As Type = Type.GetTypeFromProgID("TestLib.TestObj")
Dim o As Object = Activator.CreateInstance(t)
Console.WriteLine(o.Test)
End Sub
Sub Main()
Approach1()
Approach2()
End Sub
End Module

Please perform the test and let me know the result!

Thanks!


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Peter,
Thanks for the reply. I am sorry if I was not clear. I actually want to
load and create an instance of an object in a .Net assembly. What I tried to
point out in my original post was that when I was unable to get
CreateInstance to work, I went back and exposed the the original assembly as
a Com object and was able to get CreateObject to work. But my original
intent was not to use a Com obj.

In your responce, you used the following line of code:
Dim t As Type = Type.GetTypeFromProgID("TestLib.TestObj")
This looks to me like you have a reference to TestLib, which is not the case
in my example. I hold a reference to an Interface and not to the actual
object that implements that interface. Also, TestLib is not in the GAC.
Hence, I am using the following:
.....CreateInstance("D:\TestProj\gpiCatalog\WindowsApplication1\bin\Debug\2006\gpiCat2006.dll", "gpiCat2006")

at which point I get a fileload exception saying it could not load
D:\\TestProj\\gpiCatalog\\WindowsApplication1\\bin\\Debug\\2006\\gpiCat2006.dll'

why does it have double '\'s in the path and is that the problem?
 
P

Peter Huang [MSFT]

Hi Terry,

Thanks for your quickly reply!
For the call Dim t As Type = Type.GetTypeFromProgID("TestLib.TestObj")
The "TestLib.TestObj" is the progid of the COM object, it is not a .NET
assembly, so it was not in the GAC.

So far I understand that you want to CreateInstance on a .NET assembly.
Here goes the code for your reference, all these is pure .NET code.
We need to use CreateInstanceFrom.

[Test Assembly]
Imports System.Reflection
Module Module1
Sub Main()
Dim path As String = <path to the dll>
Dim o As Object = Nothing
Try
o = Activator.CreateInstanceFrom(path,
"WindowsApplication1.TestClass").Unwrap()
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
If o IsNot Nothing Then
Console.WriteLine(o.Test())
End If
End Sub
End Module


["WindowsApplication1.TestClass"]
Public Class TestClass
Public Function Test() As String
Return "hello"
End Function
End Class


Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Peter,
Thanks for the reply. I am off fishing for 3 days and will give it a try
first thing when I get back. I wwill keep you posted.
 
P

Peter Huang [MSFT]

Hi Terry,

Thanks for your quickly reply!

I look forward to hear from you!

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Peter,
Thanks again - I got it to work. So, if I understand correctly, I use
CreateInstanceFrom if the .Net assembly is NOT in the GAC and in this case I
supply a path to the assem bly. If the assembly is in the GAC, I use
CreateInstance and do NOT supply a path. And if it is a COM object, I use
CreateObject or I use the method you first gave me with the GetType. The
documentation seems awful weak in this area.
 
P

Peter Huang [MSFT]

Hi Terry,

Thanks for your quickly reply!
You are correct, if you still have any concern, please feel free to post
here.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

Guest

Hi Again Peter,
I do have another question for you and I am not sure that what I want to
do is possible. Having added an assembly to the GAC, is there a way to find
out where the assembly was added from? Basically what I would like to do is
a add a single 'Catalog.dll' (which contains the names of 50 or so other
assemblies) to the GAC and then be able to find the other 50 dll's (not in
the GAC) based on the original location of the 'Catalog' dll.
To put it another way. I want to add 1 assembly to the GAC. And then,
based on where is was 'added from', be able to find the other 50 dll's so
that I can use CreateInstanceFrom to load 1 of them.
 
P

Peter Huang [MSFT]

Hi Terry,

Thanks for your posting!
Based on my research, I think we can not do that.
Because after we added an assembly into we even can delete it.
We can consider the behavior that install it into the GAC as the simple
copy job. Actually we can do it by using Explorer to copy the assembly into
%windir%\assembly, and the installation into GAC is done. It did not store
the information about where did you

Also for your concern, I suggest you store the path where the assemblies is
in the app.config, so that you can read the setting when the application is
started and change them when you want. Because the app.config is just a
plain text xml file.

Best regards,

Peter Huang

Microsoft Online Community Support
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top