A question about shell but different question 
Author Message
 A question about shell but different question

Hello,

I have an other question.
I have an application in VB which call calc.exe in win98 with
shell('calc.exe',1)
Could you tell how I can do for close this calc.exe if I close my VB
application.

thank you for your help again.



Tue, 14 Sep 2004 20:53:28 GMT  
 A question about shell but different question
Since you started Calc using Shell(), you can obtain the handle to Calc
using this code ... http://www.mvps.org/vbnet/code/system/shellhwnd.htm.
Save the handle to a variable, and on exiting your app, use PostMessage to
send a WM_CLOSE message to Calc specifying the hwnd saved. Pass 0& as wparam
and ByVal 0& as lparam.

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


Quote:
> Hello,

> I have an other question.
> I have an application in VB which call calc.exe in win98 with
> shell('calc.exe',1)
> Could you tell how I can do for close this calc.exe if I close my VB
> application.

> thank you for your help again.



Wed, 15 Sep 2004 12:24:53 GMT  
 A question about shell but different question
excuse me I am beginner in VB and I dont understand :((
could you send me a example please you can help me.

Thank you a lot for your help



Quote:
> Since you started Calc using Shell(), you can obtain the handle to Calc
> using this code ... http://www.mvps.org/vbnet/code/system/shellhwnd.htm.
> Save the handle to a variable, and on exiting your app, use PostMessage to
> send a WM_CLOSE message to Calc specifying the hwnd saved. Pass 0& as
wparam
> and ByVal 0& as lparam.

> --

> Randy Birch
> MVP Visual Basic

> http://www.mvps.org/vbnet/
> Please respond only to the newsgroups so all can benefit.



> > Hello,

> > I have an other question.
> > I have an application in VB which call calc.exe in win98 with
> > shell('calc.exe',1)
> > Could you tell how I can do for close this calc.exe if I close my VB
> > application.

> > thank you for your help again.



Wed, 15 Sep 2004 16:29:51 GMT  
 A question about shell but different question
Randy,
out of interest, is there a problem using WM_DESTROY instead of
WM_CLOSE ?
Quote:

>Since you started Calc using Shell(), you can obtain the handle to Calc
>using this code ... http://www.mvps.org/vbnet/code/system/shellhwnd.htm.
>Save the handle to a variable, and on exiting your app, use PostMessage to
>send a WM_CLOSE message to Calc specifying the hwnd saved. Pass 0& as wparam
>and ByVal 0& as lparam.

>--

>Randy Birch
>MVP Visual Basic

>http://www.mvps.org/vbnet/
>Please respond only to the newsgroups so all can benefit.



>> Hello,

>> I have an other question.
>> I have an application in VB which call calc.exe in win98 with
>> shell('calc.exe',1)
>> Could you tell how I can do for close this calc.exe if I close my VB
>> application.

>> thank you for your help again.



Wed, 15 Sep 2004 17:23:16 GMT  
 A question about shell but different question
please give me a example I dont know where is this WM_DESTROY  and WM_CLOSE

thank you



Quote:
> Randy,
> out of interest, is there a problem using WM_DESTROY instead of
> WM_CLOSE ?


> >Since you started Calc using Shell(), you can obtain the handle to Calc
> >using this code ... http://www.mvps.org/vbnet/code/system/shellhwnd.htm.
> >Save the handle to a variable, and on exiting your app, use PostMessage
to
> >send a WM_CLOSE message to Calc specifying the hwnd saved. Pass 0& as
wparam
> >and ByVal 0& as lparam.

> >--

> >Randy Birch
> >MVP Visual Basic

> >http://www.mvps.org/vbnet/
> >Please respond only to the newsgroups so all can benefit.



> >> Hello,

> >> I have an other question.
> >> I have an application in VB which call calc.exe in win98 with
> >> shell('calc.exe',1)
> >> Could you tell how I can do for close this calc.exe if I close my VB
> >> application.

> >> thank you for your help again.



Wed, 15 Sep 2004 17:26:04 GMT  
 A question about shell but different question
This is what I use as a testbed for zapping Apps

Watch out for wrapping on the declares

Option Explicit

Private Declare Function FindWindow Lib "user32" Alias "FindWindowA"
(ByVal lpClassName As Long, ByVal lpWindowName As Long) As Long
Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long)
As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal
hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long,
ByVal wCmd As Long) As Long
Private Declare Function LockWindowUpdate Lib "user32" (ByVal hwndLock
As Long) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA"
( _
     ByVal hwnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Long, _
     lParam As Any) As Long
Private Declare Function OpenProcess Lib "kernel32" _
  (ByVal dwDesiredAccess As Long, _
   ByVal bInheritHandle As Long, _
   ByVal dwProcessId As Long) As Long
Private Declare Function TerminateProcess Lib "kernel32" _
  (ByVal hProcess As Long, _
   ByVal uExitCode As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" _
  (ByVal hObject As Long) As Long

Private Const WM_QUIT = &H12
Private Const WM_DESTROY = &H2
Private Const WM_CLOSE = &H10

Const GW_HWNDNEXT = 2
Dim mWnd As Long
Dim Pid As Long

' --- This gives a hWnd from a Process ID
Function InstanceToWnd(ByVal target_pid As Long) As Long
    Dim test_hwnd As Long, test_pid As Long, test_thread_id As Long
    'Find the first window
    test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
    Do While test_hwnd <> 0
        'Check if the window isn't a child
        If GetParent(test_hwnd) = 0 Then
            'Get the window's thread
            test_thread_id = GetWindowThreadProcessId(test_hwnd,
test_pid)
            If test_pid = target_pid Then
                InstanceToWnd = test_hwnd
                Exit Do
            End If
        End If
        'retrieve the next window
        test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
    Loop
End Function

Private Sub Form_Load()

    'KPD-Team 1999
    'URL: http://www.allapi.net/

    Dim S$

    'Execute LIST.COM
    'S$ = Environ("comspec") + " /c c:\utils\list.com *.exe"
    S$ = "notepad.exe"
    Pid = Shell(S$, vbNormalFocus)
    If Pid = 0 Then MsgBox "Error starting the app"
    'retrieve the handle of the window
    'we don't use it - but you may find a solution
    'and it is what you asked for
    mWnd = InstanceToWnd(Pid)
End Sub

' --- This is pretty drastic - but works
Private Sub Command1_Click()
    Dim hProcess&

    'hProcess = OpenProcess(0, False, Pid)
    'TerminateProcess hProcess, 0
    'CloseHandle hProcess
    ' WM_CLOSE (Randy Birch)
    Call SendMessage(mWnd, WM_CLOSE, 0, 0)  ' No use for dos apps
End Sub



Quote:
>please give me a example I dont know where is this WM_DESTROY  and WM_CLOSE

>thank you



>> Randy,
>> out of interest, is there a problem using WM_DESTROY instead of
>> WM_CLOSE ?


>> >Since you started Calc using Shell(), you can obtain the handle to Calc
>> >using this code ... http://www.mvps.org/vbnet/code/system/shellhwnd.htm.
>> >Save the handle to a variable, and on exiting your app, use PostMessage
>to
>> >send a WM_CLOSE message to Calc specifying the hwnd saved. Pass 0& as
>wparam
>> >and ByVal 0& as lparam.

>> >--

>> >Randy Birch
>> >MVP Visual Basic

>> >http://www.mvps.org/vbnet/
>> >Please respond only to the newsgroups so all can benefit.



>> >> Hello,

>> >> I have an other question.
>> >> I have an application in VB which call calc.exe in win98 with
>> >> shell('calc.exe',1)
>> >> Could you tell how I can do for close this calc.exe if I close my VB
>> >> application.

>> >> thank you for your help again.



Wed, 15 Sep 2004 18:34:07 GMT  
 A question about shell but different question
WM_CLOSE
The WM_CLOSE message is sent as a signal that a window or an application
should terminate. An application can prompt the user for confirmation, prior
to destroying a window, by processing the WM_CLOSE message and calling the
DestroyWindow function only if the user confirms the choice. By default, the
DefWindowProc function calls the DestroyWindow function to destroy the
window.

WM_DESTROY
The WM_DESTROY message is sent when a window is being destroyed. It is sent
to the window procedure of the window being destroyed after the window is
removed from the screen. This message is sent first to the window being
destroyed and then to the child windows (if any) as they are destroyed.
During the processing of the message, it can be assumed that all child
windows still exist.

So, to my reading, WM_CLOSE it the message one sends to request a shutdown,
whereas WM_DESTROY is the message an app receives after a close or quit has
been issued. Further, it seems, from the MSDN, that when by the time a
WM_DESTROY is received by an app it must be prepared to terminate gracefully
(ie save data, close files), IOW coding specifically to handle reacting to a
received a WM_DESTROY message. IIR, this is the last message the window gets
before it's nuked.  I'd be surprised if a VB form's unload event fired in
response to this message, for example.

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


Quote:
> Randy,
> out of interest, is there a problem using WM_DESTROY instead of
> WM_CLOSE ?


> >Since you started Calc using Shell(), you can obtain the handle to Calc
> >using this code ... http://www.mvps.org/vbnet/code/system/shellhwnd.htm.
> >Save the handle to a variable, and on exiting your app, use PostMessage
to
> >send a WM_CLOSE message to Calc specifying the hwnd saved. Pass 0& as
wparam
> >and ByVal 0& as lparam.

> >--

> >Randy Birch
> >MVP Visual Basic

> >http://www.mvps.org/vbnet/
> >Please respond only to the newsgroups so all can benefit.



> >> Hello,

> >> I have an other question.
> >> I have an application in VB which call calc.exe in win98 with
> >> shell('calc.exe',1)
> >> Could you tell how I can do for close this calc.exe if I close my VB
> >> application.

> >> thank you for your help again.



Wed, 15 Sep 2004 22:58:22 GMT  
 A question about shell but different question
ie: Req Close

or: Die

I must check this out on a variety of Apps - Thanks



Thu, 16 Sep 2004 03:12:08 GMT  
 A question about shell but different question


Quote:
>Since you started Calc using Shell(), you can obtain the handle to Calc

Would it be possible for the OP to use:-

    AppActivate "Calculator"
    'Now send ALT+F4
    SendKeys "%{F4}"

?

Quick, but maybe someone will tell me it's dirty too.

Regards.

--
Martin Trump



Thu, 16 Sep 2004 03:29:13 GMT  
 A question about shell but different question
Sure. But I detest apps that even momentarily steal focus away from the
current app.  And if the user had already closed it when appactivate was
called, vb throws a runtime error 5. Why not just save the hwnd and fire off
the message later. Poof...its gone.

--

Randy Birch
MVP Visual Basic

http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


Quote:


> >Since you started Calc using Shell(), you can obtain the handle to Calc

> Would it be possible for the OP to use:-

>     AppActivate "Calculator"
>     'Now send ALT+F4
>     SendKeys "%{F4}"

> ?

> Quick, but maybe someone will tell me it's dirty too.

> Regards.

> --
> Martin Trump



Thu, 16 Sep 2004 04:15:27 GMT  
 A question about shell but different question
Probably because that is almost exactly what MS told be to do - about
5 years ago when I 'phoned them and was desperate (deadlines)

The other reason is that it is (almost certainly) *not* what you would
do.  Abiguity ?

Oh and the word 'naff' comes to mind

On Sat, 30 Mar 2002 19:29:13 +0000, Martin Trump

Quote:



>>Since you started Calc using Shell(), you can obtain the handle to Calc

>Would it be possible for the OP to use:-

>    AppActivate "Calculator"
>    'Now send ALT+F4
>    SendKeys "%{F4}"

>?

>Quick, but maybe someone will tell me it's dirty too.

>Regards.

>--
>Martin Trump



Thu, 16 Sep 2004 05:22:16 GMT  
 A question about shell but different question

Quote:



> >Since you started Calc using Shell(), you can obtain the handle to Calc

> Would it be possible for the OP to use:-

>     AppActivate "Calculator"
>     'Now send ALT+F4
>     SendKeys "%{F4}"

> ?

> Quick, but maybe someone will tell me it's dirty too.

What if it whacks the wrong Calculator?  BTW, AppActivate's first
parameter is Variant for a reason:

 URL:http://msdn.microsoft.com/library/en-us/vbenlr98/html/vastmappactivat...

Of course, it'll still look ugly and might even eat keystrokes if the
user was trying to work with something else, perhaps with disastrous
consequences.  Once I was building a DEL command in a DOS box and some
damn thing popped up a MsgBox.  Before I noticed it, I had already hit
space, dismissing the box, typed a few more characters, and hit Enter,
nuking the wrong files entirely...

 URL:http://sysinternals.com/ntw2k/source/fundelete.shtml

 URL:http://winternals.com/products/repairandrecovery/filerestore.asp

--
Joe Foster <mailto:jlfoster%40znet.com>     On the cans? <http://www.xenu.net/>
WARNING: I cannot be held responsible for the above        They're   coming  to
because  my cats have  apparently  learned to type.        take me away, ha ha!



Thu, 16 Sep 2004 05:34:20 GMT  
 A question about shell but different question
ok ok but I dont have a solution :(
I am begining in vb please could you send me a sample please

Thank you for your help



Quote:
> Probably because that is almost exactly what MS told be to do - about
> 5 years ago when I 'phoned them and was desperate (deadlines)

> The other reason is that it is (almost certainly) *not* what you would
> do.  Abiguity ?

> Oh and the word 'naff' comes to mind

> On Sat, 30 Mar 2002 19:29:13 +0000, Martin Trump



> >>Since you started Calc using Shell(), you can obtain the handle to Calc

> >Would it be possible for the OP to use:-

> >    AppActivate "Calculator"
> >    'Now send ALT+F4
> >    SendKeys "%{F4}"

> >?

> >Quick, but maybe someone will tell me it's dirty too.

> >Regards.

> >--
> >Martin Trump



Thu, 16 Sep 2004 06:08:08 GMT  
 A question about shell but different question


Quote:
>What if it whacks the wrong Calculator?  BTW, AppActivate's first
>parameter is Variant for a reason:

> URL:http://msdn.microsoft.com/library/en-us/vbenlr98/html/vastmappactivat...

Perhaps better I bow out, I really don't understand this. I'm out of my
depth.

That URL tells me:-
====================================================
AppActivate Statement
See Also    Example    Specifics

Activates an application window.

Syntax

AppActivate title[, wait]

The AppActivate statement syntax has these named arguments:

Part Description
title Required. String expression specifying the title in the title bar
of the application window you want to activate. The task ID returned by
the Shell function can be used in place of title to activate an
application.
====================================================

First parameter a string? A variant??? I don't know but I hope I can
learn. TIA.

Regards.

--
Martin Trump



Thu, 16 Sep 2004 06:55:49 GMT  
 A question about shell but different question

Quote:

> ok ok but I dont have a solution :(
> I am begining in vb please could you send me a sample please


Try the following: 1 form, 1 module

If you try it not with Calculator but programs that load DLLs or open database connections etc.,
terminating it like this will cause bad things to happen.

--
MikeC

Please reply to the group.

Form: 2 Command buttons (Command1 and Command2)
-----------------------------------------------

Option Explicit

Private mhCalc As Double

Private Sub Command1_Click()
    mhCalc = StartProcess("Calc.Exe")
    Debug.Print mhCalc
End Sub

Private Sub Command2_Click()
    Debug.Print StopProcess(mhCalc, 0)
End Sub

Module
------

Option Explicit

Private Const NORMAL_PRIORITY_CLASS = &H20&

Private Type STARTUPINFO
    cb As Long
    lpReserved As String
    lpDesktop As String
    lpTitle As String
    dwX As Long
    dwY As Long
    dwXSize As Long
    dwYSize As Long
    dwXCountChars As Long
    dwYCountChars As Long
    dwFillAttribute As Long
    dwFlags As Long
    wShowWindow As Integer
    cbReserved2 As Integer
    lpReserved2 As Long
    hStdInput As Long
    hStdOutput As Long
    hStdError As Long
End Type

Private Type PROCESS_INFORMATION
    hProcess As Long
    hThread As Long
    dwProcessID As Long
    dwThreadID As Long
End Type

Private Declare Function CreateProcessA Lib "kernel32" (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, ByVal lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, lpStartupInfo As STARTUPINFO, lpProcessInformation As PROCESS_INFORMATION) As Long
Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, ByVal uExitCode As Long) As Long

Public Function StartProcess(ByVal sCommandLine As String) As Double
    Dim udtProcess As PROCESS_INFORMATION
    Dim udtStartup As STARTUPINFO
    Dim lReturn As Long

    ' Initialize the STARTUPINFO structure:
    udtStartup.cb = Len(udtStartup)

    ' Start the shelled application:
    lReturn = CreateProcessA(0&, sCommandLine, 0&, 0&, 1&, NORMAL_PRIORITY_CLASS, 0&, 0&, udtStartup, udtProcess)

    StartProcess = udtProcess.hProcess
End Function

Public Function StopProcess(ByVal hProcess As Double, Optional lExitCode As Long = 0) As Boolean
    StopProcess = (0 <> TerminateProcess(hProcess, lExitCode))
End Function



Thu, 16 Sep 2004 09:55:11 GMT  
 
 [ 17 post ]  Go to page: [1] [2]

 Relevant Pages 

1. A different Shell question

2. questions,questions,questions.

3. Atten: Larry Serflaten - Re: Dictionary Questions, Questions, Questions

4. Questions!Questions!Questions

5. Questions Questions Questions

6. HELP QUESTION HELP QUESTION HELP QUESTION

7. ORACLE AND ODBC A VERY DIFFERENT QUESTION

8. Slightly different Auto email question

9. Determining different borders: answer this question, you win a prize

10. different header/footer question w/ merged docs

11. Different Question about datasets

12. Scrolling TextBoxes - a different question

 

 
Powered by phpBB® Forum Software