Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

I have an mfc - application: foo.exe.

Now i want this foo.exe to create a process of itself and kill itself.

(I simply want to create a new process of itself and kill the old one.)

I tried this like that:

CreateProcess("path of foo.exe", NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, "directory of the foo.exe", &startup, &info);

PostQuitMessage(0);


But: This does not work, it comes up a message box.... if i click ok there, it works. (in german, the text of the messagebox is "server ausgelastet" which means that the server is busy. (this is an ole message)

Does anyone has a solution to this problem?

Thx alot!
Posted
Comments
nv3 28-Apr-15 11:27am    
Could you provide the following information in order to help:
1. What is the return code of the CreateProcess call?
2. Put a breakpoint on the PostQuitMessage call and see, if the new process is created correctly before trying to terminate the old process.
3. Which process is the owner of the message box? (You can check with Spy++, which is a tool that is part of Visual Studio)

Could it be that this message box is generated as part of your MFC application. Some application check on startup whether they are already running in another instance. Perhaps that is where the message box comes from.
[no name] 28-Apr-15 19:59pm    
This may be what you need: http://www.codeproject.com/Articles/15184/How-To-Create-a-Self-Restartable-Application

1 solution

The trouble is: PostQuiteMessage does not work in all situations. It posts WM_QUITE, so you should assume that all conditions work the way that it would lead to application termination eventually: the message loop is being still executed, messages are being processed, and so on. Please see: https://msdn.microsoft.com/en-us/library/windows/desktop/ms644945%28v=vs.85%29.aspx[^].

This is probably not the case. As you did not provide any information on the state of your process, I have no idea what is it. It could be anything, really.

The hard way of terminating the process (yes, you can terminate your current process) is this:
https://msdn.microsoft.com/en-us/library/windows/desktop/ms683179%28v=vs.85%29.aspx[^],
https://msdn.microsoft.com/en-us/library/windows/desktop/ms686714%28v=vs.85%29.aspx[^].

Better avoid doing it by all means.

Many would say that the idea to kill the current process in this way is abusive. And I will agree with this opinion. I will only add that the whole idea to terminate the current process (even nicely) and start another instance of the same application is itself a pretty strong indication of technology abuse. If you need more help, you will need to explain some motivation of what you are doing, and your ultimate goals. Chances are, you will be suggested more reasonable alternative.

—SA
 
Share this answer
 
Comments
Member 10781325 29-Apr-15 3:26am    
Thx for your help! I found the solution by myself. The problem was not that postquitmessage(0) did not work. I had to change a flag in the creation of the new process:

BOOL bCreateProcessSuccess = CreateProcess(buffer, param, NULL, NULL, FALSE, DETACHED_PROCESS, NULL, fileFind.GetRoot().GetBuffer(0), &startup, &info);

WaitForSingleObjectEx(info.hProcess, 2000, FALSE);

Btw: i am doing this, because our application has memory-leaks. It is a big beast and finding this leaks is a biiig task. By just restarting the process, now everything works. (i know not a very good solution:-))
Sergey Alexandrovich Kryukov 29-Apr-15 9:02am    
All right, but you need to prevent memory leaks instead of doing it all. :-)
You are very welcome. Good luck, call again.
—SA
Sergey Alexandrovich Kryukov 29-Apr-15 9:02am    
...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900