views:

957

answers:

2

I've made a simple C# DLL (that's part of a much larger project) using VS2005. I need to use the DLL in Excel via VBA code so I am using COM Interop on the assembly. I am trying to make the build process automatically generate the necessary TLB file so that I don't need to go to the command line and use regasm after every build.

My problem is that although the DLL compiles and builds fine, it does not generate a TLB file. Instead, the error in the title prints out in the output box.

I've gotten other DLLs to build TLB files by going to the project's properties in VS2005 -> Build -> Output -> Check "Register for COM interop". Also I have [assembly: ComVisible(true)] in the AssemblyInfo.cs.

Here's the summary of the source for the problem DLL and the DLL that it references for a return type:

using System;
using System.IO;
using System.Runtime.InteropServices;
using SymbolTable;

namespace ProblemLibrary
{
    public class Foo
    {    
        public Foo(string filename)
        {
            ...
        }

        // method to read a text file into a SymbolTable
        public SymbolTable BuildDataSet(string[] selected)
        {
            ...
        }
    }
}

Here is a summary of SymbolTable.dll. It holds a return type that ProblemLibrary uses.

using System;
using System.Collections.Generic;

namespace SymbolTable
{
    public class SymbolTable
    {
        readonly Dictionary<SymbolInfoStub, string> _symbols = new Dictionary<SymbolInfoStub, string>();

       /*methods that interact with Dictionary snipped*/
    }
}
A: 

In the AssemblyInfo.cs file, make sure you have the following:

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components.  If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(true)]

UPDATE:

Read: http://stackoverflow.com/questions/179452/how-can-i-make-use-of-net-objects-from-within-excel-vba

Which links to: http://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/

Chris Lively
Thanks for the info, but I wrote in my description that I already have that.
I've gotten other C# DLLs to work in COM before. Though the links didn't help me with my issue, I did learn about the link between COM client intellisense and [ClassInterface(ClassInterfaceType.AutoDual)] in C# from them. Thanks for that.
+2  A: 
  1. You need to have ctor without any params.
  2. You should have GuidAttribute and ProgIdAttribute around the classes.
  3. Its better to mark the assembly as ComVisible(false) and mark explicitly the classes that need export.
  4. Use interfaces for your classes.
  5. Make sure the you have GuidAttribute in the assembly level.

    [Guid("<PUT-GUID-HERE-1>")]
    [ComVisible(true)]
    interface IFoo
    {
        void DoFoo();
    }
    
    
    [Guid("<PUT-GUID-HERE-2>")]
    [ComVisible(true)]
    [ProgId("ProgId.Foo")]
    class Foo : IFoo
    {
        public void DoFoo()
        {
        }
    }
    
Shay Erlichmen
Awesome, the Constructor without any parameters did the trick. Thanks a lot!