Reference to existing instance of a COMVisible object

587 views Asked by At

I have two .NET projects, and I would like to create a 2-way communication between these. ProjectA is a Shared Add-In, that creates a ribbon in an MS Office application. ProjectB is a WinForms app, that gathers/sends info from/to a server.

I have created an interface, IAddInCommunication, that defines methods which I want to call from ProjectB. This interface is in a separate class library project. The Connect class in ProjectA implements the IAddInCommunication interface (along with some others). I added a reference to the interface's DLL in ProjectB as well.

Now I'm trying to call the methods of the shared add-in, which is running in MS Word, for example. To achieve this, I'm using the following code (in ProjectB):

const string ProgId="ProjectA.Connect";
//...
Type officeAddInType = Type.GetTypeFromProgID(ProgId);           
object obj = Activator.CreateInstance(officeAddInType);
_remoteAddIn = (IAddInCommunication)obj;
_remoteAddIn.TestMethod("Hello");

The problem is, that the _remoteAddIn is not pointing to the instance running in Office. It's just a new instance (as one would expect). How could I connect to the actual running instance of the Shared-AddIn?

I solved the communication from ProjectA to ProjectB with the SendMessage API, because I needed that in my application anyway. I would only implement SendMessage communication in my Add-In if there's really no better way.

1

There are 1 answers

0
vcsjones On BEST ANSWER

You are probably looking for:

(IAddInCommunication)System.Runtime.InteropServices.Marshal.GetActiveObject(Pro‌​gId);

To get something from the running object table.