|
-
Aug 28th, 2001, 06:07 PM
#1
Thread Starter
New Member
FindWindow help!
hello,
i want to use FindWindow in conjunction with PostMessage to be able to write a Closewindow type function. However, I really really don't understand either of the arguments you pass to FindWindow. Using the window title is unreliable to me, since it changes with each document that you open. (I'm trying to close something like Microsoft Word). And I don't know what the other argument (the first?) is. can anybody help me?
thx
j
-
Aug 28th, 2001, 06:48 PM
#2
It's the classname, which is a constant (unlike the title which is variable). You can get the classname of an App, by using a tool such as Spy++. I believe the classname for Word is OpusApp.
-
Aug 28th, 2001, 08:46 PM
#3
Registered User
To close an application, once you have the hwnd, use this code:
VB Code:
Option Explicit
Private Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As Long, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long
Private Const SC_CLOSE = &HF060&
Private Const WM_SYSCOMMAND = &H112
Private Declare Function IsWindow Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Declare Function TerminateProcess& Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long)
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Sub CloseWindowByHwnd(ByVal hwnd&)
'Nucleus
Dim lPid As Long
Dim lHp As Long
SendMessageTimeout hwnd, WM_SYSCOMMAND, SC_CLOSE, 0, 0, 500, 0
'
' If the window doesn't like gentle persuasion, bring out the nipple clamps to force it to close
'
If IsWindow(hwnd) Then
Call GetWindowThreadProcessId(hwnd, lPid)
lHp = OpenProcess(PROCESS_ALL_ACCESS, 0&, lPid)
TerminateProcess lHp&, 0&
CloseHandle lHp
End If
'
End Sub
-
Aug 29th, 2001, 12:59 PM
#4
Thread Starter
New Member
thx nucleus and megatron...
nucleus, the postmessage method i've seen is shorter than yours, but yours seems more thorough-- i'll check it out.
and another question--
i just realized that ShellExecute returns a value-- is the return value the same value (ie the windowhandle) returned by FindWindow?
thx
j
-
Aug 29th, 2001, 05:54 PM
#5
Registered User
See here for more info if you want to return PID from shellexecute:
http://www.vbforums.com/showthread.p...ELLEXECUTEINFO
-
Aug 30th, 2001, 10:30 AM
#6
Assuming that no errors occur, ShellExecute returns the instance handle of the application that was run.
-
Aug 30th, 2001, 12:40 PM
#7
Thread Starter
New Member
megatron, so i should be able to use something like GetWindowTextLength with the ShellExecute return as an argument, right? But I'm running my test program line by line, and ShellExecute returns a value like 33 or 42, and passing that (which *is* the hwnd of the window resulting from ShellExecute, right? i'll feel so stupid if it's not) to GetWindowTextLength returns a 0, which shouldn't happen. what's wrong?
-
Aug 30th, 2001, 05:43 PM
#8
Registered User
*ahem* the link I posted shows you how to get the Process ID for ShellExecute which is the same as the number from Shell.
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
|