0

I would like to host a WPF control in a MFC Dialog application. I am following the chapter 7.4.1 (Using a mixed-mode extension DLL) instructions mentioned in book (C++/CLI in Action by NISHANT SIVAKUMAR) to create this application.

Please help me the below issue

The following is sample important code

 class AFX_EXT_CLASS CMyWnd : public CWnd
{
    DECLARE_DYNAMIC(CMyWnd)

public:
    CMyWnd();
    virtual ~CMyWnd();

    void Stop();
    void Play();
    virtual void SetSource(LPCTSTR strSource);
    virtual void OnMediaEnded();

protected:
    DECLARE_MESSAGE_MAP()

public:
    afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);

protected:
#ifdef _MANAGED                                             
    gcroot<CustomControlLibrary::MyControl^> m_ObjHandle;
#else
    intptr_t m_ObjHandle;
#endif                                                      
};


void CMyWnd::SetSource(LPCTSTR strSource)
{
    m_ObjHandle->mVid->Source = gcnew System::Uri(gcnew System::String(strSource));
}



 int CMyWnd::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
    if (CWnd::OnCreate(lpCreateStruct) == -1)
        return -1;

    CRect rect;
    GetClientRect(&rect);

    HwndSourceParameters sourceParams("CustomHost");
    sourceParams.ParentWindow = (IntPtr)m_hWnd;
    sourceParams.Width = rect.Width();
    sourceParams.Height = rect.Height();
    sourceParams.WindowStyle = WS_VISIBLE | WS_CHILD;

    HwndSource^ source = gcnew HwndSource(sourceParams);
    CustomControlLibrary::MyControl^ control = gcnew CustomControlLibrary::MyControl();
    control->InitializeComponent();

    m_ObjHandle = control;
    source->RootVisual = control;

    return 0;
}


class CMyDerivedWnd : public CMyWnd
{
public:
    CMyDerivedWnd() :bRepeatVideo(false)
    {       
    }
    bool bRepeatVideo;
    CString strVideoPath;
    virtual void OnMediaEnded()
    {
        if (bRepeatVideo)
        {
            // The following 3 lines are to workaround 
            // a bug in the February CTP
            // <Workaround>
            SetSource(_T("c:\\nonexistent.avi"));
            Sleep(200);
            SetSource(strVideoPath);
            // </Workaround>

            Play();
        }
    }
};



// CAvalonHostDlgDlg dialog
class CAvalonHostDlgDlg : public CDialogEx
{
// Construction
public:
    CAvalonHostDlgDlg(CWnd* pParent = nullptr); // standard constructor

// Dialog Data
#ifdef AFX_DESIGN_TIME
    enum { IDD = IDD_AVALONHOSTDLG_DIALOG };
#endif

    protected:
    virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support


// Implementation
protected:
    HICON m_hIcon;
    CMyDerivedWnd m_WpfWnd;

    // Generated message map functions
    virtual BOOL OnInitDialog();
    afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
    afx_msg void OnPaint();
    afx_msg HCURSOR OnQueryDragIcon();
    DECLARE_MESSAGE_MAP()

public:
    afx_msg void OnBnClickedCheckLoop();
public:
    CComboBox m_ComboVideos;
public:
    afx_msg void OnCbnSelchangeComboVideos();
    afx_msg void OnCbnSelchangeCombovideos();
    afx_msg void OnBnClickedCheckloop();
};


BOOL CAvalonHostDlgDlg::OnInitDialog()
{
    CDialogEx::OnInitDialog();
    .....
    ....
    m_WpfWnd.SetSource(_T("C:\\Users\\Downloads\\testvid.avi"));
    m_WpfWnd.Create(NULL, _T("Host"), WS_VISIBLE | WS_CHILD, CRect(0, 0, 200, 200), this, 1000); //==> (PROBLEM CREATING m_WpfWnd)

}

I have problem with m_WpfWnd SetSource() & Create() Methods during Dialog Initialization. I get their error during SetSource & Create methods ( Error = Exception thrown at 0x00007FFC03D850DE in AvalonHostDlg.exe: 0xC0000005: Access violation reading location 0x0000000000000160.). Why the Methods of CMyDerivedWnd are not properly triggered. I even tried with this memberCMyWnd m_WpfWnd instead of CMyDerivedWnd m_WpfWnd

8
  • It doesn't sound like a good idea. It would be much more affordable and logical to host C++ code in a fully fledged .NET WPF application. I don't say that hosting of WPF in native C++ application is totally impossible, I say that is so difficult and purely maintainable that nothing worth the effort. Commented Feb 28 at 18:02
  • Why did you tag C++/CLI? It does not seem relevant. And if it was relevant, you could develop entire WPF application in C++/CLI. Commented Feb 28 at 18:03
  • 1
    @SergeyAKryukov The question is tagged c++-cli because the code is using C++/CLI. Given that there is dedicated WPF hosting support, hosting a WPF control in a Win32 application isn't as wild as you make it out to be. Commented Mar 1 at 11:21
  • You did not answer if you also use C++, not only C++/CLI. If you do, also add appropriate tag. Commented Mar 1 at 12:25
  • 1
    @SergeyAKryukov C++/CLI is a superset of C++. The code clearly uses C++ and C++/CLI, and both tags are present. Commented Mar 1 at 14:14

0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.