tags:

views:

116

answers:

1

I have an application that has its own database as a COM component. I need to write an application that links into an already running instance for this COM database using C# so that I can tweek the values in the database and see how they effect the application.

In the past I have written similar applications using Monikers.

Is there anyway of getting access to a COM component that is ready running, from a .NET app?

+1  A: 

To use monikers in .NET you might want to check out Marshal.BindToMoniker which internally calls the Win32 BindToMoniker function:

void BindToMoniker()
{
    string pptxFile;
    PowerPoint.Presentation pptx;

    pptx = (PowerPoint.Presentation)Marshal.BindToMoniker(pptxFile);
    pptx.Application.Visible = Office.MsoTriState.msoTrue;
}

Another option is to get an IDispatch pointer from a window handle using AccessibleObjectFromWindow (A full sample is described in this question):

// AccessibleObjectFromWindow gets the IDispatch pointer of an object
// that supports IAccessible, which allows us to get to the native OM.
[DllImport("Oleacc.dll")]
private static extern int AccessibleObjectFromWindow(
    int hwnd, uint dwObjectID,
    byte[] riid,
    ref PowerPoint.DocumentWindow ptr);

The following blog post (related to MS Office automation) by Andrew Whitechapel might also be a helpful resource on how to create and retrieve COM objects in .NET:

Launching Office Apps Programmatically

0xA3