Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function CreateFile Lib "KERNEL32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As String, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Private Declare Function CloseHandle Lib "KERNEL32" (ByVal hObject As Long) As Long
Private Const GENERIC_READ = &H80000000
Private Const GENERIC_WRITE = &H40000000
Private Const OPEN_EXISTING = 3
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const INVALID_HANDLE_VALUE = -1
Private Sub Command1_Click()
Dim RetVal
If Dir("D:\222.exe") <> "" And IsFileRun("D:\222.exe") Then
MsgBox "文件D:\222.exe已运行!"
Else: RetVal = Shell("cmd.EXE", 1)
End If
End Sub
Private Sub Form_Load()
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
End Sub
Private Function IsFileRun(ByVal pFile As String) As Boolean
Dim ret As Long
ret = CreateFile(pFile, GENERIC_READ Or GENERIC_WRITE, 0&, vbNullString, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0&)
IsFileRun = (ret = INVALID_HANDLE_VALUE)
CloseHandle ret
End Function
'我把cmd.exe当作你所说的111.exe.另外你用个exe文件改成222名放在D盘吧.不然没文件就会出错.我测试过了.通过!
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = 'explorer.exe'")
If colProcessList.Count>0 Then
'如果正在运行的语句块
Else
Shell "Explorer.exe" '没运行则运行之
End If
Select * from Win32_Process Where Name = 'explorer.exe'
这句里头explorer.exe是进程名称
run=false
for each ps in getobject("winmgmts:\\.\root\cimv2:win32_process").instances_
if ps.executablepath="D:\222.exe" then run=true:exit for
next
if not run then createobject("wscript.shell").run "D:\111.exe"