0
votes

The problem
I got "Cannot access ActiveX Control type library" error while trying to add variable for ActiveX control in Visual Studio designer (either through right click -> "Add variable" or through class wizard) in MFC dialog-based application. I need it for handling methods and events of this ActiveX control, which was created from windows form control and registered in system (Windows 7).

What I have
Here is simplified C# COM implementation (one method to call from MFC side and one event for notifying MFC about anything):

[ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IWinFormControl
{
    [DispId(1)]
    string MethodTest(string s);
}

[ComVisible(true),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IWinFormControlEvents
{
    [DispId(2)]
    void SimpleEvent(string s);
}

[ComVisible(true),
ClassInterface(ClassInterfaceType.None),
ComDefaultInterface(typeof(IWinFormControl)),
ComSourceInterfaces(typeof(IWinFormControlEvents))]
public partial class WinFormControlImpl : UserControl, IWinFormControl
{
    public delegate void SimpleEventType(string s);
    public event SimpleEventType SimpleEvent; 

    public WinFormControlImpl()
    {
        this.InitializeComponent();
    }

    private void Button_Click(object sender, EventArgs e)
    {
        if (this.SimpleEvent != null)
        {
            this.SimpleEvent("TESTEVENT");
        }
    }

    public string MethodTest(string s)
    {
        MessageBox.Show("TESTMETHOD " + s);
    }

    // ... ComRegisterFunction and ComUnregisterFunction
}

"Register for COM interop" project flag checked, after build component successfully adding to registry, I can test it with TstCon32 utility and invoke MethodTest there.

In MFC project I open dialog panel in design view, right click and choose "Insert ActiveX Control", then successfully find my ActiveX control and insert it into dialog panel. Application runs and I see my ActiveX with all content. But I can't create variable for this control.

What I tried
Research gives me two general workarounds. First is to forget about COM, add reference to .NET project into MFC project and create native C++ wrapper for user control using CWinFormsControl and /CLR. This is not appropriate for my task.

Another possible solution is to create variable for ActiveX control manually, but I couldn't find enough examples to implement this. The closest hack I found is in question, where ActiveX object created inside CWnd variable and functional of this COM accessed through interface, but I don't get how to put visual content of this variable info displaying dialog panel.

The question is: how to create variable for ActiveX component (created in C#) in MFC dialog-based window?

1

1 Answers

0
votes

Hope it helps :

if (m_pWndFramework == NULL)//  CWnd* m_pWndFramework;

{

    m_pWndFramework = new CWnd;
    CRect rcRange;    //set the display area
    rcRange.left = 0;
    rcRange.top = 0;
    rcRange.right = 500;
    rcRange.bottom = 500;

    //get the ID of the user control
    m_pWndFramework = GetDlgItem(IDC_TESTT1);

    //if(bCreate)
    {
        IUnknown * pUn = m_pWndFramework->GetControlUnknown(); 
        if(pUn)
        { 
            pUn->QueryInterface(IID_IDispatch,(void **)&m_pFramework);     //m_pFramework
            bResult = TRUE;
        }
    }
}

m_pFramework->messageShowClose();