Wait until reboot to delete

Windows specific forum
User avatar
RichAlgeni
Addict
Addict
Posts: 914
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Wait until reboot to delete

Post by RichAlgeni »

I was searching for a way for a program to delete itself, for uninstall processes. There were some examples of batch files doing this, but wasn't interested in this. I found a way to get the system to delete the program on reboot. Note that administrator mode is required.

test_delete_delay_until_reboot.pb

Code: Select all

EnableExplicit

Define result.l
Define errNumber.l
Define infoText.s
Define progName.s = ProgramFilename()
Define infoText.s = "This process '" + progName + "' will be deleted during the next reboot."

Import "C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\um\x64\kernel32.lib"
   MoveFileWithProgressW(oldFileName.s, newFileName.s, *progressRoutine, *lpData, dwFlags.l)
EndImport

result = MoveFileWithProgressW(progName, #Null$, #Null, #Null, #MOVEFILE_DELAY_UNTIL_REBOOT)
If result < 1
    errNumber = GetLastError_()
    infoText  = "result = " + Str(result) + ", " + Str(errNumber)
Else
    infoText  = "This process '" + progName + "' will be deleted during the next reboot."
EndIf

MessageRequester("Testing", infoText)

; IDE Options = PureBasic 5.73 LTS (Windows - x64)
; CursorPosition = 17
; EnableXP
; EnableAdmin
; Executable = test_delete_delay_until_reboot.exe
; DisableDebugger
; HideErrorLog
; EnableCompileCount = 6
; EnableBuildCount = 5
; EnableExeConstant