vb 启动外部程序后的问题(?)
如何用VB运行一个VC程序编写的代码,运行VB后,点按钮,运行VC,之后的效果是当关闭VB后,自动关闭VC,不单单是按关闭按钮的那种主要困扰的是,别人会用进程关闭VB程序...
如何用VB运行一个VC程序编写的代码,
运行VB后,点按钮,运行VC,之后的效果是
当关闭VB后,自动关闭VC,不单单是按关闭按钮的那种
主要困扰的是,别人会用进程关闭VB程序,而VC不会被关闭,怎么样
让VB被用户关闭(强制关闭)后VC也自动关闭!! 展开
运行VB后,点按钮,运行VC,之后的效果是
当关闭VB后,自动关闭VC,不单单是按关闭按钮的那种
主要困扰的是,别人会用进程关闭VB程序,而VC不会被关闭,怎么样
让VB被用户关闭(强制关闭)后VC也自动关闭!! 展开
3个回答
展开全部
在知道VC程序的标题的情况下用API函数FindWindow和PostMessage
第一步:声明API函数以及一个关键常量WM_CLOSE:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_CLOSE = &H10
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
首先定义一个变量用来存放那个VC程序的窗口句柄:
Dim hwnd_VC As Long
然后再用FindWindow函数获得句柄(假设窗口标题为title):
hwnd_VC=FindWindow(vbNullString,title)
最后用PostMessage函数关闭窗口:
PostMessage hwnd_VC,WM_CLOSE,0,0
完成关闭窗口的功能.
或者不知道标题时先声明一个ActiveX对象以获取进程ID:
Dim cim_obj As Object,p As Object,b As Object,pid As Long,pname As String,hproc As Long
'pname是进程名
Set cim_obj=GetObject("winmgmts://./root/cimv2")
Set b=cim_obj.execQuery("select * from win32_process where name='"+pname+".exe'")
For each p In b
p.Terminate
Next
这是第二个方法
第一步:声明API函数以及一个关键常量WM_CLOSE:
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Const WM_CLOSE = &H10
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
首先定义一个变量用来存放那个VC程序的窗口句柄:
Dim hwnd_VC As Long
然后再用FindWindow函数获得句柄(假设窗口标题为title):
hwnd_VC=FindWindow(vbNullString,title)
最后用PostMessage函数关闭窗口:
PostMessage hwnd_VC,WM_CLOSE,0,0
完成关闭窗口的功能.
或者不知道标题时先声明一个ActiveX对象以获取进程ID:
Dim cim_obj As Object,p As Object,b As Object,pid As Long,pname As String,hproc As Long
'pname是进程名
Set cim_obj=GetObject("winmgmts://./root/cimv2")
Set b=cim_obj.execQuery("select * from win32_process where name='"+pname+".exe'")
For each p In b
p.Terminate
Next
这是第二个方法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询