Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Const WM_CLOSE = &H10 Public Const GW_HWNDNEXT = 2 Public Const GW_HWNDFIRST = 0Public Sub findrunningprogram() Dim RenHWND Dim HD RenHWND = GetWindow(Form1.hwnd, GW_HWNDFIRST) Do RetValue = GetWindowText(RenHWND, WinText, 256) If RetValue <> 0 Then WinTextLength = GetWindowTextLength(RenHWND) If Left$(WinText, WinTextLength) <> Form1.Caption And Left$(WinText, WinTextLength) <> App.Title Then Text = WinText If Text = "" Then HD = RenHWND End If End If End If RenHWND = GetWindow(RenHWND, GW_HWNDNEXT) Loop Until Text = "" findrunningprogram = HD End Sub HD的返回值就是Handle了!
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPublic Const WM_CLOSE = &H10
Public Const GW_HWNDNEXT = 2
Public Const GW_HWNDFIRST = 0Public Sub findrunningprogram()
Dim RenHWND
Dim HD
RenHWND = GetWindow(Form1.hwnd, GW_HWNDFIRST)
Do
RetValue = GetWindowText(RenHWND, WinText, 256)
If RetValue <> 0 Then
WinTextLength = GetWindowTextLength(RenHWND)
If Left$(WinText, WinTextLength) <> Form1.Caption And Left$(WinText, WinTextLength) <> App.Title Then
Text = WinText
If Text = "" Then
HD = RenHWND
End If
End If
End If
RenHWND = GetWindow(RenHWND, GW_HWNDNEXT)
Loop Until Text = ""
findrunningprogram = HD
End Sub
HD的返回值就是Handle了!
然后用这几个API函数
openprocess
terminateprocess
closehandle
我的程序是这样的:一个主程序(很小,是一个无标题的窗口),通过它可以调用和升级所有的子程序(exe文件),现在我想通过子程序升级主程序,那么就需要先将主程序关闭,所以才会提出问题。如果没有更好的方法,我只有复制一个临时文件到临时目录,然后做一个自动批处理文件放入Winstart或者Autoexec.bat里面,等到用户重新启动电脑的时候,将临时文件覆盖原来的主程序文件,再对主程序进行升级了。希望大家能够给出更好的办法,结帖截止日期:2003年7月10日!