flat assembler
Message board for the users of flat assembler.

Index > Windows > Unique instance with a Mutex not working.

Author
Thread Post new topic Reply to topic
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg 23 Aug 2011, 12:10
When I run this code CreateMutexA always returns zero no matter what I do. What is the problem? I'm on win7 32bit.
Code:
format PE GUI 4.0
include "win32ax.inc"

;Only one instance

invoke CreateMutexA, 0, 0, "Global\\{12345678-1234-1234-1234-123456789ABC}"
cmp eax, 0
jne another_instance_not_found
invoke MessageBoxA, 0, "Another instance found!", "Test", MB_OK
invoke ExitProcess, 1
another_instance_not_found:
invoke MessageBoxA, 0, "Another instance NOT found!", "Test", MB_OK
invoke ExitProcess, 0
data import

library kernel32,'KERNEL32.DLL',user32,'USER32.DLL'
                                                      
 import kernel32,\                                    
        ExitProcess,'ExitProcess', CreateMutexA,'CreateMutexA',\
                                                     
 import user32,\                                      
        MessageBoxA,'MessageBoxA'


end data    

Thanks for help!
Post 23 Aug 2011, 12:10
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20425
Location: In your JS exploiting you and your system
revolution 23 Aug 2011, 12:18
Have you read this?:
Win32 Doc wrote:
lpName
Points to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.
Why do you have two backslashes there?
Post 23 Aug 2011, 12:18
View user's profile Send private message Visit poster's website Reply with quote
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg 23 Aug 2011, 12:24
revolution wrote:
Have you read this?:
Win32 Doc wrote:
lpName
Points to a null-terminated string specifying the name of the mutex object. The name is limited to MAX_PATH characters and can contain any character except the backslash path-separator character (\). Name comparison is case sensitive.
Why do you have two backslashes there?


Amazing! I couldn't figure that out myself! It's there because in c++ it worked for me that way.
Somewhere there was a random mutex name generator and I just pasted it there and it completely worked in code::blocks
Post 23 Aug 2011, 12:24
View user's profile Send private message Reply with quote
AsmGuru62



Joined: 28 Jan 2004
Posts: 1660
Location: Toronto, Canada
AsmGuru62 23 Aug 2011, 13:06
Pretty cool!

MSDN says that "Global\" is a valid prefix:
http://msdn.microsoft.com/en-us/library/ms682411(v=VS.85).aspx

In FASM a double backslash will produce a double backslash!
In C++, however, this turned into a single one, which is valid!

That shows that paths and names can't be "ported" into FASM just by copying stuff.

btw: URL tag is not working. Mad

Edit by revolution: Fixed url tag
Post 23 Aug 2011, 13:06
View user's profile Send private message Send e-mail Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20425
Location: In your JS exploiting you and your system
revolution 23 Aug 2011, 13:14
AsmGuru62 wrote:
MSDN says that "Global\" is a valid prefix
Indeed, but not compatible with older Windows versions like Win95 (if this is ever an issue for anyone anymore).
Post 23 Aug 2011, 13:14
View user's profile Send private message Visit poster's website Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 23 Aug 2011, 13:20
You also could have checked the error code from GetLastError.
Post 23 Aug 2011, 13:20
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg 23 Aug 2011, 13:36
vid wrote:
You also could have checked the error code from GetLastError.


I sent this pm to revolution but I'll put it here, too:

If I comment out the getlasterror line CreateMutexA doesn't work. Otherwise it works. Is getlasterror a requirement for this function to return a value?
Code:
format PE GUI 4.0
include "win32ax.inc"

invoke CreateMutexA, 0, 1, mutex_name
invoke GetLastError     ;this!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

cmp eax, 0
je another_instance_not_found
invoke MessageBoxA, 0, "Another instance found!", "Test", MB_OK
invoke ExitProcess, 1

another_instance_not_found:
invoke MessageBoxA, 0, "Another instance NOT found!", "Test", MB_OK
invoke ExitProcess, 0
data import

library kernel32,'KERNEL32.DLL',user32,'USER32.DLL'
                                                      
 import kernel32,\                                    
        ExitProcess,'ExitProcess', CreateMutexA,'CreateMutexA',\
        GetLastError,'GetLastError'
                                                      
 import user32,\                                      
        MessageBoxA,'MessageBoxA'


end data

mutex_name db "d41d8cd98f00b204e9800998ecf8427e",0    
Post 23 Aug 2011, 13:36
View user's profile Send private message Reply with quote
rohagymeg



Joined: 19 Aug 2011
Posts: 77
rohagymeg 23 Aug 2011, 13:56
Everything is fine, I just understood that it returns with GetLastError, and CreateMutexA returns the mutex handle so it always showed the same return value because I checked eax after CreateMutexA, not after GetLastError, which returns ERROR_ALREADY_EXISTS, and that's what I was looking for to ensure that only one instance is running.
Post 23 Aug 2011, 13:56
View user's profile Send private message 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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.