Assembly code snippets

Back to the snippets overview

Details

TitleBlock multiple instances
Authorsmurf
Submitted by:smurf
Date added:2002-02-18 03:29:42
Date modified:2002-02-21 01:14:02

Comments

This code snipet blocks multiple instances of your program. When you first start your application a mutex is created and isnt destroyed until you exit your application. All attempts to startup another instance is blocked.

Snippet

.data
;// important: change the text to something original
SingleInstance db "Only one Instance of Me Allowed",0

.data?
;// will be the mutex handle you will need to destroy this later
hMutex dd ?

.code
;// this code will be the very first code in your .code section
   invoke CreateMutex,0,FALSE,addr SingleInstance ;// create a mutex
   mov hMutex,eax                                 ;// save the handle
   invoke GetLastError                            ;// check for errors
      .if eax == ERROR_ALREADY_EXISTS             ;// if mutex exists already
         invoke ExitProcess, 0                    ;// exit program
      .endif

;// destroy the handle to the mutex before you exit your application
.ELSEIF uMsg == WM_DESTROY
   invoke CloseHandle,hMutex