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 Answer 1

4

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.