flat assembler
Message board for the users of flat assembler.
![]() |
Author |
|
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. ![]() 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 ![]() [EDIT]Minor code corrections.[/EDIT] Last edited by MHajduk on 26 Mar 2013, 11:31; edited 1 time in total |
|||
![]() |
|
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 |
|||
![]() |
|
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. |
|||
![]() |
|
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 |
|||
![]() |
|
AsmGuru62 26 Mar 2013, 13:09
Very nice code layout!!
|
|||
![]() |
|
< Last Thread | Next Thread > |
Forum Rules:
|
Copyright © 1999-2025, Tomasz Grysztar. Also on GitHub, YouTube.
Website powered by rwasa.