Jump to content
Sign in to follow this  
potitpanda

How to close an instance of Application before Uninstall

Recommended Posts

potitpanda

I have added my application in "Add remove" tab of Windows

Before remove IT, I Want to check if an instance is running and close the running instance before removing regkeys and autokill script.

I Launch My app while "MyApp /uninstall"

Normally my App is launched in HKLM\Software\Microsoft\Windows\CurrentVersion\Run "myapp.exe"

Myapp and Myapp /uninstall is the same application with command line in order to uninstall it

Thanks for replies.

Share this post


Link to post
Share on other sites
monoceres

I have added my application in "Add remove" tab of Windows

Before remove IT, I Want to check if an instance is running and close the running instance before removing regkeys and autokill script.

I Launch My app while "MyApp /uninstall"

Normally my App is launched in HKLM\Software\Microsoft\Windows\CurrentVersion\Run "myapp.exe"

Myapp and Myapp /uninstall is the same application with command line in order to uninstall it

Thanks for replies.

Check out _Singleton() :)


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
MikeP

Simple functions : ProcessList , ProcessExists, ProcessClose

For more detailed results, you might wanna check that UDF from PsaltyDS : _ProcessListProperties()

Share this post


Link to post
Share on other sites
MilesAhead

Many times trying to do things all in one .exe/app causes complications.

I use Inno Setup which has an UninstallRun section that makes it convenient

to run an app before uninstalling. I include a generic app killer in the install

that takes a command line param for the program to kill:

#include <RefreshSystemTray.au3>

If Not $CmdLine[0] Then

Exit

EndIf

If ProcessExists($CmdLine[1]) Then

ProcessClose($CmdLine[1])

_RefreshSystemTray()

EndIf

if your app has a common name then you might need to get more specific but

chances are if it's a Tray Icon type app and the user is uninstalling, then they

probably forgot it's running in the tray. So I kill it, then use the RefreshSystemTray()

to clear off the icon, before the uninstall runs. It compiles to around 250KB which

is pretty economical these days. You might include a note in your Readme file

so that people don't think it's something sinister you're sneaking in on them

if they notice it in the install folder. :)

Share this post


Link to post
Share on other sites
monoceres

I made a fully working example how a single script can work as installer / uninstaller and also main app.

It also closes other instances:

#include <WINApi.au3>
If $CmdLine[0] > 0 Then
    If $CmdLine[1] = "/install" Then
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp", "DisplayName", "REG_SZ", "MyApp")
        RegWrite("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp", "UninstallString", "REG_SZ", @ScriptFullPath & " /uninstall")
    ElseIf $CmdLine[1] = "/uninstall" Then
        ; Check for running apps
        $pid = _WinAPI_GetCurrentProcessID()
        $plist = ProcessList(@ScriptName)
        For $i = 1 To UBound($plist, 1) - 1
            If $plist[$i][1] <> $pid Then
                ProcessClose($plist[$i][1])
            EndIf
        Next
        ; Here you can do all uninstalling stuff
        RegDelete("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\MyApp")
    EndIf
Else
    ; Main app
    While 1
        ; fooling around...
        Sleep(100)
    WEnd
EndIf

Edit: This will of course work best if it's compiled.

:)

Edited by monoceres

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
ProgAndy

_WinAPI_GetCurrentProcessID() could alos be replaced with @AutoitPID :)


*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Share this post


Link to post
Share on other sites
monoceres

_WinAPI_GetCurrentProcessID() could alos be replaced with @AutoitPID :)

...you learn something everyday.

Wonder why there is a _WinAPI_GetCurrentProcessID() function though...


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
weaponx

...you learn something everyday.

Wonder why there is a _WinAPI_GetCurrentProcessID() function though...

Thats probably where @AutoItPid is generated from internally.

Edited by weaponx

Share this post


Link to post
Share on other sites
MilesAhead

I made a fully working example how a single script can work as installer / uninstaller and also main app.

It also closes other instances:

How do you delete the exe from the HD while it's running?

Share this post


Link to post
Share on other sites
weaponx

How do you delete the exe from the HD while it's running?

You don't. You call ProcessClose() first.

Share this post


Link to post
Share on other sites
MilesAhead

You don't. You call ProcessClose() first.

I don't get the point of closing a process using the same process if you're still left

with the self-deleting problem. That's not uninstalling. Just settings cleanup.

Share this post


Link to post
Share on other sites
AdmiralAlkex

I don't get the point of closing a process using the same process if you're still left

with the self-deleting problem. That's not uninstalling. Just settings cleanup.

You cant delete a file that is running/opened, check Q14 in the FAQ for one possible way of self-deleting

Share this post


Link to post
Share on other sites
MilesAhead

You cant delete a file that is running/opened, check Q14 in the FAQ for one possible way of self-deleting

Ok I think I see what the OP is getting at now. His app is it's own

uninstaller because he registers with the install services(Add/Remove)

and that deletes the .exe when he's done with the cleanup?

Another way to skin the cat. :)

Main disadvantage is carrying uninstall logic into ram

every time your program loads but if it's a small utility

it prolly doesn't matter.

I like the KillApp.exe approach for the general case since

I don't have to reinvent the wheel for every app.. plus

the workhorse app doesn't have to be written in AutoIt.

I think that's the first time I heard of somebody registering

the main application as the uninstaller though. Novel idea.

Share this post


Link to post
Share on other sites
monoceres

Ok I think I see what the OP is getting at now. His app is it's own

uninstaller because he registers with the install services(Add/Remove)

and that deletes the .exe when he's done with the cleanup?

Another way to skin the cat. :)

Main disadvantage is carrying uninstall logic into ram

every time your program loads but if it's a small utility

it prolly doesn't matter.

I like the KillApp.exe approach for the general case since

I don't have to reinvent the wheel for every app.. plus

the workhorse app doesn't have to be written in AutoIt.

I think that's the first time I heard of somebody registering

the main application as the uninstaller though. Novel idea.

Actually this isn't new, if you look at the bt client uTorrent you will see that it's just a single excutable, main app, installer and uninstaller everything is in the exe. which is pretty amazing since it's only 214 kB big.

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites
MilesAhead

Actually this isn't new, if you look at the bt client uTorrent you will see that it's just a single excutable, main app, installer and uninstaller everything is in the exe. which is pretty amazing since it's only 214 kB big.

Yeah, I wrote a program that managed a bunch of shell extensions. It had a button to uninstall that unregistered all the dlls and removed registry settings for itself, and closed itself. But I didn't think of deleting the files and registering with the install service. In that particular case it may not have worked though. The shell can be a pain letting go of extensions. Usually just a log out and log back in will free them up so you can delete, but it's a fly in the ointment for otherwise smooth uninstalls. For some reason Inno Setup seems to do a better job of deleting registered dll servers it installs, as compared to unregistering and deleting manually. Windows has so many "undocumented" gizmos anyway that it's tough to know what some programs are doing. I had a shell extension utility that I first wrote in 2001 that used the Context Menu to do special copy on selected files. I just found out a few months ago how to drag & drop copy with my extension instead of using the Browse For Folder dialog! "Undocumented APIs" must be in the list of PC Oxymorons. :)

Share this post


Link to post
Share on other sites
potitpanda

Is not working...

How can I get All process Name and All Process PIDS on a single GUI window....

Thanks

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×