- '窗体中放一个默认的按钮
- PrivateDeclareFunctionCreateToolhelp32SnapshotLib"kernel32"(ByValdwFlagsAsLong,ByValth32ProcessIDAsLong)AsLong
- PrivateDeclareFunctionProcess32FirstLib"kernel32"(ByValhSnapshotAsLong,lppeAsPROCESSENTRY32)AsLong
- PrivateDeclareFunctionProcess32NextLib"kernel32"(ByValhSnapshotAsLong,lppeAsPROCESSENTRY32)AsLong
- PrivateDeclareFunctionCloseHandleLib"kernel32"(ByValhObjectAsLong)AsLong
- PrivateDeclareFunctionOpenProcessLib"kernel32"(ByValdwDesiredAccessAsLong,_
- ByValblnheritHandleAsLong,ByValdwAppProcessIdAsLong)AsLong
- PrivateDeclareFunctionTerminateProcessLib"kernel32"(ByValApPHProcessAsLong,_
- ByValuExitCodeAsLong)AsLong
- PrivateTypePROCESSENTRY32
- dwSizeAsLong
- cntUsageAsLong
- th32ProcessIDAsLong
- th32DefaultHeapIDAsLong
- th32ModuleIDAsLong
- cntThreadsAsLong
- th32ParentProcessIDAsLong
- pcPriClassBaseAsLong
- dwFlagsAsLong
- szExeFileAsString*1024
- EndType
- ConstTH32CS_SNAPHEAPLIST=
- ConstTH32CS_SNAPPROCESS=
- ConstTH32CS_SNAPTHREAD=
- ConstTH32CS_SNAPMODULE=
- ConstTH32CS_SNAPALL=(TH32CS_SNAPHEAPLISTOrTH32CS_SNAPPROCESSOrTH32CS_SNAPTHREADOrTH32CS_SNAPMODULE)
- ConstTH32CS_INHERIT=
- PrivateSubCommand1_Click()
- DimmyAsPROCESSENTRY32
- DimlAsLong
- Diml1AsLong
- DimFileNameAsString*9
- DimmProcIDAsLong
- l=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0)
- IflThen
- my.dwSize=1060
- If(Process32First(l,my))Then'遍历第一个进程
- Do
- FileName=my.szExeFile
- IfUCase(FileName)="EXCEL.EXE"Then'当前的进程是Excel
- mProcID=OpenProcess(1&,-1&,my.th32ProcessID)
- TerminateProcessmProcID,0&'结束Excel进程
- Debug.Print"结束了"&FileName
- DoEvents
- EndIf
- LoopUntil(Process32Next(l,my)<1)'遍历所有进程直到返回值为False
- EndIf
- l1=CloseHandle(l)
- EndIf
- EndSub
用VB结束所有Excel的进程,用VB开发Exxcel的应用时,如果出现意外退出,Excel进程会驻留,以后再调用Excel时不能正常使用,所以有时有必要将所有Excel全部结束
原文链接:https://www.f2er.com/vb/264194.html