Figure 2   Invoking the Dialog

void WINAPI GetInfo(HWND hWnd, char* lpszText) {
   // Need to do this to set up the resources…
   AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
   if(lpszText) {
      // All we have here is the window handle of 
      //  the parent. Need to turn it into a CWnd object
      CWnd* pWnd = CWnd::FromHandle(hWnd);

      CSomeMFCDialog dlg(pWnd);

      dlg.m_strText = lpszText;
      if(dlg.DoModal() == IDOK) {
         strcpy(lpszText, dlg.m_strText);
      }
   }
}


Figure 3   CSomeMFCDialog

Header


 long WINAPI CreateSomeMFCDialog(HWND hWnd, char* lpszText);
 void WINAPI DeleteSomeMFCDialog(long hDlg);
 int WINAPI SomeMFCDialogDoModal(long hDlg, char* lpszBuffer);

Implementation


 long WINAPI CreateSomeMFCDialog(HWND hWnd, char* lpszText) {
    AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
 
    CWnd* pWnd = CWnd::FromHandle(hWnd);
 
    // We're behind the DLL boundary, so we can 
    // use operator new to create a new 
    // CSomeMFCDialog object 
    CSomeMFCDialog* pDlg = new CSomeMFCDialog(pWnd);
 
    pDlg->SetData(lpszText);
 
    // Pass the pointer dialog back to the client code
    return (long)pDlg; 
 }
 
 void WINAPI DeleteSomeMFCDialog(long hDlg) {
    AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
 
    // hDlg had better be a CSomeMFCDialog object...
    // could use RTTI and IsKindOf to find out...
    CSomeMFCDialog* pDlg = (CSomeMFCDialog*)hDlg;
    if(pDlg)
      delete pDlg;
 }
 
 int WINAPI SomeMFCDialogDoModal(long hDlg, 
                                 char* lpszBuffer) {
    AFX_MANAGE_STATE(AfxGetStaticModuleState( ));
 
    // hDlg had better be a CSomeMFCDialog object...
    // could use RTTI and IsKindOf to find out... 
    CSomeMFCDialog* pDlg = (CSomeMFCDialog*)hDlg;
    int nResult = pDlg->DoModal();
 
    if(nResult == IDOK && lpszBuffer) {
       strcpy(lpszBuffer, pDlg->m_strText);
    }
 
    return nResult;
 }

Figure 4   Using the SomeMFCDialog API


 Rem Function Prototypes: 
 Declare Function CreateSomeMFCDialog Lib "visprog" (ByVal hWnd As Long, _
     ByVal lpBuffer As String) As Long
 Declare Sub DeleteSomeMFCDialog Lib "visprog" (ByVal hWnd As Long)
 Declare Function SomeMFCDialogDoModal Lib "visprog" (ByVal hDlg As Long, _
     ByVal lpszBuffer As String) As Integer
 
 Rem: Button handler that uses the API:
 Private Sub GetInfoUsingHandle_Click()
    Dim hDlg As Long
    hDlg = CreateSomeMFCDialog(MainForm.hWnd, TextFromDlg.Caption)
    SomeMFCDialogDoModal hDlg, txt
    DeleteSomeMFCDialog (hDlg)
    TextFromDlg.Caption = txt
 End Sub