Monday, May 29, 2006

Accessing mdbg COM wrappers from native C++

I needed a way to pass the Debugger COM object wrappers from mdbg to a COM library I wrote in native C++. Suppose you want to pass ICorDebugModule from mdbg (or, more likely, an mdbg plugin) to a C++ COM object:

  1. In your COM object's IDL file, add a method that will be called from mdbg:

    interface IHelper : IDispatch {
    ...
    HRESULT HelperMethod([in] LONGLONG piunkCorDebugModule);
    ...

  2. Add the method implementation:

    STDMETHODIMP IHelper::HelperMethod(/*[in]*/ LONGLONG piunkCorDebugModule)
    {
    CComPtr spiCorDebugModule;
    hr = ((IUnknown*) piunkCorModule)->QueryInterface(__uuidof(spiCorDebugModule.p),
    (void**) &spiCorDebugModule.p);
    ...

  3. Create a COM Interop assembly for your COM object so you can call it from managed code. Right click on mdbg (or your mdbg plugin) and select Add Reference. Click on the COM tab and select your COM object.
  4. Call the COM method from mdbg/plugin:

    using System.Runtime.InteropServices;

    IntPtr ptrUnkCorDebugModule = Marshal.GetIUnknownForObject(
    module.CorModule.CorModuleInterface);

    HelperLib.IHelper = new HelperLib.IHelper();
    helper.HelperMethod(ptrUnkCorDebugModule);

No comments: