Option Explicit
'''FUNCTION TO RUN SHELL'ED PROGRAMS WITH'''
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal Hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
'''GETTING HWND FUNCTIONALITY'''
Private Const GW_HWNDNEXT = 2
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal Hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal Hwnd As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal Hwnd As Long, ByVal wCmd As Long) As Long
'''DIM SOME STRING TO PICK UP THE WINDOW'S ID'''
Dim PID As Long
Private Const WM_CLOSE = &H10
'----'
'''FUNCTION HWND'''
Public Function GetHwnd(ByVal PID As Long) As Long
' Return the window handle for an instance handle.
Dim lHwnd As Long
Dim test_pid As Long
Dim Thread_ID As Long
' Get the first window handle.
lHwnd = FindWindow(vbNullString, vbNullString)
' Loop until we find the target or we run out
' of windows.
Do While lHwnd <> 0
' See if this window has a parent. If not,
' it is a top-level window.
If GetParent(lHwnd) = 0 Then
' This is a top-level window. See if
' it has the target instance handle.
Thread_ID = GetWindowThreadProcessId(lHwnd, test_pid)
If test_pid = PID Then
' This is the target.
GetHwnd = lHwnd
Exit Do
End If
End If
' Examine the next window.
lHwnd = GetWindow(lHwnd, GW_HWNDNEXT)
Loop
End Function
Private Sub CommandX_Click()
'PID = Shell("notepad.exe", vbHide)
PID = Shell("notepad.exe", vbNormalFocus)
End Sub
Private Sub Command2_Click()
' Close the other application
Dim lngHWND As Long
lngHWND = GetHwnd(PID)
If lngHWND Then
MsgBox "OK lngHWND = " & lngHWND
SendMessage lngHWND, WM_CLOSE, 0, 0 '''I GET AN ERROR HERE'''
Else
MsgBox "Error: lngHWND = " & lngHWND
End If
Private Sub Command1_Click()
'''CHECKS IF YOU HAVE SELECTED TO END THE PROGAM'''
If Combo1.Text = "CLOSE PROGRAM" Then
Unload Form1 '''ENDS THE PROGRAM'''
'''Also in the combo1's list I put, cmd.exe, notepad.exe and mspaint.exe'''
Else
'''IF NOT ENDING, THEN RUN THE CHOOSEN OR WRITTEN COMMAND'''
PID = Shell(Combo1.Text, vbNormalFocus)
'PID = ShellExecute 0, "OPEN", ProgramText.Text, "", "", 1
End If
End Sub