|
-
Aug 21st, 2000, 10:52 PM
#1
Thread Starter
Frenzied Member
Can someone give me the code for the api
called "Close"
Thanks!
-
Aug 21st, 2000, 10:59 PM
#2
Close what?
a program by its hwnd?
-
Aug 21st, 2000, 11:01 PM
#3
Thread Starter
Frenzied Member
No the api does it. and its called
close .. It minimises the hwnd named
-
Aug 21st, 2000, 11:04 PM
#4
Hyperactive Member
u can use the send_message API if u need to close a window.
[Edited by rammy on 08-22-2000 at 12:10 AM]
-
Aug 21st, 2000, 11:09 PM
#5
Code:
Private Declare Function CloseWindow Lib "user32" Alias "CloseWindow" (ByVal hwnd As Long) As Long
-
Aug 21st, 2000, 11:19 PM
#6
Here is how to close a Window:
Code:
Declare Function FindWindow Lib "user32" Alias _
"FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Declare Function PostMessage Lib "user32" Alias _
"PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, _
ByVal wParam As Long, lParam As Any) As Long
Public Const WM_CLOSE = &H10
Dim winHwnd As Long
Dim RetVal As Long
winHwnd = FindWindow(vbNullString, "WindowCaption")
Debug.Print winHwnd
If winHwnd <> 0 Then
RetVal = PostMessage(winHwnd, WM_CLOSE, 0&, 0&)
If RetVal = 0 Then
MsgBox "Error posting message."
End If
Else
MsgBox "The Window is not open."
End If
If you want to minimize it:
Code:
Declare Function ShowWindow Lib "User" (ByVal hWnd As Integer, ByVal nCmdShow As Integer) As Integer
Public Const SW_MINIMIZE = 6
Dim winHwnd As Long
winHwnd = FindWindow(vbNullString, "WindowCaption")
If winHwnd = 0 Then
Msgbox "Window Not Found!", 16
Else
X = ShowWindow(winHwnd , SW_MINIMIZE)
End If
[Edited by Matthew Gates on 08-22-2000 at 12:21 AM]
-
Aug 21st, 2000, 11:21 PM
#7
What is the difference Between SendMessage and PostMessage ???
-
Aug 22nd, 2000, 02:08 PM
#8
Good question. Don't know about PostMessage, but SendMessage sends a message to the application asking it to close. I guess PostMessage posts a message instead of sending one .
But here is another way to kill an application by it's path.
And it works very well.
Code:
Declare Function ProcessFirst Lib "kernel32" Alias "Process32First" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function ProcessNext Lib "kernel32" Alias "Process32Next" (ByVal hSnapshot As Long, uProcess As PROCESSENTRY32) As Long
Declare Function CreateToolhelpSnapshot Lib "kernel32" Alias "CreateToolhelp32Snapshot" (ByVal lFlags As Long, lProcessID As Long) As Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Type PROCESSENTRY32
dwSize As Long
cntUsage As Long
th32ProcessID As Long
th32DefaultHeapID As Long
th32ModuleID As Long
cntThreads As Long
th32ParentProcessID As Long
pcPriClassBase As Long
dwFlags As Long
szexeFile As String * MAX_PATH
End Type
Public Function KillApp(myName As String) As Boolean
Const PROCESS_ALL_ACCESS = 0
Dim uProcess As PROCESSENTRY32
Dim rProcessFound As Long
Dim hSnapshot As Long
Dim szExename As String
Dim exitCode As Long
Dim myProcess As Long
Dim AppKill As Boolean
Dim appCount As Integer
Dim i As Integer
On Local Error GoTo Finish
appCount = 0
Const TH32CS_SNAPPROCESS As Long = 2&
uProcess.dwSize = Len(uProcess)
hSnapshot = CreateToolhelpSnapshot(TH32CS_SNAPPROCESS, 0&)
rProcessFound = ProcessFirst(hSnapshot, uProcess)
Do While rProcessFound
i = InStr(1, uProcess.szexeFile, Chr(0))
szExename = LCase$(Left$(uProcess.szexeFile, i - 1))
If Right$(szExename, Len(myName)) = LCase$(myName) Then
KillApp = True
appCount = appCount + 1
myProcess = OpenProcess(PROCESS_ALL_ACCESS, False, uProcess.th32ProcessID)
AppKill = TerminateProcess(myProcess, exitCode)
Call CloseHandle(myProcess)
End If
rProcessFound = ProcessNext(hSnapshot, uProcess)
Loop
Call CloseHandle(hSnapshot)
Finish:
End Function
Usage:
Call KillApp("C:\Apppath\app.exe")
-
Aug 22nd, 2000, 03:14 PM
#9
Good question. Don't know about PostMessage, but SendMessage sends a message to the application asking it to close. I guess PostMessage posts a message instead of sending one .
I know what SendMessage Does. 
does anybody else have a clue to what PostMessage does? or how its different?
-
Aug 22nd, 2000, 03:24 PM
#10
PostMessage posts a message in the message queue and returns without waiting for the thread to process the message, whereas SendMessage waits until the message is processed then returns.
To minimize a window, use the CloseWindow API
Code:
Private Private Declare Function CloseWindow Lib "user32" Alias "CloseWindow" (ByVal hwnd As Long) As Long
Private Sub Command1_Click()
CloseWindow hwnd
End Sub
-
Aug 22nd, 2000, 04:00 PM
#11
Monday Morning Lunatic
So there isn't any way to fetch the result when using PostMessage?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|