Code:
hDlg = CreateDialog( g_hInstance,
MAKEINTRESOURCE( IDD_DIALOG1 ),
GetForegroundWindow(),
DialogProc );
// store GuiModule's this ptr in hidden control so DialogProc can
// access member functions and data
SetDlgItemInt( hDlg, IDC_EDIT3, (UINT)this, FALSE );
// customize dialog depending on situation
SetDlgItemText( hDlg, IDC_VSTNAME_EDIT, VSTName );
if( isRegistered )
{
ShowWindow( GetDlgItem( hDlg, IDC_TRYDEMO_BUTTON ), SW_HIDE );
ShowWindow( GetDlgItem( hDlg, IDCANCEL ), SW_HIDE );
ShowWindow( GetDlgItem( hDlg, IDC_DAYSLEFT_EDIT ), SW_HIDE );
ShowWindow( GetDlgItem( hDlg, IDC_DEMO_EDIT1 ), SW_HIDE );
ShowWindow( GetDlgItem( hDlg, IDC_DEMO_EDIT2 ), SW_HIDE );
SetDlgItemText( hDlg, IDOK, "OK" );
SetDlgItemText( hDlg, IDC_REGISTRATION_EDIT, "Registered to:" );
SetDlgItemText( hDlg, IDC_USERID_EDIT, userID );
SendDlgItemMessage( hDlg, IDC_USERID_EDIT, EM_SETREADONLY, TRUE, 0 );
SetDlgItemText( hDlg, IDC_LICENSEKEY_EDIT, licenseKey );
SendDlgItemMessage( hDlg, IDC_LICENSEKEY_EDIT, EM_SETREADONLY, TRUE, 0 );
}
else
{
SetDlgItemText( hDlg, IDC_REGISTRATION_EDIT, "Enter registration information below:" );
char daysStr[10];
itoa( demoDaysLeft, daysStr, 10 );
SetDlgItemText( hDlg, IDC_DEMO_EDIT1, "Demo period:" );
SetDlgItemText( hDlg, IDC_DAYSLEFT_EDIT, daysStr );
SetDlgItemText( hDlg, IDC_DEMO_EDIT2, "Days left" );
if( demoDaysLeft == 0 )
{
HWND hTryButton = GetDlgItem( hDlg, IDC_TRYDEMO_BUTTON );
EnableWindow( hTryButton, FALSE );
}
}
ShowWindow( hDlg, SW_SHOW );
HWND hUserEdit = GetDlgItem( hDlg, IDC_USERID_EDIT );
SetFocus( hUserEdit );
// grab messages while dialog active
BOOL bRet;
MSG msg;
while ( IsWindow( hDlg )
&& ( bRet = GetMessage( &msg, NULL, 0, 0 ) ) != 0 )
{
if ( bRet != -1 )
{
if( ! IsDialogMessage( hDlg, &msg ) )
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
This works great, except for one problem: if the host program is exited while this loop is running, another function in the module is called in another thread. All module windows, including the dialog, are closed/distroyed in an orderly way, and the above loop exits when IsWindow(hDlg) fails. So far, so good, but the host program then crashes. I've tried adding