home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Win32 Under the API
/
ProgrammingWin32UnderTheApiPatVillani.iso
/
Chapter4
/
4-7
/
Event.c
next >
Wrap
C/C++ Source or Header
|
2000-07-17
|
1KB
|
73 lines
#include <windows.h>
BOOL bCtrlC = FALSE, bCtrlBrk = FALSE;
BOOL CtrlHandler(ULONG CtrlType);
int ConsPuts(char *pszString);
//
// ConsPuts:
// Print a string to StdOut on the console
//
int ConsPuts(char *pszString)
{
DWORD nLen = strlen(pszString);
DWORD nWritten;
HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
BOOL bRet;
bRet = WriteConsole(hOutput, pszString, nLen, &nWritten, NULL);
return (!bRet || (nWritten != nLen)) ? 0 : nLen;
}
//
// CtrlHandler
// Handle ctrl-c and ctrl-break
//
BOOL CtrlHandler(ULONG CtrlType)
{
switch(CtrlType)
{
case CTRL_C_EVENT:
bCtrlC = TRUE;
return TRUE;
case CTRL_BREAK_EVENT:
bCtrlBrk = TRUE;
return TRUE;
default:
return FALSE;
}
}
int main(int argc, char *argv[])
{
ConsPuts("Welcome to the control handler program.\r\n");
// Set the handler
if(!SetConsoleCtrlHandler((PHANDLER_ROUTINE)&CtrlHandler, TRUE))
{
ConsPuts("Couldn't set handler!\r\n");
ExitProcess(1);
}
// Spin waiting for an event
while(!bCtrlC && !bCtrlBrk)
;
// Identify the event an exit
if(bCtrlC)
ConsPuts("You hit ctrl-c.\r\n");
if(bCtrlBrk)
ConsPuts("You hit ctrl-break.\r\n");
return 0;
}