利用Windows API的OpenProcess和CloseHandle函数来实现对被调用软件的检测: 1) 在VB中新建一个标准EXE工程; 2) 在Form1中声明OpenProcess和 CloseHandle 这两个Windows API 函数; Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long 3) 然后编写下面的函数: Function IsRunning(ByVal ProgramID) As Boolean ' 传入进程标识ID Dim hProgram As Long '被检测的程序进程句柄 hProgram = OpenProcess(0, False, ProgramID) If Not hProgram = 0 Then IsRunning = True Else IsRunning = False End If CloseHandle hProgram End Function 4) 在Form_Click()中加入代码: Sub Form_Click() Dim X Me.Caption = "开始运行" X = Shell("NotePad.EXE", 1) While IsRunning(X) DoEvents Wend Me.Caption = "结束运行" End Sub
如果是有窗口名的应用程序,可以用 enumwindow来判断以下是查找qq的消息窗体,并隐藏、使其透明的代码:'''form 代码: Private Sub Command1_Click() EnumWindows AddressOf EnumWindowsProc, ByVal 0&End Sub'Add this code to a form Private Sub Form_Load() 'Set the form's graphics mode to persistent Me.AutoRedraw = True 'call the Enumwindows-function End Sub'''模块代码: 'Add this code to a module Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPublic Const GW_HWNDFIRST = 0 Public Const GW_HWNDNEXT = 2 Public Const SW_HIDE = 0 Public Const SW_RESTORE = 9 Public Const WS_EX_LAYERED = &H80000 Public Const GWL_EXSTYLE = (-20) Public Const LWA_ALPHA = &H2 Public Const LWA_COLORKEY = &H1 Public Const WS_EX_TOOLWINDOW = &H80 Public Const WS_EX_APPWINDOW As Long = &H40000Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean Dim sSave As String, Ret As Long Ret = GetWindowTextLength(hwnd) sSave = Space(Ret) GetWindowText hwnd, sSave, Ret + 1 'Form1.Print Str$(hwnd) + " " + sSave If InStr(1, sSave, "TMPGEnc") Then MsgBox "find" ShowWindow hwnd, SW_HIDE
Dim rtn As Long rtn = GetWindowLong(hwnd, GWL_EXSTYLE) rtn = rtn Or WS_EX_LAYERED rtn = rtn Or WS_EX_TOOLWINDOW
'rtn = rtn And Not WS_EX_APPWINDOW SetWindowLong hwnd, GWL_EXSTYLE, rtn
ShowWindow hwnd, SW_RESTORE
SetLayeredWindowAttributes hwnd, 0, 100, LWA_ALPHA End End If 'continue enumeration EnumWindowsProc = True
1) 在VB中新建一个标准EXE工程;
2) 在Form1中声明OpenProcess和 CloseHandle 这两个Windows API 函数;
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal
bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
3) 然后编写下面的函数:
Function IsRunning(ByVal ProgramID) As Boolean ' 传入进程标识ID
Dim hProgram As Long '被检测的程序进程句柄
hProgram = OpenProcess(0, False, ProgramID)
If Not hProgram = 0 Then
IsRunning = True
Else
IsRunning = False
End If
CloseHandle hProgram
End Function
4) 在Form_Click()中加入代码:
Sub Form_Click()
Dim X
Me.Caption = "开始运行"
X = Shell("NotePad.EXE", 1)
While IsRunning(X)
DoEvents
Wend
Me.Caption = "结束运行"
End Sub
Private Sub Command1_Click()
EnumWindows AddressOf EnumWindowsProc, ByVal 0&End Sub'Add this code to a form
Private Sub Form_Load()
'Set the form's graphics mode to persistent
Me.AutoRedraw = True
'call the Enumwindows-function
End Sub'''模块代码:
'Add this code to a module
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function SetLayeredWindowAttributes Lib "user32" (ByVal hwnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As LongPublic Const GW_HWNDFIRST = 0
Public Const GW_HWNDNEXT = 2
Public Const SW_HIDE = 0
Public Const SW_RESTORE = 9
Public Const WS_EX_LAYERED = &H80000
Public Const GWL_EXSTYLE = (-20)
Public Const LWA_ALPHA = &H2
Public Const LWA_COLORKEY = &H1
Public Const WS_EX_TOOLWINDOW = &H80
Public Const WS_EX_APPWINDOW As Long = &H40000Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
Dim sSave As String, Ret As Long
Ret = GetWindowTextLength(hwnd)
sSave = Space(Ret)
GetWindowText hwnd, sSave, Ret + 1
'Form1.Print Str$(hwnd) + " " + sSave
If InStr(1, sSave, "TMPGEnc") Then
MsgBox "find"
ShowWindow hwnd, SW_HIDE
Dim rtn As Long
rtn = GetWindowLong(hwnd, GWL_EXSTYLE)
rtn = rtn Or WS_EX_LAYERED
rtn = rtn Or WS_EX_TOOLWINDOW
'rtn = rtn And Not WS_EX_APPWINDOW
SetWindowLong hwnd, GWL_EXSTYLE, rtn
ShowWindow hwnd, SW_RESTORE
SetLayeredWindowAttributes hwnd, 0, 100, LWA_ALPHA
End
End If
'continue enumeration
EnumWindowsProc = True
End Function