I am trying to write a program which starts itself as a DETACHED_PROCESS when the user hits the close botton in the title bar and following is the code for handling the WM_CLOSE message:

case WM_CLOSE:
     CreateProcess(NULL, szPath, NULL, NULL, TRUE, DETACHED_PROCESS, NULL, NULL,
                   &si, &pi); // szPath contains the path of the same calling process
     return 0;

but the process is getting terminated without creating a new instance of itself. Why?

Also, when I do the same for WM_DESTROY message, the program creates 25 new instances of itself instead of creating just one. Why?

link|improve this question

45% accept rate
Is this a console process? – Cody Gray 47 mins ago
No, its a windows process. Is it possible to handle messages for a console program like we do for windows programs?? – user1232138 43 mins ago
Well, all that the DETACHED_PROCESS flag means is that a console process won't actually be attached to a console. Using that flag doesn't make much sense with a GUI application. – Cody Gray 37 mins ago
1  
I'd assume that this doesn't work with WM_DESTROY as that will be called for all controls too, not the form. You'd need to check the wParam or lParam to check if the main form is being closed. – slugonamission 27 mins ago
feedback

1 Answer

You could try to use (misuse) the Application Recovery and Restart API to register your application to perform what you aim...

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.