How to prevent multiple instances of a non-LC Windows app?
livfoss at mac.com
Fri May 20 08:34:45 EDT 2011
Thanks to all who replied to this and my other queries about "tasklist" on Windows (not available on the basic version of XP that most of my users are running). Basically there does seem to be a bug in the LC 'launch' command, and I will report it. Meanwhile I am trying to follow Mike Bonner's advice as shown in the quote below. As I didn't want to use a separate file, I put this (VB) script into a field called "VBScript" in a test stack:
Set WshShell = WScript.CreateObject ("WScript.Shell")
Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from Win32_Process")
For Each objProcess in colProcessList
If objProcess.name = firefox.exe" then
vFound = True
If vFound = True then
Following Mike, I'm just using FireFox as a test program - in the end I will use the particular program I'm interested in.
I then run this LC script from a button of my program which has been compiled as a Windows standalone:
put "VBScript" into scriptName
do fld "VBScript" as scriptName
answer "Result is:"&&(the result)
(BTW I found that using the word 'VBScript' directly results in a compile error, even tho VBScript is in the list of alternateLanguages - dunno why).
Anyway the result is 'execution error'.
As I know zilch about VB I am stuck - can anyone suggest what I should have done? Do I **have** to use 'shell' - I am reluctant because it takes me more into unknown regions.
Finally, I should say that I do have a copy of FireFox on the PC, although I find that Windows Explorer doesn't show the '.exe' at the end of its name. So a subsidiary question is, how can I know exactly what Windows thinks the entire name of my particular executable program is, particularly if there's a space in the name (eg. "My Program" - does that get called as "My Program.exe" or what?)
On Wed, 18 May 2011 15:39:13 -0600, Mike Bonner <bonnmike at gmail.com> wrote:
> I can't test with win xp, but you might put the following into a .vbs file.
> *Set WshShell = WScript.CreateObject ("WScript.Shell")*
> *Set colProcessList = GetObject("Winmgmts:").ExecQuery ("Select * from
> *For Each objProcess in colProcessList*
> *If objProcess.name = "firefox.exe" then*
> *vFound = True*
> *End if*
> *If vFound = True then*
> *WshShell.AppActivate "Firefox"*
> *WshShell.Run "Firefox.exe"*
> *End If *
> In my example, it will check for firefox, if its running it will focus on
> it, if not it will start a new instance. To use it, do a shell call.
> get shell("myScript.vbs")
> I'm sure it could be easily modified so you could pass it the program to
> find as a parameter, but if its always the same hard coded should be fine.
> Got the script from here.
> http://www.tek-tips.com/viewthread.cfm?qid=1492889&page=6 and changed it
> Hope it works on xp home, works fine on 7.
More information about the use-livecode