latosvarun
latosvarun

Reputation: 127

Hosting a WPF control in a native MFC Dialog C++ application

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

Upvotes: -1

Views: 58

Answers (0)

Related Questions