Solved

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

Posted on 2008-06-19
5
357 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now