Archived

This topic is now archived and is closed to further replies.

Udi

VC++ 6.0 question

Recommended Posts

Hi All, I have a problem, which I''m guessing is fairly straightforward.. yet so far I''ve been unable to solve it. Any help would be greatly appreciated: I have a VC++ 6.0 console application that is running. I''m trying to stop it by doing Ctrl-C, in order to get to a special mode that will do certain commands based on characters entered. To return to the normal mode, the user will just press #. I AM succcessful in getting to my ''special'' mode, however, everything behaves strangely (for example, it takes quite a while for the CTRL-C to take place, the system("pause") command is not working at all, and when I click CTRL-C twice the application just enters into an infinite loop). What am I doing wrong? Cheers, Udi

Part of my main function:
void main(int argc, char *argv[], char *envp[]) 
{
  SetConsoleCtrlHandler(CtrlHandler, TRUE);
  ...
}

BOOL WINAPI CtrlHandler(DWORD fdwCtrlType) 
{
  switch (fdwCtrlType) 
  {
    case CTRL_C_EVENT:
    cout<<"CTRL_C_EVENT";
    cout<<endl;system("pause");
    Beep( 750, 300 ); 
    control();
    break;

    case CTRL_CLOSE_EVENT:
    cout<<"CTRL_CLOSE_EVENT";
    cout<<endl;system("pause");
    break;

    case CTRL_BREAK_EVENT:
    cout<<"CTRL_BREAK_EVENT";
    cout<<endl;system("pause");
    break;

    case CTRL_LOGOFF_EVENT:
    cout<<"CTRL_LOGOFF_EVENT";
    cout<<endl;system("pause");
    break;

    case CTRL_SHUTDOWN_EVENT:
    cout<<"CTRL_SHUTDOWN_EVENT";
    cout<<endl;system("pause");
    cout << "Signal to quit was received\n";
    system("pause");

    default:
    return FALSE;
  }
  return TRUE;
}

void control()
{
  char ch='' '';
  //cin.get(ch);

  cin>>ch; // both versions work the same

  while (ch!=''#'')
  {
    cout<<"ch="<<ch<<endl;
    //cin.get(ch);

    cin>>ch;
  }
  cin.clear();
}

Share this post


Link to post
Share on other sites
I have no idea what is wrong with your code. However, you might check out SetConsoleMode(), if all you really care about is CTRL-C, and not any of the other events. You can use SetConsoleMode() to turn of system-handling of special inputs. Which means that you can read the inputs yourself, and handle them as you see fit.

Share this post


Link to post
Share on other sites
Have you checked to see if you''re in ''special mode'' before you
call ctrl+c to get into ''special mode''?

-Hyatus
"da da da"

Share this post


Link to post
Share on other sites