Pregunta

I've got two pieces of technology that plug into excel.

One is a COM Addin that implements the IDTExtensibility2 interface. Another is an RTD server that is implemented via Excel-DNA.

Both of these objects are instantiated by excel. Each of them needs to access a third object at runtime to get data from and push it to excel.

Since I can't hand this object to the excel plugins I've made it a singleton with the hope that each of them could share the same instance.

Unfortunately when running the code, each of them don't see the instance of the singleton object that the other has created.

How can I get both addins to reference the same object?

Let's work under the assumption that both addins need to remain and I'd rather not go to an interprocess communication setup.

TL/DR Two excel plugins need to share a 3rd object, but making the third object a singleton doesn't work as each excel plugin doesn't see the instance of the third object that the other plugin created.

What is the solution to such a problem?

¿Fue útil?

Solución

You add-ins are loading in separate AppDomains. One option is to integrate the COM Add-In into the Excel-DNA add-in. This might be as easy as:

  1. Add your COM add-in code to your Excel-DNA project,
  2. Change your add-in class to derive from ExcelComAddIn (instead of IDTExtensibility2),
  3. Load that class in your AutoOpen via ExcelComAddInHelper.LoadComAddIn(...).

Otherwise you will need some sort of inter-AppDomain communication. You can set up a Marshal-By-Reference object that you set to the Object property of the COMAddIn object corresponding to your loaded add-in, and retrieve that using the COM interfaces from the Excel-DNA add-in.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top