26286 total geeks with 3498 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
MaxMouse
It's Friday... That's good enough for me!
CodeX
non stop lolz here but thats soon to end thanks to uni, surely the rest of the world is going good?
stabat
how things are going guys? Here... boring...
CodeX
I must be going wrong on the password lengths then, as long as it was done on ECB
MaxMouse
lol... the key is in hex (MD5: of the string "doit" without the "'s) and is in lower case. Maybe i should have submitted this as a challenge!

Donate
Donate and help us fund new challenges
Donate!
Due Date: May 31
May Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Herschel Space
Observatory spots
galaxies merging
Ctl-P for pizza
Report: China IP
theft now equal in
value to US exports
to Asia
Kim Dotcom claims
invention of
two-factor
authentication
US power grid the
target of "numerous
and daily"
cyber-attacks
Prenda lawyers miss
sanctions deadline
HP down on all
fronts in Q2, but
profits higher than
expected
Microsoft floats
Azure cloud into
China
Twitter locks down
logins by adding
two-factor
authentication
IiNet offloads
fibre network to
NBN Co
Slashdot
Missile Test
Creates Huge
Expanding Halo of
Light Over Hawaii
3D Printers For
Peace Contest
Intel"s Linux
OpenGL Driver
Faster Than Apple"s
OS X Driver
Rough Roving:
Curiosity"s Wheels
Show Damage
Tesla Motors Repays
$465M Government
Loan 9 Years Early
Why the "Star Trek
Computer" Will Be
Open Source and
Apache Licensed
NYPD Detective
Accused of Hiring
Email Hackers
Scientists Find
Vitamin C Kills
Drug-Resistant
Tuberculosis
German IT Firm
Seeks Autistic
Workers
Violent Galactic
Clash May Solve
Cosmic Mystery
Article viewer

Using the Mutex - Detecting if your application is already running



Written by:dimport
Published by:Nightscript
Published on:2003-06-21 07:19:46
Topic:Visual Basic
Search OSI about Visual Basic.More articles by dimport.
 viewed 23024 times send this article printer friendly

Digg this!
    Rate this article :
How to detect if your application is already running, and if so terminate it, using a neat method know as the Mutex.

The Mutex may seem like a weird and wonderful term, but it is simply a means of creating a unique process ID within the windows operating system. You can use these values for many different means, but here it is being used to uniquely identify our application. Once the Mutex value has been created, we can then detect for its existance, and action on whether it is found.

Cut and Paste the function and variable declarations into the header of your VB application, and drop the execution code into the starting function of your application, either the form_load(), or main() method.

'DLL Function Calls

Private Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (ByVal lpMutexAttributes As Long, ByVal bInitialOwner As Long, ByVal lpName As String) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
 
'variable constant to match if the mutex exists
Private Const ERROR_ALREADY_EXISTS = 183&
 
'Application Variable Declarations
Public Const AppVer = "MyApp v1.1"
Dim mutexvalue As Long
 
'*the following code would go in the starting function of your application
'either the main() or form_load() depending on how your application works
 
 'Create an individual mutex value for the application
mutexvalue = CreateMutex(ByVal 0&, 1, AppVer)
 
'If an error occured creating the mutex, that means it
'must have already existed, therefore your application
'is already running
If (Err.LastDllError = ERROR_ALREADY_EXISTS) Then
 
 'Inform the user of running the same app twice
 MsgBox "The application is already running."
 
 'Terminate the application via the reference to it, its hObject value
 CloseHandle mutexvalue
End If


This article was originally written by barnseyboy

Did you like this article? There are hundreds more.

Comments:
shab
2003-12-11 21:52:04
If you want this in C/C++ the code would be something like this:

#include <windows.h>
#include <stdlib.h>

int main(int argc, char **argv) {
 HANDLE hd;
 
 hd = CreateMutex(NULL, FALSE, "Foo");
 if(GetLastError() == ERROR_ALREADY_EXISTS)
  return 0;

 system("pause"); /* You can replace this with anything that keep the program alive ^_^ */
 return 0; }
Anonymous
2009-10-05 22:02:06
Wow, thanks for the code, it's working perfect.
1000 games
Anonymous
2009-12-04 12:05:19
Thanks Shab. How can I stop earlier running instances instead of current. I want to continue with current instance instead of earlier
Anonymous
2010-02-19 07:39:40
Visual Basic 2008: Get list of processes and how to terminate them
Anonymous
2010-03-02 15:24:54
There was an error even the first time the mutex wes created. I solved it removing space from AppVer value.
Anonymous
2011-02-18 10:45:19
Fucking SPAMMERS! No one buys the shit you post about!
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
VB Potpourri: Programming, History And Syntax by batterseapower

A nice little collection of the basics of Visual Basic.
Advanced Programming Techniques by batterseapower

A fiendish test covering some of the more obscure elements of Visual Basic 6.


     
Your Ad Here
 
Copyright Open Source Institute, 2006