如何控制可执行文件不被重复打开
我用VFP设计了一个小程序,并将其编译成可执行文件。双击这个可执行文件,运行正常。我希望再次双击它时,它不再运行,但能最大化先打开的这个运行窗口。
PUBL gcOldDir,vcxname gcOldDir = SET('DEFAULT') + SYS(2003) SET DEFA TO "&gcOldDir" ****************************************************************************** &&获取程序运行名称 Declare INTEGER GetModuleHandle IN kernel32 INTEGER lpModuleName vcxid = ALLTRIM(STR(GetModuleHandle(0))) Declare INTEGER GetModuleFileName IN kernel32; INTEGER hModule,; STRING @ lpFilename,; INTEGER nSize lpFilename = SPACE(250) lnLen = GetModuleFileName (0, @lpFilename, Len(lpFilename)) vcxname = JUSTFNAME(ALLTRIM(Left (lpFilename, lnLen))) ****************************************************************************** #DEFINE ERROR_ALREADY_EXISTS 183 DECLARE INTEGER GetLastError IN WIN32API DECLARE INTEGER CreateMutex IN WIN32API INTEGER Security,; SHORT Owned,; STRING@ MutexName DECLARE SHORT CloseHandle IN WIN32API INTEGER PUBLIC hMutex, iResult, bCanRun hMetux=CreateMutex(0,0,vcxname) iResult=GetLastError() DO CASE CASE iResult==0 bCanRun=.T. CASE iResult==ERROR_ALREADY_EXISTS MessageBox("程序已经运行。"+SPACE(2),64,"提示") bCanRun=.F. OTHERWISE bCanRun=.F. MessageBox("GetLastError() returned "+ ltrim(str(iResult))) ENDCASE IF bCanRun ==.F. =CloseHandle(hMutex) QUIT ENDIF
DECLARE LONG CreateMutex IN Kernel32 STRING@, LONG, STRING@ DECLARE LONG GetLastError IN Kernel32 DECLARE LONG CloseHandle IN Kernel32 LONG szPrg = PROGRAM() hMutex = CreateMutex(NULL, 0, @szPrg) IF GetLastError() != 183 && ERROR_ALREADY_EXISTS MESSAGEBOX(szPrg + " 正常启动运行") ELSE MESSAGEBOX(szPrg + " 不能重复运行") ENDIF CloseHandle(hMutex) CLEAR DLLS RETURN