Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2006

Tip: Looking for answers? Try searching our database.

Need help with COM in .NET and CreateObject

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Magnus - 05 Sep 2005 16:25 GMT
I need to make a class in .NET as a COM class, and get access to it with
CreateObject in VB6. I googled it and saw this link:
http://www.codeproject.com/dotnet/nettocom.asp?df=100&forumid=14076&exp=0&select
=1092357#xx1092357xx


But in this example, and others I have tried, CreateObject in VB6 throws the
infamous exception 'ActiveX component can't create object'

How do I get my COM class in .NET to be accessible with CreateObject from
VB6? I really would appreciate some input.
Zdenek Drlik - 06 Sep 2005 13:49 GMT
> I need to make a class in .NET as a COM class, and get access to it with
> CreateObject in VB6. I googled it and saw this link:
[quoted text clipped - 5 lines]
> How do I get my COM class in .NET to be accessible with CreateObject from
> VB6? I really would appreciate some input.

Create interface for your COM-visible class and implementation class:

using System;
using System.Runtime.InteropServices;

namespace MyNs {
// ...

[ComVisible(true), GuidAttribute("D541C809-198A-42ad-BB2F-E04C93AE7BFB")]
public interface IMyInterface {
    string ReturnSomething();
}

[ComVisible(true), GuidAttribute("1070C8DB-7D3A-4582-A071-5CC40815A319")]
public class MyClass : IMyInterface {
    public string ReturnSomething() {
        return "Hello world";
    }
}

// ...
}

Compile assembly, next create type library and register COM with regasm
tool:

regasm MyAssembly.dll /codebas /tlb:MyAssembly.tlb

And then you can use it with late-bind from VB 6:

dim myClass as object
set myClass = CreateObject("MyNs.MyClass");
dim returnValue as string
returnValue = myClass.ReturnSomething()

or reference it to your project and use it with early binding...

Zdenek D.
Magnus - 06 Sep 2005 14:32 GMT
Thanks for your response. I got it to work by adding a New Component/COM
Class as well (VB.NET made some GUID and stuff for you), and it is registered
when compiled.

The reason it didnt work before was because the project was an executable.
It seems the COM class has to be in a class library (makes sense, I know).

However, neither this method nor the one you gave makes it possible to get
the current running instance of the COM object with CreateObject.

For example, in VB.NET I use new to create the COM class, and then I would
like for the VB6 app to get that instance when using CreateObject. It always
creates a new instance instead.

If I do the COM class in VB6, I can get it to reuse the already created COM
instance, but is that possible in .NET?

> > I need to make a class in .NET as a COM class, and get access to it with
> > CreateObject in VB6. I googled it and saw this link:
[quoted text clipped - 44 lines]
>
> Zdenek D.
shawn clarke - 06 Sep 2005 16:34 GMT
Right-click your project and select Properties...
On the Property Pages dialog, under Configuration Properties, under Build,
select Register for COM Interop.
Build the solution and you should be able to create your ActiceX object.

One thing that tripped me up initially when doing this was that I did not
know what the reference was when late binding. Typically .Net will use a
default of <project>.<namespace>.<class> unless you specify the ProgID that
you want to use. You can do this in VB.Net like this:

<Guid("00085FFA-1764-45e1-BC6A-F0542B858F76"),
ClassInterface(ClassInterfaceType.None), ProgId("LateBindName")> _
Public Class MyClass
...
End Class

You should then be able to use Set obj = CreateObject("LateBindName")

Hope this helps,
Shawn

> Thanks for your response. I got it to work by adding a New Component/COM
> Class as well (VB.NET made some GUID and stuff for you), and it is
[quoted text clipped - 67 lines]
>>
>> Zdenek D.
Magnus - 07 Sep 2005 12:13 GMT
Yes, I got CreateObject to work, but I cant get it to reuse an already
existing instance.

In my case, we have 2 apps that needs to talk to each other, and the VB6
solution is to do 2 COM objects, one for each app, and than the app creates
an instance of its own COM object and use CreateObject to get the other apps
COM object instance. That way the applications can talk to eachother with
normal late-bind method calls.

Like this:
- App A creates its COM object C1
- App B creates its COM object C2
- App A uses CreateObject to get the inctance of C2 from App B
- App B uses CreateObject to get the inctance of C1 from App A

But when I do the COM object in .NET, CreateObject always gives me a new
instance, and I dont seem to be able to get the already created instance from
the other app. GetObject does the same.

> Right-click your project and select Properties...
> On the Property Pages dialog, under Configuration Properties, under Build,
[quoted text clipped - 88 lines]
> >>
> >> Zdenek D.
Robert Jordan - 07 Sep 2005 12:27 GMT
Hi Magnus,

> Yes, I got CreateObject to work, but I cant get it to reuse an already
> existing instance.
[quoted text clipped - 14 lines]
> instance, and I dont seem to be able to get the already created instance from
> the other app. GetObject does the same.

The framework doesn't support LocalServer COM/OLE servers.
Take a look at the registry entries of your objects:
they are all InProc servers. That's why you allways get a new instance.

Rob
Magnus - 07 Sep 2005 13:27 GMT
Uhoh, to bad.

Thanks for the reply anyway, atleast I dont have to search for the answer
anymore.

> The framework doesn't support LocalServer COM/OLE servers.
> Take a look at the registry entries of your objects:
> they are all InProc servers. That's why you allways get a new instance.
>
> Rob
Dirk Behnke - 14 Sep 2005 08:29 GMT
> Uhoh, to bad.
>
[quoted text clipped - 6 lines]
>>
>>Rob

And by the way, GetObject for ActiveX servers also in COM only give you
one of multiple running ActiveX servers back. You can not distinguish them
Dirk
ChAmP - 13 Nov 2006 08:54 GMT
Hi, I have followed the indicated way, but when I try to take the files to
another machine the following mistake goes out for me: Class does not support
Automation. I have tried to return to register the tlb but the problem
persists. Can you say to me why? thanks
ChAmP

>> I need to make a class in .NET as a COM class, and get access to it with
>> CreateObject in VB6. I googled it and saw this link:
[quoted text clipped - 40 lines]
>
>Zdenek D.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.