Link to home
Start Free TrialLog in
Avatar of iportu
iportuFlag for United Kingdom of Great Britain and Northern Ireland

asked on

ShellExecute "open" on exception does not allow a force restart

I am writing a program which on an Unhandledexception, I want it to re-open itself again. For this I have created an exception filter:

long ProgExceptionFilter(EXCEPTION_POINTERS* p)
{
    if(p->ExceptionRecord->ExceptionCode==EXCEPTION_ACCESS_VIOLATION
    {
        TCHAR(acModule[MAX_PATH];
        GetModuleFileName(NULL, acModule, MAX_PATH);
        ShellExecute(NULL, _T("open"), acModule, NULL, NULL, SW_SHOW);
        return EXCEPTION_EXECUTE_HANDLER;
    }
    return EXCEPTION_CONTINUE_SEARCH;
}

This works but the problem is that the "restart" I had scheduled for 00:00 every day using (shutdown -r -f) no longer works because it cannot force the application with the above code to close.

Do yo have any ideas to overcome this problem?

Thank you .
Avatar of sunnycoder
sunnycoder
Flag of India image

I am not too familiar with WinAPI but I am sure they must be having something like an alarm() ... When your application starts up, it can get current time using gettimeofday(), calculate the time left till midnight and set up an alarm for 00:00 hours.
The signal handler for this alarm can make your application exit gracefully
Previous approach would tie up your program to a specific time ... In case you need to restart at 2:00 AM instead, you would have to edit the code and recompile
Probably a better approach would be to define a signal handler (say for SIGINT) and send that signal to your process before restart.
Avatar of iportu

ASKER

Sunnycoder. Yes, I had not thought about that. I thikn that to tidy things up too, the windows restart should happen within the program too - I'm checking whether that would work. Otherwise it would be a bit messy to close the program at a certain time and then set a windows routine for a certain time +5 minutes. It would get me what I want as you say but I think there must be a neater way?  
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of iportu

ASKER

sunnycoder, thank you very much for your input. Jkr's solution worked very nicely though. It looks easy I suppose if you know it.