3

Can I generate COM visible assemblies (using CodeDom, or anything else) ?

I am looking to expose C# classes to VBA. Basically, I have an object-driven web server, and everytime I build my web server, I would like to also generate some wrapping classes (COM visible using CodeDom) as some clients will need these to access data from other contexts (Excel/VBA, etc...).

So is this possible at all ?

UPDATE: Trying the RegFree approach of Snoopy, I build that class into test2.dll with CodeDom:

   namespace Test
   { 
       public class TestClass
       {
        private double _d1;
        private double _d2;

              public double d1 {
                get { 
                  return _d1; }
                set { _d1 = value; }
              }

           public double d2 {
            get { return _d2; }
            set { _d2 = value; }
           }

           public double sum()
           {
            return d1 + d2;
           }
       }
   }

I get the following in VBE: enter image description here

BuZz
  • 16,318
  • 31
  • 86
  • 141
  • 1
    It just takes attributes, so no problem. – Hans Passant Nov 16 '12 at 11:08
  • I am puzzled by the COM registration aspect. My understanding is that the attribute would not be dealing with that. – BuZz Nov 16 '12 at 11:26
  • I assume the ComVisible attribute is handled by compiler magic, so this is why i think it will only work with registerless com, but i am not 100% sure, so feel free to correct me :-) – NickD Nov 16 '12 at 14:00

2 Answers2

1

If you look at the documentation for ComVisibleAttribute it says

The default is true, which indicates that the managed type is visible to COM. This attribute is not needed to make public managed assemblies and types visible; they are visible to COM by default.

Then there are some restrictions like only public types with default constructor etc.

adrianm
  • 14,468
  • 5
  • 55
  • 102
  • 1
    Okay then I'm a little puzzled, because if I need to expose C# to VBA by manually building a class project, I need to tick a couple of boxes. And my understanding is that I need a .tlb to be referenced from VBA. I don't seem to be able to produce that at the moment – BuZz Nov 16 '12 at 11:14
  • Not sure what your mean. You need to register the classes for them to be visible to VBA. Run `regasm /codebase myclasses.dll` (and ignore the warnings about unsigned assemblies) – adrianm Nov 16 '12 at 11:59
  • I think i do not fully understand com registration, but I guess an answer to the following question will clear things out for me : does that command affect the dll so that it can be distributed to all my clients and used ? Or is it affecting my registry only and all clients will need to register it ? – BuZz Nov 16 '12 at 12:03
  • VBA looks in the local registry to find available COM objects. i.e. it needs to be registered on each machine where the VBA application runs. I assume it could be distributed via policies but I have not tried it. – adrianm Nov 16 '12 at 12:15
  • Okay cool. understand all of that. However, registering on each machine is not an option. – BuZz Nov 16 '12 at 15:17
0

Yes, this is possible using Register Free COM.

1)Open Excel and add the following references

C:\Windows\Microsoft.NET\Framework\v4.0.30319\mscoree.tlb (Common Language Runtime Execution Engine 2.4 Library)

C:\Windows\Microsoft.NET\Framework\v4.0.30319\ mscorlib.tlb

Add your Code Dom generated Classlibrary to the same directory where the Excel Sheet is stored

3)use the following VBA Code:

Sub Command1_Click()
     Dim clr As CorRuntimeHost
     Dim domain As AppDomain
     Dim objTest As Object
     Dim fltResult As String

     Set clr = New CorRuntimeHost

     clr.Start
     clr.GetDefaultDomain domain
     Set objTest = domain.CreateInstanceFrom("C:\Bla\YourGeneratedClassLibrary.dll", "NetComClassLibrary.Info").Unwrap
     fltResult = objTest.Version
     clr.Stop

     '//Debug.Print fltResult
     MsgBox fltResult
 End Sub

More infos: http://blogs.msdn.com/b/junfeng/archive/2006/04/20/579748.aspx

NickD
  • 2,672
  • 7
  • 37
  • 58
  • this completes my answer: http://stackoverflow.com/questions/465882/generate-manifest-files-for-registration-free-com – NickD Nov 16 '12 at 11:33
  • Okay that looks good. But I won't get Intellisense on objTest, seems like – BuZz Nov 16 '12 at 13:20
  • Post your NetComClassLibrary.Info or however you renamed it :-) – NickD Nov 16 '12 at 15:35
  • I actually don't have one. What/Where is it supposed to come from ? – BuZz Nov 16 '12 at 15:46
  • You need one, else you need to register your type using regasm. See the link: http://stackoverflow.com/questions/465882/generate-manifest-files-for-registration-free-com – NickD Nov 16 '12 at 17:39
  • Yes the question is how do I get that generated with Codedom now – BuZz Nov 19 '12 at 10:31