Link to home
Start Free TrialLog in
Avatar of BradKilmer
BradKilmer

asked on

Multiple App Instance Blocking With Windows User Profiles

I've already seen the solution for blocking multiple app instances under a single Windows profile (i.e. AlreadyRunning := hPrevInst <> 0), but I noticed that doesn't work when the first instance is running under one profile and the second instance is running under another. Can this be done?
Avatar of 2266180
2266180
Flag of United States of America image

it's pretty hard telling what kind of method you are using.
is that hPrefInst  a mutex handle?
if not, then that's your problem.

the following is stripped from one of my projects.
resourcestring
  OnlyOneInstace = 'Only one instance of your-app-name is allowed to run.';
 
var mutex:THandle;
    m_single_id:string='your-app-name-1DBC2182-F7C6-423A-AB04-530B60772263';// this GUID can be changed of course. just hit ctrl+shift+g and delete the brackets
 
initialization
 
  Mutex := CreateMutex(nil, True, pansichar(m_single_id));
  if (Mutex = 0) or (GetLastError <> 0) then
  begin
    showmessage(OnlyOneInstace);
    halt(1);
  end;

Open in new window

Avatar of BradKilmer
BradKilmer

ASKER

I made a small app using the snippet above and ran it under my usual profile. When I tried to launch a second copy, the snippet worked (the message was shown and the app halted). However, when I switched users (leaving the app running under the first profile), I was able to launch a second copy under the second user profile. I need to block this in my target app: my target app is an alarm dialer that interfaces with a SCADA server and uses a voice modem to contact people in the event of an alarm. The alarm dialer is configured using projects and when the second copy of the selected project is launched, it tries to acquire control of the COM port to which the modem is attached (which is the same port already being used by the first instance).

We have an Australian customer that has his system configured in such a way that whenever a user signs in to a remote session, the SCADA software treis to spawn a new copy of our dialer. The real solution to the larger problem would be to sparate the alarm dialer into two apps (with the internal control logic running as a service app and a viewer client running as a normal task bar app without limit to the number of instances). This will require a major rewrite of a 220,000 line app, so I'm just trying to enforce the single instance across profiles as a temporary fix.

I'm currently trying to work around the profile issue by periodically poking the TApplication.Handle plus a time-stamp into a global registry key for the duration of the app run, if and when a second copy tries to launch, the second copy will see that the registry key has recently been poked, attempt to verify whether or not the app with the specified handle is still active, and if so, halt. I'll post my results if they work.

Thanks for the suggestion
BK
ASKER CERTIFIED SOLUTION
Avatar of 2266180
2266180
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did the trick! Thanks!