locked
What .NET Framework Classes are COMVisible? RRS feed

  • Question

  • hello

    so i saw, there are some classes in the .net framework they have the attribute "COMVisible" on "true".

    i've found a few of them on "System.Collection" (e.g. ArrayList)

    my question: are there any other classes in the framework they are "COMVisible", so i can use them in other languages (e.g. vb, with "CreateObject(...)")

    thanks for feedbacks!


    regards, jan

    Monday, February 24, 2014 9:38 AM

Answers

  • "i think when i declare the whole project as "COMVisible", then all classes / interfaces are COMVisible too in a "implicite" way?"

    It's recommended to make the project ComVisible(false) and apply ComVisible(true) only to those classes and interfaces that are supposed to be used via COM interop.

    That said, I don't think that this particular detail will make any difference with regards to your issue, it's just "good practice".

    What might be a problem is the lack of a Guid attribute. That means that a Guid will be generated automatically and you really don't want to happen when it comes to COM.

    Here's a small example I tested and it worked fine for me. The methods show up in OleView and I can call them from a vbs script.

    [ComVisible(true), Guid("E9A5631B-5985-4004-B83A-4DA76C5C4607")]
    public interface IHttpClient
    {
        string Hello(string name);
    }
    
    [ComVisible(true), Guid("D9587229-DDFA-4982-886E-E2996369FAA5")]
    public class HttpClient : IHttpClient
    {
        public string Hello(string name)
        {
            return "Hello " + name;
        }
    }
    

    Monday, March 3, 2014 12:28 PM

All replies

  • Hi Jan,

    It’s hard for me to list all the classes which are ComVisible to you. There are so many classes in Framework Class Library. So the best practice is to query the class library when necessary. And you can turn C# DLL into a com interop component. For more information, see the following link. http://stackoverflow.com/questions/7092553/turn-a-simple-c-sharp-dll-into-a-com-interop-component.

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Tuesday, February 25, 2014 2:38 AM
  • hello

    thank you for your feedback.

    i know how to compile .NET code as "COMVisible"...

    But i don't want to compile new classes, because of that my question about the existing classes in the .NET framework. (i want to use them in "classic asp"...)


    regards, jan

    Tuesday, February 25, 2014 8:17 AM
  • Just looking for ComVisible(true) is not enough, to be able to use CreateObject the class must also be registered. You probably have the Windows SDK installed, search for oleview.exe in your Program Files directory. Run oleview and look under the .NET Category node, you should see a System. class that are registered.

    There are a few other types from System.Collections, a few types from System.IO, a few types from System.Threading and a few various others that are unlikely to be very useful.

    I don't know nothing about classic asp but probably I'd think twice before dragging in a different runtime and paying the cost of COM interop.

    Tuesday, February 25, 2014 8:28 AM
  • hello mike

    thank you for your feedback. yes, i know i need to register them with "regsvr32.exe".


    regards, jan

    Tuesday, February 25, 2014 4:07 PM
  • Nope, you cannot register .NET assemblies with regsvr32.exe. You could use regasm probably but that's not the point, those types that are available for use outside .NET are already registered.

    Tuesday, February 25, 2014 4:16 PM
  • @mike: oh yes, you are right! thank you for your feedback!

    @all: can somebody tell me why i can't see the methods for my "com enabled"-.net-assembly?

    screenshots here:

    1.) "MS OLE/COM Object Viewer" -> http://s28.postimg.org/ccpi2gcml/Untitderzwrled.jpg

    2.) Code in C#2010 -> http://s28.postimg.org/4lmk94cmk/cshasrp.jpg

    but it seems i'm able to use that methods in a "classic asp" page. (Set xxx = CreateObject(...)) my http client test class works there...


    regards, jan

    Friday, February 28, 2014 10:57 AM
  • "COMView" (http://www.japheth.de/COMView.html)oe me the same: http://s28.postimg.org/itvl70t7v/comv.png

    ...the 7 methods of the "TypeKind DISPATCH" entries seems to be "internal" (QueryInterface and so on...)

    thx for feedback.


    regards, jan

    Friday, February 28, 2014 11:10 AM
  • Normally you should create an interface (with ComVisible and Guid attributes) and have your COM class implement it. The way the typelib looks in your screenshot I guess you didn't do that.

    Friday, February 28, 2014 12:42 PM
  • hello mike

    thank you for your feedback.

    now i used the following code to test it:

    public class Class1 : Class1Interface
        {
            public Class1()
            {

            }

            public void test1()
            {

            }

            public void test2()
            {

            }

            public void test3()
            {

            }
        }

    the project ist COMVisible, for sure! (Project Properties -> Build -> Application -> Assembly Information -> Make Assembly COM-Visible) and it will be registered automatically ("Register for COM-Interop") but the methods are still invisible in the OLE/COM viewer...

    why? this is really strange...


    regards, jan


    • Edited by jm_83 Friday, February 28, 2014 10:07 PM
    Friday, February 28, 2014 10:05 PM
  • "the project ist COMVisible, for sure! "

    But are the class and the interface COM visible too? Please post relevant code samples. It doesn't matter what methods you have in the class, what may matter in this case is if the interface and class have the proper COM attributes and you haven't shown any of that.

    Sunday, March 2, 2014 7:14 AM
  • hello

    i think when i declare the whole project as "COMVisible", then all classes / interfaces are COMVisible too in a "implicite" way? Or am i wrong?

    My example code is posted on top; the interface "Class1Interface" implements all 3 methods...


    regards, jan

    Monday, March 3, 2014 7:52 AM
  • "i think when i declare the whole project as "COMVisible", then all classes / interfaces are COMVisible too in a "implicite" way?"

    It's recommended to make the project ComVisible(false) and apply ComVisible(true) only to those classes and interfaces that are supposed to be used via COM interop.

    That said, I don't think that this particular detail will make any difference with regards to your issue, it's just "good practice".

    What might be a problem is the lack of a Guid attribute. That means that a Guid will be generated automatically and you really don't want to happen when it comes to COM.

    Here's a small example I tested and it worked fine for me. The methods show up in OleView and I can call them from a vbs script.

    [ComVisible(true), Guid("E9A5631B-5985-4004-B83A-4DA76C5C4607")]
    public interface IHttpClient
    {
        string Hello(string name);
    }
    
    [ComVisible(true), Guid("D9587229-DDFA-4982-886E-E2996369FAA5")]
    public class HttpClient : IHttpClient
    {
        public string Hello(string name)
        {
            return "Hello " + name;
        }
    }
    

    Monday, March 3, 2014 12:28 PM
  • hello mike

    thank you very much, i will try it with your code!


    regards, jan

    Thursday, March 6, 2014 9:26 AM
  • now i saw that i have to import  System.Runtime.InteropServices to realize that! thanks again! :-)

    regards, jan


    • Edited by jm_83 Friday, March 7, 2014 10:24 AM
    Friday, March 7, 2014 10:23 AM