Wednesday, April 14, 2004

Creating COM Callable Wrapper (CCW)

Query on using .Net components in VB6

-----Original Message-----
From: Anup Ganji
Sent: Wednesday, April 14, 2004 10:05 AM
To: Vishal Joshi
Subject: Help Required!!

Hi Vishal!
I request for a small help from you, for which I would be talking some time of your's.
A friend of mine is facing a problem in accessing a dot net (C#) coded component in VB6.0, I heard that if could come up with a wrapper around the dot net coded component you can access it from lower versions.

Waiting for more inputs from you.

Thanks,
Anup


A Quick Solution

-----Original Message-----
From: Vishal Joshi
Sent: Wednesday, April 14, 2004 10:33 AM
To: Anup Ganji
Subject: RE: Help Required!!

Hi Anup,
It is very true that by making minor changes to C# component it can be made visible to com components… There are certain attributes and steps that we need to follow for this…. These attribute informs the CLR to generate CCW(Com Callable Wrapper) which creates the Interface by which VB6 components can call C# components…
Its been long time since I wrote such component but the basic steps should be as follows…
In you C# component use using System.Runtime.InteropServices;
Now we need to create a interface with the same name as your .Net class only with "_" in the beginning… Precisely speaking in this interface you will write only those methods which have to be exposed out of your .Net component
Eg
using System.Runtime.InteropServices;
using System;
namespace SameAsClassesNamespace
{
[Guid("use GUIDGen.exe to create GUID for urself")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface _MyNetComponent
{
//ur method signatures will come here
}
}
Remember the attribute InterfaceType is important here as it tells which kindof ComInterface is required…
Now in your class following are the modifications to be made…

using System.Runtime.InteropServices;
using System;
namespace SameAsClassesNamespace
{
[Guid("use GUIDGen.exe to create GUID for urself")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("SameAsClassesNamespace.MyNetComponent")]
public class MyNetComponent : _MyNetComponent
{
//ur method definition will come here
}
}
Now go to project properties --> Configuration Properties and set last two properties (Register for Com Interop & Create Debug information) to true… Well this can be done manually but no big reason to go about doing it… {Find out more about Regasm.exe, sn.exe, gacutil.exe if you are interested in doing it manually, but better don't…}
Now as you compile your component should be visible to COM… And ofcourse you know the ProgId above to use your component...
Enjoy!!

Warm regards,
Vishal Joshi
Ph#: +1-309-763-2242.
If You Think YOU CAN.. You Can...

1 comment:

Anonymous said...

How should I set up the "Register for Com Interop & Create Debug information" automatically while using Managed C++ instead of C#. I get into a lot of problem when I try to register the classes manually using Regasm.exe.

Thanks,
Prit