COM References problem in C# VS2008

M

Milan

Hi Volks,


I just try using/generating COM under C# .NET VS2008 and I have 3 questions:

1. why in some projects I see GUID defined for every interface and class and
in some projects is only written [ComVisible(true)] ? Why both approaches are
allowed ?

2. I did register COM "MyClassLibrary.dll" with:

regasm MyClassLibrary.dll /tlb: MyClassLibrary.TLB

and in another project(but in the same solution) I needed reference to
MyClassLibrary.dll COM object but in COM Tab von "Add Reference" Dialog there
was only MyClassLibrary.TLB and not MyClassLibrary.dll !?
In order to use COM I needed to go to location where COM MyClassLibrary.dll
is located and to add it as assembly reference. In the normal COM programming
one expect to use COM and OS should find (through Registry entry) exact COM
location, right ?
Why In COM Tab where the list of all COM are is not listed
MyClassLibrary.dll ?

3. in a couple of examples projects I saw in Object Browser (after double
click on COM reference) that name of COM in "Object Browser" tool is not any
more "Some_COM_Name" but "Interop.Some_COM_name", why ?


Any help is appretiated!

BR,
Milan
 
W

Wilson, Phil

COM is interface-based, so good practice is to declare interfaces and
methods with interface ids and class guids. In C++ you had no choice but to
do this. In C# you can decide not to, and use ComVisible, and get a load of
stuff (like an interface) generated for you that is fairly invisible if
you're new to COM, and will make debugging harder IMO.

I think the rest of your question can be answered by saying that a managed
code reference to a Win32 COM object starts as a reference to its type
library, and then via a generated interop assembly (converted from the tlb)
and that's what your code calls. In Win32 COM Dlls the typelibrary is often
embedded in the Dll but it doesn't have to be.You don't need the actual COM
Dll to do development, just the type lib. Testing you need both of course.
However, if you're not using any Win32 code at all, and you are calling from
managed code to managed code then you're not expected to be using COM at
all, just normal Add Reference to an assembly. So the only actual COM
reference you can add is to the type library (and an interop assembly). I
don't believe managed code->interop assembly->CCW->managed code actually
works, so don't bother with COM at all if all your calls are managed code to
managed code.
 
M

Milan

Hi Wilson,

the "Type Library" is not always denoted with extension ".TLB", right ? so
how to know if something is type library or not ?
Is it possible from .TBL to generate manage assemblies for .NET ?
and do I have to register both .TBL and .dll files if .dll has actual C++
COM definition ?

BR,
Zoki



Wilson said:
COM is interface-based, so good practice is to declare interfaces and
methods with interface ids and class guids. In C++ you had no choice but to
do this. In C# you can decide not to, and use ComVisible, and get a load of
stuff (like an interface) generated for you that is fairly invisible if
you're new to COM, and will make debugging harder IMO.

I think the rest of your question can be answered by saying that a managed
code reference to a Win32 COM object starts as a reference to its type
library, and then via a generated interop assembly (converted from the tlb)
and that's what your code calls. In Win32 COM Dlls the typelibrary is often
embedded in the Dll but it doesn't have to be.You don't need the actual COM
Dll to do development, just the type lib. Testing you need both of course.
However, if you're not using any Win32 code at all, and you are calling from
managed code to managed code then you're not expected to be using COM at
all, just normal Add Reference to an assembly. So the only actual COM
reference you can add is to the type library (and an interop assembly). I
don't believe managed code->interop assembly->CCW->managed code actually
works, so don't bother with COM at all if all your calls are managed code to
managed code.

--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972


Milan said:
Hi Volks,


I just try using/generating COM under C# .NET VS2008 and I have 3
questions:

1. why in some projects I see GUID defined for every interface and class
and
in some projects is only written [ComVisible(true)] ? Why both approaches
are
allowed ?

2. I did register COM "MyClassLibrary.dll" with:

regasm MyClassLibrary.dll /tlb: MyClassLibrary.TLB

and in another project(but in the same solution) I needed reference to
MyClassLibrary.dll COM object but in COM Tab von "Add Reference" Dialog
there
was only MyClassLibrary.TLB and not MyClassLibrary.dll !?
In order to use COM I needed to go to location where COM
MyClassLibrary.dll
is located and to add it as assembly reference. In the normal COM
programming
one expect to use COM and OS should find (through Registry entry) exact
COM
location, right ?
Why In COM Tab where the list of all COM are is not listed
MyClassLibrary.dll ?

3. in a couple of examples projects I saw in Object Browser (after double
click on COM reference) that name of COM in "Object Browser" tool is not
any
more "Some_COM_Name" but "Interop.Some_COM_name", why ?


Any help is appretiated!

BR,
Milan
 
W

Wilson, Phil

Type libraries are always .tlb, but many tools let you refer to a Win32 COM
DLL with its type library embedded in the resources.

Generating a managed assembly from a type library is exactly what tlbimp
does, the result being a COM interop assembly.

--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972


Milan said:
Hi Wilson,

the "Type Library" is not always denoted with extension ".TLB", right ? so
how to know if something is type library or not ?
Is it possible from .TBL to generate manage assemblies for .NET ?
and do I have to register both .TBL and .dll files if .dll has actual C++
COM definition ?

BR,
Zoki



Wilson said:
COM is interface-based, so good practice is to declare interfaces and
methods with interface ids and class guids. In C++ you had no choice but
to
do this. In C# you can decide not to, and use ComVisible, and get a load
of
stuff (like an interface) generated for you that is fairly invisible if
you're new to COM, and will make debugging harder IMO.

I think the rest of your question can be answered by saying that a
managed
code reference to a Win32 COM object starts as a reference to its type
library, and then via a generated interop assembly (converted from the
tlb)
and that's what your code calls. In Win32 COM Dlls the typelibrary is
often
embedded in the Dll but it doesn't have to be.You don't need the actual
COM
Dll to do development, just the type lib. Testing you need both of
course.
However, if you're not using any Win32 code at all, and you are calling
from
managed code to managed code then you're not expected to be using COM at
all, just normal Add Reference to an assembly. So the only actual COM
reference you can add is to the type library (and an interop assembly). I
don't believe managed code->interop assembly->CCW->managed code actually
works, so don't bother with COM at all if all your calls are managed code
to
managed code.

--
Phil Wilson
The Definitive Guide to Windows Installer
http://www.apress.com/book/view/1590592972


Milan said:
Hi Volks,


I just try using/generating COM under C# .NET VS2008 and I have 3
questions:

1. why in some projects I see GUID defined for every interface and
class
and
in some projects is only written [ComVisible(true)] ? Why both
approaches
are
allowed ?

2. I did register COM "MyClassLibrary.dll" with:

regasm MyClassLibrary.dll /tlb: MyClassLibrary.TLB

and in another project(but in the same solution) I needed reference to
MyClassLibrary.dll COM object but in COM Tab von "Add Reference" Dialog
there
was only MyClassLibrary.TLB and not MyClassLibrary.dll !?
In order to use COM I needed to go to location where COM
MyClassLibrary.dll
is located and to add it as assembly reference. In the normal COM
programming
one expect to use COM and OS should find (through Registry entry) exact
COM
location, right ?
Why In COM Tab where the list of all COM are is not listed
MyClassLibrary.dll ?

3. in a couple of examples projects I saw in Object Browser (after
double
click on COM reference) that name of COM in "Object Browser" tool is
not
any
more "Some_COM_Name" but "Interop.Some_COM_name", why ?


Any help is appretiated!

BR,
Milan
 

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