Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2008-06-19
5
Medium Priority
?
409 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Industry Leaders: 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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

670 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