如:
下面为vb2005
的例子.
Dim
p
As
New
Process
For
Each
p
In
Process.GetProcessesByName("notepad")
MsgBox(p.MainModule.FileName)
If
p.MainModule.FileName.ToLower
=
"c:\windows\system32\notepad.exe"
Then
p.Kill()
End
If
Next
看是否运行:Private
Declare
Function
FindWindow
Lib
"user32"
Alias
"FindWindowA"
(ByVal
lpClassName
As
String,
ByVal
lpWindowName
As
String)
As
Long
Private
Sub
Command5_Click()
Dim
lHwnd
As
Long
lHwnd
=
FindWindow(vbNullString,
"程序的Title或Caption")
If
lHwnd
<>
0
Then
MsgBox
"程序正在运行!"
End
If
End
Sub
向它发送指令:
AppActivate
"程序的Title或Caption"
SendKeys
"指令"
要启动程序:
Call
Shell("完整路径和程序名称.exe")
Imports System.Runtime.InteropServicesModule Module1
Sub Main()
Dim instance As Process = RunningInstance()
If instance IsNot Nothing Then
ShowWindowAsync(instance.MainWindowHandle, 3) '调用api函数,正常显示窗口
Return
End If
Dim F1 As New Form1
Application.Run(F1) '显示窗体
End Sub
<DllImport("User32.dll")>
Public Function ShowWindowAsync(ByVal hWnd As System.IntPtr, ByVal cmdShow As Integer) As Boolean
End Function
Private Function RunningInstance() As Process '返回进程中已经打开的程序
Dim current As Process = Process.GetCurrentProcess()
Dim processes As Process() = Process.GetProcessesByName(current.ProcessName)
For Each process As Process In processes
If process.Id <>current.Id Then
If process.MainModule.FileName = current.MainModule.FileName Then
Return process
End If
End If
Next
Return Nothing
End Function
End Module
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)