BadJerry, I found a solution that worked for me. However after such a long time I am not at all sure what the key points were. I hope these chunks of code can help you. I removed all error checking.
This function implements the COM interface. It creates a MFC GUI thread which displays the dialog.
STDMETHODIMP CMyComServer::MyInterfaceFunction(...)
{
AfxSetResourceHandle(_AtlBaseModule.GetResourceInstance());
CMyGuiThread *pThread = dynamic_cast<cmyguithread*>AfxBeginThread(RUNTIME_CLASS(CMyGuiThread)));
}
The GUI thread, in its InitInstance function, creates the dialog.
BOOL CMyGuiThread::InitInstance()
{
CMyDlg *pWnd = new CMyDlg;
BOOL bRes = pWnd->Create();
if (bRes)
m_pWnd = pWnd;
else
delete pWnd;
return bRes;
}
The CMyGuiThread::m_pWnd member stores a pointer to the CMyDlg object so that you can destroy the dialog when it is no longer required. (Be aware that a dialog can destroy itself in response to IDOK or IDCANCEL control clicks and inresponse to a click on the X icon in the upper right corner; in that case, m_pWnd points to invalid memory; you must overwrite CMyDlg::OnCancel and OnOk to prevent this.)
The dialog, in its Create function, calls one of the inherited overloads.
BOOL CMyDlg::Create()
{
return __super::Create(IDD, NULL);
}
Maybe the important thing is not to use the AFX_MANAGE_STATE macro which you usually put at the beginning of each method that is called over the COM interface.