News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Open a program only one time

Started by RuiLoureiro, April 17, 2008, 05:45:11 PM

Previous topic - Next topic

RuiLoureiro

Hi all,
          I want to open my program only one time. If anyone try to open second time, the program cannot open. How to do this ?
          Can Anyone help me.

My prog start with
;....................................
_Start:
    invoke  GetModuleHandle, 0             
    mov     _hInstance, eax                 
    invoke  WinMain, _hInstance, NULL, NULL, SW_SHOWDEFAULT
    invoke  ExitProcess, eax
;..............................................

Thank you
Rui

Tedd

The 'cheap' way is to use FindWindow - if you can find a window with your classname and window-title, then don't call winmain.
The other is to use a named mutex - same principle, but more robust.
No snowflake in an avalanche feels responsible.

RuiLoureiro

Quote from: Tedd on April 17, 2008, 05:50:51 PM
The 'cheap' way is to use FindWindow - if you can find a window with your classname and window-title, then don't call winmain.

Thank you Tedd,
                          Is it something like this ?
;..........................................................................................
_Start:
    invoke  GetModuleHandle, 0             
    mov     _hInstance, eax 
               
    invoke  FindWindow, offset _ClassName, offset _WindowName
    cmp     eax, NULL
    je      @F
   ; ********************
   ; The program is oppend
   ;********************
    invoke  ExitProcess, 0
;.............................................................
@@:   invoke  WinMain, _hInstance, NULL, NULL, SW_SHOWDEFAULT
         invoke  ExitProcess, eax

u

Yes, exactly.


Usually it's also good to notify the existing application that the user wants to see it. (i.e if it's hidden in the tray, or the main window is minimized). In some apps, ShowWindow() is enough, in others you PostMessage(eax,WM_MY_CUSTOM_MESSAGE1,0,0). WM_MY_CUSTOM_MESSAGE1 is for instance equal to (WM_USER+200). (replace "200" with whatever positive number you want). Toy a bit with the different methods, they're very easy.
Please use a smaller graphic in your signature.

ragdog

hi

i have this found on my drive i hope it help



   invoke CreateMutex,0,TRUE,addr AppName
   push   eax
   invoke GetLastError
    .IF eax==ERROR_ALREADY_EXISTS
    pop    eax
       .IF eax !=0
    invoke CloseHandle,eax
       .ENDIF
invoke MessageBox,0,addr AlRuning,0,MB_OK + MB_ICONERROR
    jmp @F
    .ELSE
    pop    eax
    .ENDIF
   invoke GetModuleHandle,0
   mov    hInstance,eax
   invoke DialogBoxParam,eax,100,0,addr DlgProc,0
   @@:
   invoke ExitProcess,eax

chlankboot

you want to allow only one instance of your app, best way (as Tedd said) is the use of mutex (it sounds strange but it's simple, at leaset for our use here)

Declarations:


.data
MutexAtt  SECURITY_ATTRIBUTES <SIZEOF SECURITY_ATTRIBUTES,NULL,TRUE>
AppName  db "MyAppName",0

.data?
hInstance dd ?


Code:

.code
start:
invoke GetModuleHandle, NULL
mov    hInstance,eax
; we'll try to open our mutex to see if it exists
invoke OpenMutex,MUTEX_ALL_ACCESS,FALSE,addr AppName
.if eax==NULL ;mutex does not exist
invoke CreateMutex,addr MutexAtt,TRUE, addr AppName ;we create it, so next instances will find it
mov hMutex,eax
invoke WinMain, hInstance,NULL,NULL, SW_SHOWDEFAULT ; run normally
.endif
invoke ExitProcess,eax ;


This will do the job

RuiLoureiro

Hi all,
           Thank you for your help ( Ultrano, ragdog and chlankboot ).
            It works. Now i cannot open my prog or app for the second time.
            My problem is solved.
RuiLoureiro