How to handle signal (CTRL + C) in Windows CE

Hi,

I want to handle signal (ctrl+c) in wince 7.But unfortunately signal.h header file is missing in the Wince SDK path.Can you please suggest something??

Regards,
Shantanu K. Pradhan

Did you already checked this website: winapi - How can I handle Ctrl+C in a Windows CE console application? - Stack Overflow?

Hi Samuel,
i have already checked the link you provided but they have given the solution by assuming that we have console.h header file.But i checked in Wince SDK installation folder but it does not have the console.h header file.

Regards,
Shantanu K. Pradhan

One way would be to use IOCTL_CONSOLE_SETCONTROLCEVENT. As the console.h file is missing from the SDK, you could manually add the missing defines:

#include <winioctl.h>

#define IOCTL_CONSOLE_SETCONTROLCEVENT    CTL_CODE(FILE_DEVICE_CONSOLE, 10, METHOD_BUFFERED, FILE_ANY_ACCESS)

After that you should be able to call the DeviceIoCTL as described on MSDN. This only applies on consoles. Not sure if this does actually what you want.

Hi Samuel,
Thanks for your response.This is what i tested/implemented as you told and its working for me…

////#include "stdafx.h"
//

#include  
#include 
#include 
#include  

#define IOCTL_CONSOLE_SETCONTROLCEVENT    CTL_CODE(FILE_DEVICE_CONSOLE, 10, METHOD_BUFFERED, FILE_ANY_ACCESS)
HANDLE ghWriteEvent; 
HANDLE ghThreads;
void CreateEventsAndThreads(void) ;
DWORD WINAPI ThreadProc(LPVOID lpParam) ;
int FLAG=1;
int main( void )
{
//    DWORD dwWaitResult;
	
	BOOL bSuccess;
	DWORD dwRead = 0;
	DWORD dwActualOut=0;
	CreateEventsAndThreads();
	bSuccess = DeviceIoControl(_fileno(stdout), IOCTL_CONSOLE_SETCONTROLCEVENT, &ghWriteEvent, sizeof(HANDLE), &dwRead, sizeof(DWORD), & dwActualOut, NULL);
	if(bSuccess==0)
	{
		printf("Device controll failed!!!!\n");
	}
	while(FLAG)
	{
		Sleep(1000);
		printf("Hello i'm in while loop\n");
	}
	
}
void CreateEventsAndThreads(void) 
{
//int i; 
DWORD dwThreadID; 
//ghWriteEvent = CreateEvent( 
//        NULL,               // default security attributes
//        TRUE,               // manual-reset event
//        FALSE,              // initial state is nonsignaled
//        TEXT("WriteEvent")  // object name
//        ); 
      ghWriteEvent=CreateEvent( NULL, TRUE, FALSE, NULL );
if (ghWriteEvent == NULL) 
    { 
        printf("CreateEvent failed (%d)\n", GetLastError());
        return;
    }

 ghThreads = CreateThread(
            NULL,              // default security
            0,                 // default stack size
            ThreadProc,        // name of the thread function
            NULL,              // no thread parameters
            0,                 // default startup flags
            &dwThreadID); 

        if (ghThreads == NULL) 
        {
            printf("CreateThread failed (%d)\n", GetLastError());
            return;
        }

}

DWORD WINAPI ThreadProc(LPVOID lpParam) 
{
   DWORD dwWaitResult;
    printf("Thread %d waiting for write event...\n", GetCurrentThreadId());
	dwWaitResult = WaitForSingleObject( 
        ghWriteEvent, // event handle
        INFINITE);    // indefinite wait
	FLAG = 0;
	printf("Exit\n");
}

Thanks again…
Shantanu K. Pradhan