1. win32lib 'destroy_self'
...someone recently asked for something like this ?
This win32lib program destroys itself in 2 seconds if another copy is already
running.
-- start demo --
include win32lib.ew
atom ptr, ptr2, ok, proghwnd
constant
-- new function !
zFindWindow = linkFunc(user32, "FindWindowA", {C_POINTER,C_INT},C_INT)
-- the normal win32lib main szClassName
ptr = allocate_string("Win32Lib AppWindow 1")
-- the following is this win 'title'
ptr2 = allocate_string("kick_self")
-- is this already running ?
proghwnd= c_func( zFindWindow, { ptr, ptr2 } )
constant
Win = create( Window,"kick_self",0,Default,Default,250,200,0)
procedure check_self()
-- destroy second instance, if first exists
if proghwnd != 0 then
-- just to test, lets see this instance for 2 seconds...anyways
setVisible(Win,1)
sleep(2)
closeWindow(Win)
end if
end procedure
procedure goto_first_instance()
if proghwnd !=0 then
-- set focus to first instance
ok = c_func( xSetForegroundWindow,{proghwnd})
end if
free(ptr)
free(ptr2)
end procedure
onOpen[Win] = routine_id("check_self")
onDestroy[Win] = routine_id("goto_first_instance")
WinMain( Win, Normal )
-- end demo --
2. Re: win32lib 'destroy_self'
[ to BKB, and all ]
> Thanks Wolf, this is what I was looking for.
No problem, but the demo actually has a little bug, *only* because it shows
the second instance momentarily, which you normally wouldn't do, so:
procedure goto_first_instance()
if proghwnd !=0 then
-- give 'desktop' time to re-paint.
sleep(1)
--etc......
...otherwise the first instance sometimes remains behind Explorer, even tho
it has focus...
Wolf