Shane
Shane

Reputation: 2361

Unable to instantiate COM library built with Visual Studio 2008 in VBA inside a 64 bit application

I am trying to build an object library which can be registered and used from VBA using 64 bit excel 2010. I am using Dev Studio 2008. I think this is a 64 bit issue as I am pretty sure this worked when I tried it with with my previous version of Excel (XP). I have clicked "Make COM visible" in the Assembly info Information dialog and "Register for COM interop" in the build tag. When I set the target type to x64 I can't see the library in the references dialog at all. I if I select Any CPU I can register it but I get a "Can't create Active X object" error when I try to instantiate it.

namespace Tester
{
    [Guid("D6F88E95-8A27-4ae6-B6DE-0542A0FC7039")]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    public interface _Numbers
    {       
        int GetDay();
        int GetMonth();
        int GetYear();
        int DayOfYear();
    }

    [Guid("13FE32AD-4BF8-495f-AB4D-6C61BD463EA4")]
    [ClassInterface(ClassInterfaceType.None)]
    [ProgId("Tester.Numbers")]
    public class Numbers : _Numbers
    {
        public Numbers(){}

        public int GetDay()
        {
            return(DateTime.Today.Day);
        }

        public int GetMonth()
        {
            return(DateTime.Today.Month);
        }

        public int GetYear()
        {
            return(DateTime.Today.Year);
        }

        public int DayOfYear()
        {
            return(DateTime.Now.DayOfYear);
        }
    }
}

Upvotes: 1

Views: 493

Answers (1)

Frédéric Hamidi
Frédéric Hamidi

Reputation: 263047

Since Visual Studio is a 32-bit process, it will erroneously run the 32-bit version of regasm.exe in order to register your 64-bit assembly. Of course, that version of regasm.exe will write to the 32-bit portion of the registry, so that won't work.

One way to solve the problem would be to run the 64-bit version of regasm.exe on the target assembly yourself (e.g. in a post-build step). You'll need to export its type library using the /tlb option, and you'll also need to pass the /codebase option since the assembly doesn't reside in the GAC:

"%SystemRoot%\Microsoft.NET\Framework64\v2.0.50727\regasm.exe"
    /tlb /codebase "$(TargetPath)"

Upvotes: 1

Related Questions