Solved

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

Posted on 2008-06-19
5
393 Views
Last Modified: 2008-06-19
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 .
0
Comment
Question by:iportu
  • 2
  • 2
5 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 21821925
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
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 21822299
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.
0
 

Author Comment

by:iportu
ID: 21822483
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?  
0
 
LVL 86

Accepted Solution

by:
jkr earned 250 total points
ID: 21823147
What about explicitly exiting your app when a shutdown occurs? E.g.
BOOL WINAPI MyCtrlHandler(
  DWORD dwCtrlType   //  control signal type
) {
 
  switch(dwCtrlType) {
 
    case CTRL_LOGOFF_EVENT:
    case CTRL_SHUTDOWN_EVENT:
 
      ExitProcess(0);
 
    break;
  }
 
  return TRUE;
}
 
 
//...
 
SetConsoleCtrlHandler(MyCtrlHandler,TRUE);

Open in new window

0
 

Author Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question