Solved

Multiple App Instance Blocking With Windows User Profiles

Posted on 2007-12-04
4
146 Views
Last Modified: 2010-04-21
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?
0
Comment
Question by:BradKilmer
  • 2
  • 2
4 Comments
 
LVL 28

Expert Comment

by:ciuly
ID: 20409727
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

0
 

Author Comment

by:BradKilmer
ID: 20413569
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
0
 
LVL 28

Accepted Solution

by:
ciuly earned 125 total points
ID: 20413712
I looked at
http://msdn2.microsoft.com/en-us/library/ms682411.aspx
and they mention to prefix the mutex name with "Global\". try that since you already have the test applicaiton made ;)

also note the note for win2000, as it will be ignored if terminal services is not running.
0
 

Author Closing Comment

by:BradKilmer
ID: 31412757
That did the trick! Thanks!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now