Vb制作小程序
我想做2个小程序,我要让他们互相检测程序是否在运行,如果2个程序中只要关掉任意一个,立即打开!这2个程序我分别放在d盘和e盘!
补充:我是新手,跪求高手帮我代码写下!
追问:为什么没用呀。。。。。我把2个全部打开了。在用任务管理器把123进程给关了,结果456没把123打开!
我想做2个小程序,我要让他们互相检测程序是否在运行,如果2个程序中只要关掉任意一个,立即打开!这2个程序我分别放在d盘和e盘!
补充:我是新手,跪求高手帮我代码写下!
追问:为什么没用呀。。。。。我把2个全部打开了。在用任务管理器把123进程给关了,结果456没把123打开!
答案:这个不难办到
你用一个新的模块将下面的代码放进去
Private Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, lppe As PROCESSENTRY32) As Long
Private Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal dwFlags As Long, ByVal th32ProcessID As Long) As Long
Private Declare Function CloseHandle Lib "kernel32.dll" (ByVal hObject As Long) As Long
Private Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szExeFile As String * 260
End Type
Private Const PROCESS_QUERY_INFORMATION = 1024
Private Const PROCESS_VM_READ = 16
Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const TH32CS_SNAPPROCESS = &H2
Public Function TestProcIsRunning(ByVal ProcName As String) As Boolean
Dim hFindProc As Long
hFindProc = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
Dim ProcData As PROCESSENTRY32
ProcData.dwSize = Len(ProcData)
Process32First hFindProc, ProcData
While (Process32Next(hFindProc, ProcData) = 1)
Dim sPro As String
sPro = Left(UCase(ProcData.szExeFile), InStr(UCase(ProcData.szExeFile), Chr(0)) - 1)
If sPro = UCase(ProcName) Then
CloseHandle hFindProc
TestProcIsRunning = True
Exit Function
End If
Wend
CloseHandle hFindProc
TestProcIsRunning = False
End Function
然后你用一个timer来实现判断就可以啦
Private Sub Timer1_Timer()
If TestProcIsRunning("你要判断的进程名") = True Then
Shell "调用这个程序的路径", vbHide ''' 你写双进程源码的时候最好要隐藏程序
End If
End Sub
你好,双进程我也写过,至于编译后运行很容易被360拦截掉的,不过用来练手也感觉不错..使用WINDOWS API中的相关进程函数