ComVisible(false) doesn't work as expected

T

Tony Johansson

Hi!

I have this class library that is called from COM.
The strange thing is that even if I add this attribute [ComVisible(false)]
to the only method that the COM can call it still work.
Accordingly to the docs this attibute will hide those members that have this
attribute set to false like this [ComVisible(false)]

So why won't this work in my case ?

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MyInterop
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///

[Guid("3E0E2EB2-CC13-40fb-9346-34809CB2418C")]
public interface IMyDotNetInterface
{
void ShowDialog();
}


[ClassInterface(ClassInterfaceType.None)]
[Guid("3A13EB34-3930-4e06-A3EB-10724CCC2F4B")]
public class MyDotNetClass:IMyDotNetInterface
{
public MyDotNetClass()
{}

[ComVisible(false)]
public void ShowDialog()
{
MessageBox.Show ("I am a Managed DotNET C# COM Object Dialog");
}
}
}

//Tony
 
T

Tony Johansson

Tony Johansson said:
Hi!

I have this class library that is called from COM.
The strange thing is that even if I add this attribute [ComVisible(false)]
to the only method that the COM can call it still work.
Accordingly to the docs this attibute will hide those members that have
this attribute set to false like this [ComVisible(false)]

So why won't this work in my case ?

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace MyInterop
{
/// <summary>
/// Summary description for Class1.
/// </summary>
///

[Guid("3E0E2EB2-CC13-40fb-9346-34809CB2418C")]
public interface IMyDotNetInterface
{
void ShowDialog();
}


[ClassInterface(ClassInterfaceType.None)]
[Guid("3A13EB34-3930-4e06-A3EB-10724CCC2F4B")]
public class MyDotNetClass:IMyDotNetInterface
{
public MyDotNetClass()
{}

[ComVisible(false)]
public void ShowDialog()
{
MessageBox.Show ("I am a Managed DotNET C# COM Object Dialog");
}
}
}

//Tony

This ComVisibleAttributeattribute is a Design-Time Attributes and here is
what the docs says about it
This attribute specifies whether this member is exposed to COM. It accepts
true or false; the default value is true. Setting the attribute to false on
the assembly hides all public types within the assembly. You can selectively
make types visible within the assembly by setting the individual types to
true.

Setting the attribute to false on a specific type hides that type and its
members. However, you cannot make members of a type visible if the type is
invisible. Setting the attribute to false on a type prevents that type from
being exported to a type library.

So if I set this ComVisibleAttribute to false as I have done this should
result in that my method ShowDialog should not be visible for COM but this
attribute settings has no effect my method is still visible for COM.

//Tony
 
P

Peter Duniho

Tony said:
[...]
So if I set this ComVisibleAttribute to false as I have done this should
result in that my method ShowDialog should not be visible for COM but this
attribute settings has no effect my method is still visible for COM.

Without a concise-but-complete code example, it's hard to say for sure.
But, is it possible that you have forgotten to unregister the type
from the previous COM-visible build, causing the new type library to
have no effect?

Alternatively, you can always look at the type library directly and see
if the attribute is doing what is says it's supposed to do. That will
at least let you know where to look for the problem.

Pete
 
T

Tony Johansson

Peter Duniho said:
Tony said:
[...]
So if I set this ComVisibleAttribute to false as I have done this should
result in that my method ShowDialog should not be visible for COM but
this
attribute settings has no effect my method is still visible for COM.

Without a concise-but-complete code example, it's hard to say for sure.
But, is it possible that you have forgotten to unregister the type from
the previous COM-visible build, causing the new type library to have no
effect?

Alternatively, you can always look at the type library directly and see if
the attribute is doing what is says it's supposed to do. That will at
least let you know where to look for the problem.

Pete

My communication is from COM to managed code so the type library is a COM
thingy that describe the COM so I can't use neither ILDASM or Red Gats's
Reflector to eximine this type library

If the communication had been from managed code to COM I would have been
able to look into this type library by using either ildasm ot Red Gats's
Reflector.

So what do you mean by saying Alternatively, you can always look at the type
library directly and see
if the attribute is doing what is says it's supposed to do. That will at
least let you know where to look for the problem.

So here is the component I have one .NET library dll one COM exe file one
type library that describe the COM component.
What steps exactly should I use to make this work when I use this ComVisible
attribute.

//Tony
 
T

Tony Johansson

Peter Duniho said:
Tony said:
[...]
So if I set this ComVisibleAttribute to false as I have done this should
result in that my method ShowDialog should not be visible for COM but
this
attribute settings has no effect my method is still visible for COM.

Without a concise-but-complete code example, it's hard to say for sure.
But, is it possible that you have forgotten to unregister the type from
the previous COM-visible build, causing the new type library to have no
effect?

Alternatively, you can always look at the type library directly and see if
the attribute is doing what is says it's supposed to do. That will at
least let you know where to look for the problem.

Pete

Hi!

To make sure that I run a new version of the .NET library dll I changed
something in the text that the MessageBox is writing
and that works. So I do know that I succeed to make the COM to use my new
dll version.
But the same problem exist that the ComVisible(false) doesn't work. It could
be a bug.

[System.Runtime.InteropServices.ComVisible(false)]
public void ShowDialog()
{
MessageBox.Show ("I. am a Managed DotNET C# COM Object Dialog..");
}

//Tony
 
P

Peter Duniho

Tony said:
My communication is from COM to managed code so the type library is a COM
thingy that describe the COM so I can't use neither ILDASM or Red Gats's
Reflector to eximine this type library

Your question is not a C# question. It's a COM question, and you'll get
better information asking it in a forum where COM questions are more
topical.

That said, one thing that it appears you are confused about is what a
COM type library really is. While it has the name "library" in it, it
is _nothing_ like an unmanaged .lib library or a managed or unmanaged
..dll library. It is literally a library of types, with no
implementation whatsoever. It simply _describes_ the COM objects.

If you want to examine the contents of the .tlb, you need to use the COM
tools available specific to that task. I haven't used it in awhile but
just looking at the docs it appears there is still the Object Browser in
Visual Studio. You should be able to use that to look at what the .tlb
information actually is:
http://msdn.microsoft.com/en-us/library/exy1facf(v=VS.100).aspx

Note that since the .tlb is not the actual implementation of your COM
object, it is not the final word on what's available from your COM
object or not. After you've rebuilt your COM object with the new
settings, you probably have to make sure you've unregistered the
previous version and re-register the new version, with the new .tlb.

Pete
 

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