下面這個例子找出程式是否在執行,你可作乙個處理,如果在執行就退出.
declare function findwindow lib "user32" alias "findwindowa" (byval lpclassname as string, byval lpwindowname as string) as long
declare function postmessage lib "user32" alias "postmessagea" (byval hwnd as long, byval wmsg as long, byval wparam as long, lparam as any) as long
declare function getclassname lib "user32" alias "getclassnamea" (byval hwnd as long, byval lpclassname as string, byval nmaxcount as long) as long
declare function showwindow lib "user32" (byval hwnd as long, byval ncmdshow as long) as long
const sw_shownormal = 1
const wm_close = &h10
const gcclassnamem***cel = "xlmain"
const gcclassnamemsiexplorer = "ieframe"
const gcclassnamemsvbasic = "wndclass_desked_gsk"
const gcclassnamenotepad = "notepad"
private sub main()
'kpd-team 1998
'url:
'e-mail: [email protected]
dim winwnd as long, ret as string, retval as long, lpclassname as string
'ask for a window title
ret = inputbox("enter the exact window title:" + chr$(13) + chr$(10) + "note: must be an exact match")
'search the window
winwnd = findwindow(vbnullstring, ret)
if winwnd = 0 then msgbox "couldn't find the window ...": exit sub
'show the window
showwindow winwnd, sw_shownormal
'create a buffer
' lpclassname = space(256)
'retrieve the class name
' retval = getclassname(winwnd, lpclassname, 256)
'show the classname
' msgbox "classname: " + left$(lpclassname, retval)
'post a message to the window to close itself
postmessage winwnd, wm_close, 0&, 0&
end sub
也可用以下方法:
方法一
option explicit
public sub checkexist(fm as form)
dim title as string
call msgbox("這程式已執行", vbcritical)
fm.caption = ""
end
end if
end sub
private sub form_load()
call checkexist(me)
end sub
方法二
使用mutual exclusion去做
呼叫openmutex函式,如果所指定的mutex已經建立,會傳回非0值,便可斷定程式已經執行,若傳回0,變建立乙個新的mutex,以供判定.程式碼如下
dim hmutex as long
private sub form_load()
if openmutex(0, true, "honey") then
msgbox "程式已經在執行了", vbexclamation
unload me
else
hmutex = createmutex(byval 0&, false, "honey")
end if
end sub
private sub form_unload(cancel as integer)
releasemutex hmutex
closehandle hmutex
end sub
值得注意的事createmutex的宣告中和api檢視員所不同的是將第乙個引數宣告成any型態
sub main()
end
eles frmmain.show
end if
Delphi 防止程式多次執行
program project1 uses forms,windows,unit1 in unit1.pas var hmutex hwnd ret integer begin hmutex createmutex nil,false,test ret getlasterror if ret err...
C 防止程式多次執行
經過我的測試,還比較好用,但是有個問題,如果不登出,用另乙個使用者進入,則程式不能判斷出已執行。所以只限於用在單使用者環境,還是不太完美。class program console.writeline 正在執行中 console.readline public static class oneins...
防止程式多次執行(呼叫DLL)
region 防止程式多次執行 public abstract class oneinstance return ret dllimport kernel32.dll charset charset.auto private static extern intptr openmutex uint d...