Newsgroup: it.comp.lang.visual-basic
Titolo: Closing

Whats the best way to close your program? with out using the "X" ?
Unload still leave code in memory?   anyone know of a better way?
Also, anyone know how to terminate one app from another app?
Thanks!! :-)
Joe!

Also, anyone know how to terminate one app from another app?
several ways, listed from the "weakest" to the "strongest":
 - send Alt+F4 to the top window of the app (won't always work). This
   simulates a user pressing Alt+F4 to close the main window. This could
   require further user intervention
 - send WM_SYSCOMMAND message with a wParam of SC_CLOSE to the main window
   or thread. This will cause the app to "think" the user had just pressed
   the "X" button
 - send WM_CLOSE: does approx. the same of the previous
 - send WM_DESTROY then WM_NCDESTROY, or use DestroyWindow()
 - send WM_QUIT (this is better in case of windowless applications)
 - send WM_QUERYENDSESSION then WM_ENDSESSION, to make the app "think"
   that Windows is ending the work session, then it must close immediately
 - EndTask(): not documented. This is the same as closing the app from the
   CTRL+ALT+DEL task list. The declaration is Declare Function EndSession
   Lib "user32" (ByVal hWnd as Long, ByVal Unknown as Long) As Long. Don't
   use it since nobody has idea of how this works...
 - TerminateProcess(): very cruel. Immediately kills the application. Use 
   with caution [NOTE: in Windows NT this won't work if the target app
   had been started from another user - i.e. a different SID - or if the
   target app is a service] The "advantage" of this is that you don't ask
   the app to gently close, you just kill it
How to send a message:
 - SendMessage(): sends the message to a window and waits for reply
 - PostMessage(): sends the message to a window and immediately returns.
   Specify a hWnd (window handle) of HWND_BROADCAST to simulate a system-
   wide message
 - PostThreadMessage(): sends the message to the specified thread and
   immediately returns. Threads are kinda sub-processes. This is useful in
   case of windowless applications
See MSDN (
http://msdn.microsoft.com/
) for a complete discussion on the 
Windows API, code examples, and topics such as getting the handle to a 
process from the handle of one of its windows, or vice versa, or on how to 
enumerate threads, etc.

KJK :: Juliet ha scritto nel messaggio ...
[supersnip]
Ecco una bella FAQ per Jimi

OK, I've tried, but so long, no success.
I launch Lotus ScreenCam w/ CreateProcess(0&, cmdline, 0&, 0&, 1&,
NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc), then I've tried
SendMessage WM_SYSCOMMAND w/SC_CLOSE, PostCommand WM_CLOSE,
"GetExitCodeProcess proc.hProcess, uExitCode" + ExitProcess uExitCode
(and this closes my main app), and a couple of other ways, but the
only way I've found partially successfull is invoking
TerminateProcess(proc.hProcess, 1&), but this still leave something in
memory: in fact, when I re-create a process for ScreenCam, it finds
SCA32C10.DLL still loaded, so that I must press a "Retry" button in a
form displayed by ScreenCam to let it run, and this doesn't make me
happy, of course: is there some of you gurus who can help me?


SendMessage WM_SYSCOMMAND w/SC_CLOSE, PostCommand WM_CLOSE,
are you sure you're sending this to the correct window? always check 
(GetParent()) if the windows is at the top level in hierarchy
Then, simply DestroyWindow() it (this works for example with Notepad; 
don't try it on the VB IDE, it has a bizarre effect...)

Hi KJK and all you gurus, forgive me if I submit an obvious question.
The problem is, how can I know the right window handle (w/
CreateProcess I know just a process handle, and I'm not so expert to
know how I can get a window handle). Thanks for your kindness.
