Your first attempt will not work because , you looking for a modal dialog , which has a style of
WS_POPUP
A popup window can not contain a style of
WS_CHILD
Folowing code will not return dialog template ID (for popup windows), it returns control ID for child windows (including dialog template ID of child dialogs)
That field is used for menu resource ID for popup windows , so if your modal dialog has not got any menu then that function will return 0
GetWindowLong(hwndForeGround, GWL_ID);
GetLastActivePopup function (Windows)[
^] can be used to get last popup window but you still can not idetify that dialog by Template ID , even if you will find a CDialog class
you will already use this for testing purpose ,you can compare window title to ensure that you found the dialog you looking for
following code should help
CFrameWnd *pMainFrame = (CFrameWnd *)AfxGetMainWnd();
if(pMainFrame)
{
CWnd *pWnd = pMainFrame->GetLastActivePopup();
if(pWnd)
{
if(pWnd->IsKindOf(RUNTIME_CLASS(CDialog)))
{
CDialog *pDialog = reinterpret_cast<cdialog*>(pWnd);
if(pDialog)
{
CString strTitle;
pDialog->GetWindowText(strTitle);
if(strTitle.CompareNoCase(_T("My Dialog")) == 0)
{
pDialog->EndDialog(0);
}
}
}
}
}