SelfDeleting application in VC++? - visual-c++

I want to delete a folder which contains the currently running application. How can i do it..? is there any way of doing it ? i.e the folder which contains the application should delete after the application has finished running ?

Your best bet is probably to use the Win32 API MoveFileEx. It has a flag that can be set for deleting files when they are in use on the next reboot called MOVEFILE_DELAY_UNTIL_REBOOT. Set the new filename parameter of MoveFileEx to NULL to perform this type of delete.
If dwFlags specifies
MOVEFILE_DELAY_UNTIL_REBOOT and
lpNewFileName is NULL, MoveFileEx
registers the lpExistingFileName file
to be deleted when the system
restarts.
Note: Normal files that are in use can be deleted normally using the Win32 API DeleteFile depending on if they were opened (Using the Win32 API CreateFile) with FILE_SHARE_DELETE permission. I don't think running programs by default on Windows have that permission though. When a file is specified to be deleted that is in use but that was opened with this flag, then the file will be removed when the last file handle is closed.

This is hairy. I had to implement this once for a self-patching app, where the patcher had to (by client request) delete itself after installing the patch. You can do this by launching a helper DLL which deletes your process, along with itself.
The full method for deleting your process can be found here: http://www.handcraftedbytes.com/articles/writing-install-and-uninstall
As others have pointed out, you're not going to be able to delete the folder that your executable resides in while it exists there. My suggestion is to:
Use MoveFileEx to move your executable off to a temporary directory,
delete your application's directory,
delete your executable using the self-deleting DLL method described in the link above.

You cannot delete an executable file that is currently running, however you can delete a batch file that is currently running (cmd.exe loads the whole file into memory and then you can delete it).
So the simplest solution would be to launch a batch that tries to delete the .exe in a loop (because it may not work the first time - until your .exe has been unloaded) and then exit your process - with the batch file still running.

Related

Linux: which standard directory has write access by default?

my application is required update its program files when it detects a new version on the server. The update mechanism is performed by the application itself, and it replaces old code with new code in the same directory. Which directory in linux is best suited for this? If there is a standard directory which has write access by default(analogous to %AppData%\Local for windows), I would like to know which one it is.
I have tried using /var/<my-app-name-here> and /opt/<my-app-name-here>, but none of these directories have write access by default. The only way I can use them update files in them is if the user uses sudo, so i would just like to avoid making those problems by just installing to a directory with write access to begin with.
I am using a tool called jpackage to make the .deb file which install the application.

How to hard remove uninst.exe that is generated by NSIS?

I am using NSIS to write an installer for my windows application. After installing the application, an uninst.exe is also generated in my program directory. Later on I need to uninstall my program but I failed to do that in control panel. Therefore I went to the file system and tried to delete the directory. Everything other than that uninst.exe was removed. I've tried changing permission of this file and other methods but it doesn't work.
WriteUninstaller does not set file permissions or any other attribute that might prevent you from deleting it. It sounds like the file might still be in use by something on your machine.
Things to try:
Use Task Manager or Process Explorer to see if there is a uninst.exe process still running.
Use the find handle feature in Process Explorer to find any open handles to the file.
Use Process Monitor to get detailed information about the failed delete operation.
Check %LOCAL­APP­DATA%\Virtual­Store to make sure UAC Virtualization is not tricking you with "ghost" files.
Disable your Anti-Virus.
Reboot the machine and try to delete the file again.

Renaming executable's image name is giving it write permission

Dear community members,
We have three of same hardware Windows 7 Professional computers. No one of them is connected to a domain or directory service etc.
We run same executable image on all three computers. In one of them, I had to rename it. Because, with my application's original filename, it has no write access to it's working directory.
I setup full access permisions to USER group in working directory manually but this did not solve.
I suspect some kind of deny mechanism in Windows based on executable's name.
I searched the registry for executable's name but I did not find something relevant or meaningfull.
This situation occured after lot of crashes and updates of my program on that computer (I am a developer). One day, it suddenly started not to open files. I did not touch registry or did not change something other on OS.
My executable's name is karbon_tart.exe
When it start, it calls CreateFile (open mode if exist or create mode if not exist) to open karbon_tart.log file and karbon_tart.ini file.
With the files are exist and without the file exists, I tried two times and none of them, the program can open the files.
But if I just rename the name to karbon_tart_a.exe, program can open files no matter if they are exist or not.
Thank you for your interest
Regards
Ömür Ölmez.
I figured out at the end.
It is because of an old copy of my application in Virtual Store.

How to update DLL in approot/bin

I currently have an Azure instance running. The problem is that Publishing to staging is taking 15+min each time.
I was hoping to make small changes to my code and update the corresponding DLL in the VM directly myself via remote desktop. However, when I try to copy paste the DLL into the approot/bin folder, I get an error that the file is currently in use.
I have already stopped the IIS instance in the IIS manager. What other service/process do I have to stop in order to update the file?
Thanks
To answer your question after you RDP your instance, you can kill the role host process WaWebHost.exe or WaWorkerHost.exe which will release the DLL holdout and you can copy the DLL.
You have a very small window to copy your DLL to bin folder once you kill the host process so have it ready to copy. May be open a command window and write your command to copy DLL from Source to BIN, and then kill the host process (WaWebHost.exe/WaIISHost.exe/WaWorkerhost.exe) and then launch the command to copy DLL.
I also think you know that DLL will not persist so finally you would need to update package with proper dll as usual process.

Getting the Windows CE uninstaller to work properly

On some of the devices that I am working on, the \Windows directory is not on permanent storage. That is, once the device is rebooted, whatever was written to \Windows is lost. This is particularly problematic for uninstalling programs since wceload.exe (the Windows CE CAB installer program) generates a .unload file and places it in \Windows. The application can be uninstalled before the device is rebooted, but afterward it can't (the "Remove Programs" tool in the control panel comes back with an error about not being able to open the unload file). So how can I get the .unload file to persist across reboots? Is there any way to control where the uninstaller (I believe it is called unload.exe) looks for the .unload file?
I haven't been able to find any good info on this still. It looks like the path to the unload file is hardcoded so the best I can do is make a backup of the unload file. If the user wishes to uninstall they will have to manually copy the file to the \windows directory and then use the uninstall tool in the control panel. Here is more info:
http://www.generation-nt.com/us/answer/wince5-force-wceload-install-unload-into-nonvolatile-memory-help-87676002.html
You can probably keep the .unload file across reboots by copying it to a persistent directory after installing. This directory may vary per device, e.g. \Hard Disk\ramroot\Windows or \Backup\Windows. It will then automatically be copied to the \Windows on reboot. (Although I am not sure whether such a directory and behavior exist for every device.)
To copy this file you need to execute a copy-statement from a custom setup-dll after the install completes. This codeproject-site has a very comprehensive explanation, with screenshots and code.
Simply re-Install application and then un-install from remove programs in control panel.

Resources