How to register UDF/RTD within VSTO project

2019-07-19 02:04发布

问题:

All,

This is a follow up for my question here.

My setup:

  • Visual Studio 10
  • Language C#
  • Excel 2007+
  • Windows XP+

What I would like to achieve is this:

  • Create VSTO addin for Excel with a custom Ribbon component and a custom Task Pane
  • Create an RTD server
  • Create some UDF's that wrap the =RTD()-calls for the Excel users
  • Have the clients install the Addin through a ClickOnce installation

Apart from the last requirement, I am basically done. The only problem I have is to get the RTD server and the UDF's registered on the target machine without using regasm.exe. I basically want the client to indeed click once and then forget about it, especially since there are hundreds of potential users, all distributed geographically over the whole world.

Also, since they are not technically savvy, I do not want them to have to register COM components manually or anything like that.

I do not care if the UDF's and the RTD server are a separate project, as long as I can include and register them together with the VSTO addin via ClickOnce.

I looked into registration-free COM, but was not for the life of me able to get this to work in my VSTO addin. What I did was create a second project (a library class) and trying to export and register it for COM interop, then to include it into my 'main' project and use it there: it either would not compile (Cannot embed interop types from assembly [...] because it is missing either the 'ImportedFromTypeLibAttribute' attribute or the 'PrimaryInteropAssemblyAttribute' attribute) or not register.

There are loads and loads of examples on the internet for the separate parts of my requirements, but nothing for the full monty.

Am I completely off track here? Can somebody please point me in the right direction and ideally offer some example code/configuration?

Cheers, Che

回答1:

This can be accomplished using an installer class that runs RegAsm.exe during installation. See: How to register a .NET CCW with regasm from a Visual Studio 2008 Setup project

Hope this helps, Frank



回答2:

With the help of the link provided by Frank above, I finally ended up doing this in my ThisAddin class:

private void ThisAddIn_Startup(object sender, System.EventArgs e)
    {
        RegistrationServices regsrv = new RegistrationServices();
        if (!regsrv.RegisterAssembly(GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase))
        {
            throw new Exception("Failed to register for COM Interop.");
        }
    }

    private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
    {
        RegistrationServices regsrv = new RegistrationServices();
        if (!regsrv.UnregisterAssembly(GetType().Assembly))
        {
            throw new Exception("Failed to unregister for COM Interop.");
        }
    }

I know that this is not ideal, but at least I know for sure that all COM objects are registered if my plugin is started...