用vb 防止 其他应用程序的重复运行
我的想法:通过VB打开一个应用程序(如autocad、word、solidworks)。若autocad没有打开,则打开;若autocad已经打开,则提示该程序已经打开,...
我的想法:通过VB打开一个应用程序(如autocad、word、solidworks)。
若autocad没有打开,则打开;
若autocad已经打开,则提示该程序已经打开,退出vb程序。
请高手赐教啊! 展开
若autocad没有打开,则打开;
若autocad已经打开,则提示该程序已经打开,退出vb程序。
请高手赐教啊! 展开
5个回答
2011-09-13
展开全部
如下,可以这么调用:CheckRun "AutoCad.exe"
Function CheckRun(ByVal procname As String) As Boolean
On Error GoTo ErrCheckRun
'判断程序是否运行
'procname-文件名称
CheckRun = False
Dim rtn&, strcomputer$
Dim objWMIService As Object
Dim colProcessList As Object
Dim objProcess As Object
strcomputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strcomputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & procname & "'")
If colProcessList.Count = 0 Then
Exit Function
End If
CheckRun = True'procname正在运行
Exit Function
ErrCheckRun:
MsgBox Err.Description, vbCritical, "Error in CheckRun"
End Function
Function CheckRun(ByVal procname As String) As Boolean
On Error GoTo ErrCheckRun
'判断程序是否运行
'procname-文件名称
CheckRun = False
Dim rtn&, strcomputer$
Dim objWMIService As Object
Dim colProcessList As Object
Dim objProcess As Object
strcomputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strcomputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process Where Name = '" & procname & "'")
If colProcessList.Count = 0 Then
Exit Function
End If
CheckRun = True'procname正在运行
Exit Function
ErrCheckRun:
MsgBox Err.Description, vbCritical, "Error in CheckRun"
End Function
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
方法很多,比如说先列举当前进程名(这种代码很多搜索一下就能找到),比如说要打开autocad,就先在进程列表中查找有没有名字是acad.exe的,如果没找到就运行,如果找到了就做你要的操作。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Option Explicit
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_RESTORE = &HF120&
Private Sub Form_Load()
Call ActivateTheForm
End Sub
Public Sub ActivateTheForm()
Dim Designation As String, According As String
If App.PrevInstance Then
Designation = Me.Caption
Me.Caption = Designation & Space$(1)
According = FindWindow(vbNullString, Designation)
Call SetForegroundWindow(According)
Call SendMessage(According, WM_SYSCOMMAND, SC_RESTORE, 0)
End
End If
End Sub
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_SYSCOMMAND = &H112
Private Const SC_RESTORE = &HF120&
Private Sub Form_Load()
Call ActivateTheForm
End Sub
Public Sub ActivateTheForm()
Dim Designation As String, According As String
If App.PrevInstance Then
Designation = Me.Caption
Me.Caption = Designation & Space$(1)
According = FindWindow(vbNullString, Designation)
Call SetForegroundWindow(According)
Call SendMessage(According, WM_SYSCOMMAND, SC_RESTORE, 0)
End
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用API的FINDWINDOW找一下啊
打开用SHELL函数
打开用SHELL函数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
程序打开都都有运行的进程,可以通过查找进程实现
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询