0
votes

I have a COM object called Test and the .rgs files looks as follows. I do see that this class is registered in the registery at the location HKEY_LOCAL_MACHINE>>SOFTWARE>>Classes>>CLSID.

HKCR
{   
    NoRemove CLSID
    {

        ForceRemove {17899D17-698D-4781-894C-3BAA80F3D4BB} = s 'Test Class'
        {
            ForceRemove 'Programmable'
            InprocServer32 = s '%MODULE%'
            {
                val ThreadingModel = s 'Apartment'
            }
            val AppID = s '{D2A47892-4C98-11D6-8C6D-00105A1790CD}'
            'TypeLib' = s '{9A887890-4C99-11d6-8C6D-00105A1790CD}'
        }
    }
}

When I create an instance of this object inside a C++ project it works fine. The code used is:

ATLTRACE( _T( " | CoCreateInstance Test\n" ) );
    hr = m_Test.CoCreateInstance(CLSID_Test, NULL, CLSCTX_INPROC_SERVER);
    if (FAILED(hr))
        CREATE_COM_OBJECT_ERROR(_T("Test"));

However when I try to call this object from .NET application it errors out saying "An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in PatternEditor.exe

Additional information: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))". The code used is as follows:

        public partial class MainWindow : Window
    {
        Main m_Main = new Main();
        Test m_Test;

        public MainWindow()
        {
            InitializeComponent();
            //This line errors
            m_Test = m_Main.Test;
        }
    }
1

1 Answers

0
votes

Usually, the easiest way to use COM objects inside .NET is creating a reference in the project (the tab COM). It will generate a "stub" and you can use as simple .NET code.

Other way is through reflection. This answer has an idea: C# COM and attaching events