Im trying to write a program that checks every so offen if a program (calculator for testing) is running then kill it. This code doesent works and i want to know why not. Also every comment about it is wellcome. I having problems with no cpu infinite loop (is Sleep, INFINITE correct?). Thank u for all.


.386
.model flat, stdcall
option casemap:none

include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
include \masm32\include\shell32.inc
includelib \masm32\lib\shell32.lib
include \masm32\include\advapi32.inc
includelib \masm32\lib\advapi32.lib

Check proto

.const
CHECKTIMER equ 1

.data
appname db "first",0
mutexname db "only once mutex",0
msgno db "Not found",0
msgyes db "Found",0
KilledName db "Calculadora",0
IsPresent dd 0

.data?
hinstance HINSTANCE ? ; Instance handle of our program
hMutex HANDLE ? ; Mutex handle (for run only once))
hcheck HANDLE ? ; Hnadle for check timer

.code
start:

; this checks if a previous instance is running
; =============================================

invoke OpenMutex, MUTANT_ALL_ACCESS, FALSE, offset mutexname
.if eax==0 ; if no mutex then is the first instance
invoke CreateMutex, NULL, TRUE, offset mutexname
mov , eax
.else ; oups, exit
invoke ExitProcess, NULL
.endif

;Get application instance
;========================

invoke GetModuleHandle, NULL
mov hinstance, eax

;Set idle priority
;=================

invoke SetPriorityClass, hinstance, IDLE_PRIORITY_CLASS

;set timer to check program existence
;====================================

invoke SetTimer, NULL, CHECKTIMER, 2000, addr Check
mov hcheck, eax
.if eax==0 ; if 0 no timer
invoke ExitProcess, NULL
.endif

infiniteloop:

invoke Sleep, 100
;invoke WaitMessage
jmp infiniteloop


;==============================================
; Check procedure
;==============================================
Check proc

invoke MessageBox, NULL, addr mutexname, addr appname, MB_OK

cmp IsPresent, 0
jnz exitcheck

invoke FindWindow, addr KilledName, addr KilledName
cmp eax, 0
jz nod2

mov IsPresent, 1
invoke MessageBox, NULL, addr msgyes, addr appname, MB_OK
ret

nod2:

mov IsPresent,0
invoke MessageBox, NULL, addr msgno, addr appname, MB_OK

exitcheck:
ret

Check endp


end start
Posted on 2002-06-11 14:51:40 by bibbo
your check procedure should be defined like this
check PROC hwnd:HWND, uMsg:DWORD, idEvent:DWORD, dwTime:DWORD
Posted on 2002-06-11 15:49:51 by Kudos
The timer doesn't work without a message loop. Windows sends a WM_TIMER message, and GetMessage/PeekMessage will call the timer proc when they receive this message.
Posted on 2002-06-11 18:37:05 by tenkey
I have changed:

infiniteloop:

invoke Sleep, INFINITE
invoke WaitMessage
invoke GetMessage, addr aMsg, NULL, 0, 0
jmp infiniteloop

and:

CheckD2 PROC hwnd:HWND, uMsg:DWORD, idEvent:DWORD, dwTime:DWORD

But it still doesnt work :(
And im lost.
Posted on 2002-06-12 15:45:19 by bibbo
If you do INFINITE sleeping, GetMessage is never called, and thus
the timer proc never gets dispatched. Also, why use WaitMessage?
GetMessage ought to be sufficient.
Posted on 2002-06-12 16:48:07 by f0dder
I thougt, sleep allow messages (like interrupts).
So how can i make a no cpu infinite loop? with only GetMessage?

Also i tried without sleep and proc its not called.

thanks.
Posted on 2002-06-12 18:37:39 by bibbo
a typical message loop looks like this

.WHILE TRUE
invoke GetMessage, ADDR msg,NULL,0,0
.BREAK .IF (!eax)
invoke DispatchMessage, ADDR msg
.ENDW

the cpu must keep calling GetMessage so your program remains responsive so you can never realy have zero cpu ussage if you want your program to keep functioning.
Posted on 2002-06-12 20:28:38 by Kudos
Sleep() suspends your thread. Messages aren't really interrupts,
they're handled with GetMessage and DispatchMessage... if you
suspend your thread infinitely, you wont be calling these APIs, and
messages will not be handled.

The nice thing about GetMessage is that it suspends your thread
until a messages arrives.

Try doing GetMessage + DispatchMessage in the loop, that might
make the timer work. If not, register a wndclass and create an
invisible window.
Posted on 2002-06-13 00:14:11 by f0dder