I've got a Visual Basic App that tends to get severely messed up if the installation runs more than once. It seems the occasionally client mistakes the installer for the shortcut to it later on down the road, runs the installer again and it messes everything up. I can't for the life of me figure out why so I decided the easiest way would be to make it so the exe could only be run once on a machine otherwise it would just end. Any ideas?
Have your installer place a file in the applications folder.
When runs again, check for that file, if it exists, display a "Already installed" popup and exit.
You could have the installer EXE file delete itself, well not directly while it's running but pass off a call to another service to delete it after it's done running.
I thought this was interesting so I Googled it, seems like some good info on this post:
Assuming this is a VB6 question, you can use the built in App.PrevInstance.
Documentation: http://msdn.microsoft.com/en-us/library/aa268085(VS.60).aspx
App.Previnstance returns True if your application is already running.
In your Startup Form's load event or your Sub Main:
Private Sub Form_Load()
If App.PrevInstance = True Then
MsgBox "Already running"
'Do whatever you need to do before closing
End If
End Sub
If you want to go one step further and bring the previous instance to the foreground, you can check out these articles:
http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=21131&lngWId=1
If you are using VB.NET with Visual Studio 2005 or 2008 you can check the 'Make Single Instance Application' option in the Windows Application Framework section of the Application tab in the project settings.
Maybe checking the running processes on the machine to tell you if another instance exists would help? See this thread for more info...
If you are using .net then Mutex's are your friend here.
Never, ever use the Process.GetProcessesByName method. You'll only hate yourself later for using something that requires Admin priviliges
private bool CanIStart
{
try
{
MyAppMutex= new Mutex(false, "myAppMutex", out createdNew);
if(MyAppMutex.WaitOne(0,false))
{
return true;
}
else
{
MyAppMutex = null;
return false;
}
}
catch(ApplicationException ex)
{
// we couldn't create the mutex. // log the error if you care
return false;
}
}
Have the installer create a registry entry. Refuse to install (again) if the registry entry already exists.
Exactly how to achieve this will depend on the installer technology that you are using.
Why don't you FIX the installer or whatever problems are happening rather than try to make some hack to avoid it...
Just my $.02
On the installer app
' Test eventual mark, settings in the registry.
if GetSetting("MyInstallerApp","Startup","BeenHere",0) = 1 then
MsgBox "This installer was ran once already... first run the un-installer."
End ' or some other code to properly exit the installer
EndIf
Call SaveSetting ("MyInstallerApp","Startup", "BeenHere", 1) 'leave a mark for future
On the uninstaller app (or the "uninstall" option of the installer)
' Allow future Installer to run again
Call DeleteSetting("MyInstallerApp", "Startup")
It strikes me that leaving an application around that shouldn't be run more than once is like leaving a big red button somewhere in someone's desk, that when pushed blows up the desk. Not cool.
Most installers have a feature to not offer repeat installation. Check that first - that seems like the best, most obvious solution.
What is severely messed up? How is running the installer a second time different from running it the first from your application's point of view? That should be addressed in your code as well.
You could check and see if the installed application files already exist. Assuming that is, you know where the application was installed to.