473,545 Members | 2,443 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vb 2005 equivalent to VB6 CreateObject

Based on the documentation that I have found on this subject, I am trying to
use ‘Activator.Cr eateInstance(st ring, 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 ‘implementati on
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(Ca tName)
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(ByVa l Yr As Integer) as ISelObj
Dim CatYr As String = Yr.ToString
Dim CatName As String = "gpiCat" & CatYr
Dim CatPath As String = My.Application. Info.DirectoryP ath & "\" & CatYr &
"\" & CatName & ".dll"

Try
Return Activator.Creat eInstance(CatPa th, CatName).Unwrap
Catch ex As Exception
Throw New ArgumentExcepti on("Catalog for Requested Year not found.")
End Try

I get the following exception:
System.IO.FileL oadException = {"Could not load file or assembly
'D:\\TestProj\\ gpiCatalog\\Win dowsApplication 1\\bin\\Debug\\ 2006\\gpiCat200 6.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\gp iCatalog\Window sApplication1\b in\Debug\2006\g piCat2006.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("g piCat2006.gpiCa t2006")

What am I doing wrong?

One other thing. I would like to be able to get to the ‘’gpiCat200 6.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 ‘CreateInstan ce’ method to get
to it?

Thanks in advance for any input.

--
Terry
Jun 21 '06 #1
9 5199
Hi Terry,

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

Now you want to use the Activator.Creat eInstance 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("T estLib.TestObj" )
Console.WriteLi ne(o.Test)
End Sub
Sub Approach2()
Dim t As Type = Type.GetTypeFro mProgID("TestLi b.TestObj")
Dim o As Object = Activator.Creat eInstance(t)
Console.WriteLi ne(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.

Jun 22 '06 #2
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.GetTypeFro mProgID("TestLi b.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:
.....CreateInst ance("D:\TestPr oj\gpiCatalog\W indowsApplicati on1\bin\Debug\2 006\gpiCat2006. dll", "gpiCat2006 ")

at which point I get a fileload exception saying it could not load
D:\\TestProj\\g piCatalog\\Wind owsApplication1 \\bin\\Debug\\2 006\\gpiCat2006 .dll'

why does it have double '\'s in the path and is that the problem?
--
Terry
""Peter Huang" [MSFT]" wrote:
Hi Terry,

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

Now you want to use the Activator.Creat eInstance 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("T estLib.TestObj" )
Console.WriteLi ne(o.Test)
End Sub
Sub Approach2()
Dim t As Type = Type.GetTypeFro mProgID("TestLi b.TestObj")
Dim o As Object = Activator.Creat eInstance(t)
Console.WriteLi ne(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.

Jun 22 '06 #3
Hi Terry,

Thanks for your quickly reply!
For the call Dim t As Type = Type.GetTypeFro mProgID("TestLi b.TestObj")
The "TestLib.TestOb j" 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 CreateInstanceF rom.

[Test Assembly]
Imports System.Reflecti on
Module Module1
Sub Main()
Dim path As String = <path to the dll>
Dim o As Object = Nothing
Try
o = Activator.Creat eInstanceFrom(p ath,
"WindowsApplica tion1.TestClass ").Unwrap()
Catch ex As Exception
Console.WriteLi ne(ex.ToString( ))
End Try
If o IsNot Nothing Then
Console.WriteLi ne(o.Test())
End If
End Sub
End Module
["WindowsApplica tion1.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.

Jun 23 '06 #4
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.
--
Terry
""Peter Huang" [MSFT]" wrote:
Hi Terry,

Thanks for your quickly reply!
For the call Dim t As Type = Type.GetTypeFro mProgID("TestLi b.TestObj")
The "TestLib.TestOb j" 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 CreateInstanceF rom.

[Test Assembly]
Imports System.Reflecti on
Module Module1
Sub Main()
Dim path As String = <path to the dll>
Dim o As Object = Nothing
Try
o = Activator.Creat eInstanceFrom(p ath,
"WindowsApplica tion1.TestClass ").Unwrap()
Catch ex As Exception
Console.WriteLi ne(ex.ToString( ))
End Try
If o IsNot Nothing Then
Console.WriteLi ne(o.Test())
End If
End Sub
End Module
["WindowsApplica tion1.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.

Jun 24 '06 #5
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.

Jun 26 '06 #6
Hi Peter,
Thanks again - I got it to work. So, if I understand correctly, I use
CreateInstanceF rom 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.
--
Terry
""Peter Huang" [MSFT]" wrote:
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.

Jun 26 '06 #7
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.

Jun 28 '06 #8
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 CreateInstanceF rom to load 1 of them.

--
Terry
""Peter Huang" [MSFT]" wrote:
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.

Jun 28 '06 #9
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%\assemb ly, 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.

Jun 29 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2124
by: sathyashrayan | last post by:
Following are the selected thread from the date:30-jan-2005 to 31-jan-2005. I did not use any name because of the subject is important. You can get the original thread by typing the subject "string" in google comp.lang.c archives.Hope this helps.Hope I am not bothering any one. am I? ...
2
7264
by: Gary McGill | last post by:
In (traditional) VB, it's possible to instantiate a COM object using something like Set obj = CreateObject("MyDll.MyObject), and then call that object's methods (either using late binding, or using early binding if the "obj" variable is declared as an object of a particular type). I'd like to do something similar in C#, but I'm not sure...
1
4724
by: ricolee99 | last post by:
Hi There, I'm trying to convert some VB code into C#. There is a function defined in VB, GetAbsolutePathName(path) where it returns the complete path from the root of the drive for the specified path. so for ex.: Assume that the current directory is c:\temp\test:
2
12381
by: Ron | last post by:
Hello, I have to invoke instances of MS Excel and MS Access from my vb.net applications so that I can invoke procedures that reside in the Excel and Access applications from the vb.net application (as in remotely). After I make a reference to the com object library for Excel or Access I use CreateObject to instantiate an session of Excel...
0
1176
by: Rob Blackmore | last post by:
Hi, A simple question I'm sure, but can someone point me in the right direction of how to have the equivalent of 2 projects in VB.Net, 1 that is a standard EXE and 1 that is an ActiveX.EXE that can either be run standalone or invoked from the first synchronously via CreateObject. The example is that I have a seperate file processing...
10
8179
by: Steve | last post by:
I am trying to create a DLL in Visual Studio 2005-Visual Basic that contains custom functions. I believe I need to use COM interop to allow VBA code in Excel 2002 to access it. I've studied everything I can find on COM Interop and .NET. I've also tried many of the 'Walkthroughs' on the MSDN site relating to COM add-ins, .NET and Office XP...
1
3815
by: pompair | last post by:
Hi, Could someone give a pointer how to import couple of hundred images into Sql Server 2005 Express Edition database? Is there a tool for it? Can it be done with Sql Management Studio or is it just a matter of writing own piece of software (a little helper app) to do it? -timonardo
2
7463
by: Carlton Kirby | last post by:
I need to execute a job on a SQL Express 2005 instance (no SQLAgent). The job will be executed manually by a user, so it doesn't need to be scheduled to run automatically. I thought I could execute the job through a stored procedure, but it appears that SQL Agent is necessary even for that. The job was given to me by a software vendor to...
0
7420
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7934
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6003
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5349
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupr who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4966
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3476
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1908
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1033
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
731
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.