Qt Run an External Executable and Stop Running the Current - c++

I'm trying to make an updater for an Application I've been developing for a few weeks. So lets say I have my app, which is "App.exe" and I download the updater from my web server, which is "Update.exe".
So in "App.exe", I will start downloading "Update.exe" from my web server. Once it is finished, I need to execute "Update.exe" from "App.exe" and then have "App.exe" close itself so that "Update.exe" can replace the old "App.exe" with the new one. Once it is replaced, "Update.exe" will need to run the new "App.exe" and then close itself.
Now, I'm not even sure if it's possible to fully close an app while leaving the externally executed app running and being able to delete the original executable, but it seems like one of the easiest methods to creating an application updater.
If this is possible, can someone show me an example or give me a link to an app opening another and closing itself leaving the other running? If not, is there a way similar to this that could create a basic updater?

Related

Windows store app automating testing, run and detect when the application is stopped (suspended) c++

Im using this guide "automating the testing of windows 8 apps" to test my windows store app
http://blogs.msdn.com/b/windowsappdev/archive/2012/09/04/automating-the-testing-of-windows-8-apps.aspx
Unfortunaltely Ive run into a problem, I need to know when my app closes (crashes) or when it goes into suspended mode, to log that info, and I want the launcher to be able to know the application has stopped , unfortunately iApplicationActionManager, does not have that method. And IPackageDebugSettings which is used to change the application suspend and resume states does not have a readable current state property (afaik)
Is there any way to do this?

Closing process from service without killing the processes

I am searching for a way to close a process running under any user by a windows service running under the system account. I've nearly tried everything but I could not find any solution except killing the process.
Why can't I kill the process?
I can't kill the process uses a tray icon. If I kill the process, the tray icon won't disappear.
What I've tried so far
I've already tried to use global eventhandles (did not work because the child process got extremely laggy).
I also tried to use PostMessage/SendMessage to communicate with the process. That solution did not work because a windows service can not interact with any user interfaces,...
I found another question (here on stackoverflow) which describes exactly my problem: Close a child process from a windows service.
This question does not contain a nice solution.
First of all I don't use C++ instead of C#. The next problem is, that the child process uses a mouse hook. So it has so run very fast without many overhead. Otherwise it would get laggy which would mean, that the mouse would lag on the whole system.
So is there really no simple solution to close a process from a windows service?
A service can use SetProcessWindowStation() and SetThreadDesktop() to attach to the interactive desktop of the target process before then sending messages to the target process.
Alternatively, the service can use CreateProcessAsUser() to run a new process in the same session as the target process, and then that process can send messages to the target process.

How to Communicate With a Program Through Console / C++

When I run an application through a console, for example, $application start, how can I communicate with said application? So I can for example do $application load --/home/application/files/file.txt --warn=0 --notice=0 and that running instance of the application would react to this...
I do not want to keep listening to the console on the application side. I want to be able to close the console, reopen it, and still interact with the program.
The reason why I am doing this is because I want a master program that loads in different operations which it performs in the background. I want to be able to add operators, and remove operations.
Myself I have some experience with PHP and I know Apache has such behavior.
EDIT: After some comments of you guys, I concluded that I am required to use IPC. I have heard of this before but I never really understood how it works. After some Googling and the WikiPedia links you showed me I concluded that there are a sh-t tun of ways of handling IPC. I want to send packages of data to the main process, which one would be the best in my case? My personal favorite atm is a message queue but that only seems to work within the same process.
to be able to run application in background and have ability to close console where it was started, you may use nohup utility. then first instance of you app should create some ("well known") IPC resource (message queue, FIFO, whatever), so further instances will communicate over it with the first instance.
and it will be relatively easy, then to turn you app into a full functional daemon.
Since #LokiAstari pointed out, that you may don't have much experience with C++, I would recommend you to read: How to parse command line parameters.
I would then use a temporary file in /tmp to communicate with the main program, which run an infinite loop, waiting for modifications to the temporary file.
Personally I would do this in multiple stages.
As otherwise you are going to be trying to solve to many different problems at once.
What you are doing is writing a service (a long running application). Communication with the service by running a command usually involves running a different application that talks to the service (in apaches case the apache command starts the httpd service. Then subsequent commands talk to the httpd service).
But to get this up and running it is easier to go through a few steps first.
Write an application that on startup read commands from a directory
: So on startup you have a command directory.
: You open each file (in order execute the file if it is valid) then re-name the file to show it was done.
Modify your application to run as a continious loop.
All the loop does is look for events in job queue.
: if it sees them execute the job.
: If no job there then sleep for 10 seconds.
: On startup you just inject one job
-> : It reads the command directory and creates a job for each file.
-> : The file job executes the file then renames the file to show it is done.
Modify your service to use threading.
: Run the event loop in one thread.
: Use locks and semaphores so that added items to the queue is thread safe.
: When the application starts up you start the event loop (making sure it started then inject the job (as in 2). Then just waits for the event loop to finish (it will not).
Add a timer thread that fires very ten seconds to check the command directory
: All the timer should do is create a job and put it in the event queue.
: Now you don't need to inject a job at startup its the timers job.
Once you have all the above running you are ready to introduce a listener that will listen on a socket for indirect commands from another application.
: Doing all the above in one go to run the service is going to be long an error prone for a beginner. I advice you to fallow through all the steps above to get to this state then ask another question about how to do IPC.
: So add a new thread that listens on a socket (OK this is not the best technique but this is bootstrapping a beginner). When it receives input it creates a file in the command directory then places a job in the job queue.
You should now be able to test your command using the command line curl (or wget command) to send files to your service.
Once you have it working with curl.
You can write a standalone application that converts command line arguments into command files and sends them to your service.
Convert your application from using files to having all the information in the job object.
Thats it.

Windows 7 UAC elevation

I have a single thread that I'd like to run as an administrator in my application. The rest of the application I can happily run as the default user level (asInvoker). Is this possible? I notice there is an "ImpersonateLoggedOnUser" function. Can I somehow use this to log the administrator on and then get the thread to impersonate that person?
It seems as though this ought to be something pretty trivial to do ... but there doesn't appear to be any obvious way to do it. Can anyone help me out?
Edit: So if I have to fire off a seperate process is there any way I can CreateProcess a new process and have it launch from a specific entry point. I can, of course use command line processing to do it, but i'd really rather I could stop the user from entering the command line and starting an unclosable process!
No, elevation is per process, not thread.
If the rest of the application has to run non-elevated, you could run yourself elevated with some parameter (myapp.exe /uac "ipcparamhere") and use some sort of Inter-process communication to communicate back to the "main instance" of your app. (If the elevated process only performs a simple operation, you could probably check for success by using the exit code of the process)
This is not possible. You'll need to gain admin privileges by including a manifest in the app. Google "requireAdministrator" to find the manifest you'll need. Your user will probably quickly tire of doing this over and over again, your best bet is to spin-off the task that requires these privileges into a separate process. A service for example.
You can launch a separate exe and have a manifest on it saying it requires administrator. Then be sure to launch it with shell execute, which uses manifests, and you're all set. As a thoughtful touch, put a UAC shield on the button that kicks off that thread. (Send it a BCM_SETSHIELD message if you're doing this all by hand.)
You can decided whether you want a visible window or not on the separate process. Meanwhile the user can still drag and drop into the main app.

Cannot recreate named pipe under vista

In my application i have the app and a service that does stuff for the app. They communicate via a named pipe. Now i can start the app and it starts the service and connects and this works well.
When the application downloads an update, it restarts it self (to unload in use modules) killing the service and then starts the service again to install the update. This is working fine on Windows XP and 7 however Vista keeps returning Access denied when trying to recreate the pipe.
I create the pipe with the flag FILE_FLAG_FIRST_PIPE_INSTANCE as to only have one pipe. I restart the app using CreateProcess and the new instance waits for the old instance to stop.
If i completely kill the application and restart it, it can install the update and the start up normally.
This is driving me insane, any ideas?
What kind of security attributes are you passing in to the CreateFile call that opens the handle to the pipe? Are you allowing handles to be inherited when you call CreateProcess?