There is a very simple way to ensure that a PE is runing only once, when we want to prevent multi-instance runs: api 'KERNEL32.OpenMutexA' 0FFFFFFFF &TRUE BaseMutexName If eax e 0 api 'KERNEL32.CreateMutexA' &NULL &TRUE BaseMutexName Else jmp Exit_PE End_If Problem: 1) 0FFFFFFFF should be: "MUTEX_ALL_ACCESS" which is NOT inside my Windows.inc... Does any one know what is the true value? (0FFFFFFFF is of my invention). 2) The way i do it is durty: CreateMutex should be enough alone and should return 0 when Mutex already defined. But i have never been able to make this function fail. Did someone understood relative structures? and how to make it fail if Mutex already defined? betov.
betov, Is this what you are looking for? James .data szAppId db "MyApp",0 .code OnlyOneInstance PROC LOCAL hMutex:DWORD INVOKE CreateMutex,NULL,1,ADDR szAppId mov hMutex,eax INVOKE GetLastError .IF eax == ERROR_ALREADY_EXISTS INVOKE CloseHandle,hMutex mov eax,1 .ELSE mov eax,0 .ENDIF ret OnlyOneInstance ENDP
Yes, exactly. Thanks JcFuller. What i didn't understood is that this function returns the handle wether it previously exist or not. I was waiting for a zero in case it already exist... Thanks a lot. betov.