flat assembler
Message board for the users of flat assembler.

Index > Tutorials and Examples > Mutex example.

Author
Thread Post new topic Reply to topic
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 25 Mar 2013, 21:11
As I understood, this section of the forum is intended to be a place for examples i.e. programs that shed some light to the selected aspects of programming possibly in the simplest way leaving other, not directly related aspects, only roughly sketched. The purpose of such an approach is to give people learning FASM the kind of quick and convenient answers for questions that may occur during the programming work.

So, here you have a very simple example of the mutex realization that may be used to determine whether the current instance of your application is the first or another one.

Image

The source code:
Code:
;
; A simple mutex example written with FASM. Allows to determine whether the current
; instance of application is the only one running in the system or there are also
; other active instances.
;
; (C) Mikolaj Hajduk, 25-26.03.2013.
;
format PE GUI 4.0

; Include a header file containing necessary macros.
;
include 'win32wx.inc'

; Use UTF-8 encoding if you want to have your strings displayed always the same
; way despite of local settings of the user system and if your application has to
; be multilingual.
;
include 'ENCODING\utf8.inc'

; The data section.
;
.data
        ; A string containing an arbitrary chosen name of the mutex.
        ;
        MutexName       du "MyMutex", 0

        ; A place for the mutex handle.
        ;
        hMutex          dd ?

; The code section.
;
.code

        start:
                ; Create (if it didn't exist) or open (if another instance of application
                ; has created it) a mutex object.
                ;
                invoke  CreateMutex,\
                        \
                        NULL,\          ; The mutex handle won't be inherited by
                        \               ; child processes.
                        \
                        FALSE,\         ; Set this parameter to 'FALSE' if you want
                        \               ; to use the same mutex in different instances
                        \               ; of application and don't want to specify
                        \               ; the mutex owner.
                        \
                        MutexName       ; A pointer to the mutex name (length of maximally
                                        ; MAX_PATH characters).

                ; If the function has failed, display a proper error message.
                ;
                test    eax, eax
                jz      .Error

                ; If the function has worked properly, copy the mutex handle to
                ; the 'hMutex' variable.
                ;
                mov     [hMutex], eax

                ; Check the value returned by 'GetLastError'. If the value is not
                ; equal to zero (ERROR_SUCCESS), i.e. in fact is equal to the value
                ; ERROR_ALREADY_EXISTS then it means that the current instance
                ; of the application isn't the first one.
                ;
                invoke  GetLastError
                test    eax, eax
                jnz     .NextInstance

                ; Operations performed when the application has been run for the first time.
                ;
                .FirstInstance:
                        invoke  MessageBox, HWND_DESKTOP, "I'm the FIRST instance of the application.",\
                                        "MutexExample", MB_OK

                        jmp     .Cleanup

                ; Operations performed when there are already some other running instances
                ; of the application.
                ;
                .NextInstance:
                        invoke  MessageBox, HWND_DESKTOP, "I'm ANOTHER instance of the application.",\
                                        "MutexExample", MB_OK

                .Cleanup:
                        ; Close the mutex handle.
                        ;
                        invoke  CloseHandle, [hMutex]
                        test    eax, eax
                        jnz     .End

                ; If there an error occurred then the proper message box is displayed.
                ;
                .Error:
                        stdcall ShowLastError, HWND_DESKTOP

                .End:
                        invoke  ExitProcess, 0

        ; A simple procedure that displays a message box containing a human readable
        ; description of the last error.
        ;
        proc    ShowLastError, hwnd

                locals
                        Buffer dd ?
                endl

                push    ebx

                invoke  GetLastError

                lea     ebx, [Buffer]
                invoke  FormatMessage, FORMAT_MESSAGE_ALLOCATE_BUFFER + FORMAT_MESSAGE_FROM_SYSTEM,\
                                        0, eax, LANG_NEUTRAL, ebx, 0, 0
                invoke  MessageBox, [hwnd], [Buffer], NULL, MB_ICONERROR + MB_OK
                invoke  LocalFree, [Buffer]

                pop     ebx

                ret
        endp

.end start
    
The zipped archive with the source and executable may be found here.

Smile

[EDIT]Minor code corrections.[/EDIT]


Last edited by MHajduk on 26 Mar 2013, 11:31; edited 1 time in total
Post 25 Mar 2013, 21:11
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 25 Mar 2013, 22:00
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:15; edited 1 time in total
Post 25 Mar 2013, 22:00
View user's profile Send private message Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 25 Mar 2013, 22:11
http://en.wikipedia.org/wiki/Mutual_exclusion

Mutexes are objects that may be used to control access to shared resources to avoid collisions. Here in this thread has been mentioned rather peripheral yet quite useful aspect of the mutex realization.
Post 25 Mar 2013, 22:11
View user's profile Send private message Visit poster's website Reply with quote
HaHaAnonymous



Joined: 02 Dec 2012
Posts: 1178
Location: Unknown
HaHaAnonymous 25 Mar 2013, 22:34
[ Post removed by author. ]


Last edited by HaHaAnonymous on 28 Feb 2015, 21:15; edited 1 time in total
Post 25 Mar 2013, 22:34
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1670
Location: Toronto, Canada
AsmGuru62 26 Mar 2013, 13:09
Very nice code layout!!
Post 26 Mar 2013, 13:09
View user's profile Send private message Send e-mail Reply with quote
MHajduk



Joined: 30 Mar 2006
Posts: 6115
Location: Poland
MHajduk 26 Mar 2013, 13:24
Thank you, AsmGuru62. Smile I used to form code this way because it allows me to comeback after some time and recall almost the whole idea of the particular program without an extra effort, so the initial work on the code structuring usually pays off. Wink
Post 26 Mar 2013, 13:24
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.