Results 1 to 9 of 9

Thread: ShellExecute API

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Location
    CWMBRAN,WALES,UK
    Posts
    146

    Question

    Hi all,
    As I,m not too familiar with API's, I'm having a problem closing Notepad from my app. I can create a new text file, write and save to it, then later open the file in Notepad.
    My question is...how do I close Notepad from my app.

    I've tried this:

    ShellExecute 0&, "Close", "C:\Programs\Accessories\Notepad"

    but its obviously wrong. Can anyone give me the correct code to close Notepad please.

    GRAHAM

  2. #2
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Angry Not much help

    I thought I waould at least tell you that my information tells me that Shell does not support a verb "close". Also, I don't think notepad provides this verb (although on my system I couldn't even find notepad in the registry so I gues it's not a COM object?)

    From a logical point of view, if you consider what Shell would be doing if you were able to get it to close an application, it would have to "guess" as to which running instance of notepad you want to close.

    I suspect you will need to use a combination of AppActivate and SendKeys to close the notepad instance. This will reply on you knowing the application title (what's in the title bar) and then the question, "What if there are two with the same title?"

    I don't know But someone else will

    This reply was intended to hopefully save you any more frustration on the Shell command...

    Hope it helps

    Paul Lewis

  3. #3
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633
    Code:
    Option Explicit
    
    Private Declare Function Findwindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) 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 Const WM_CLOSE = &H10
    
    Private Sub Form_Load()
      Dim lng_Dialog As Long
      
      'PURPOSE: Get the handle of an application by the classname
      lng_Dialog = Findwindow(vbNullString, "Untitled - Notepad")
      Call SendMessage(lng_Dialog, WM_CLOSE, 0, 0&)
    End Sub
    Instead of "Untitled - Notepad", put in your application's name. If you don't know what it is, just open it and right click on the Taskbar and select Task Manager. Or you can just look at the title on the upper left hand of the application. Before doing anything further, just open a regular Notepad session and run this code against it.

    Make sure you don't misspell otherwise it won't be able to get the handle.

    [Edited by Nitro on 07-03-2000 at 07:38 AM]
    Chemically Formulated As:
    Dr. Nitro

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Location
    CWMBRAN,WALES,UK
    Posts
    146
    Thanks anyway Paul, and thanks for the code Nitro.

    Am I right in assuming that the ShellExecute API is only used to open an application and does not support closing.
    It seems strange to have to find the open Window, then send a Close message to it.

    Nitro,is there a single API that will handle both Open and Close, just to simplify matters, or doesn't one exist. There are so many API calls, its hard to be familiar with most of them

    Thanks again
    GRAHAM

    Edit
    (Thats strange...it didn't wordwrap??)

    [Edited by GRAHAM on 07-03-2000 at 07:59 AM]

  5. #5
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431
    GRAHAM's solution will probably do the job for you because you most likely know the exact Notepad caption, but just in case you don't, you can do the following. Put this code in a module.
    Code:
    Option Explicit
    
    Public g_bFound As Boolean
    Public Const WM_CLOSE = &H10
    Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    Public Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Public Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
    Public Function EnumProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
    
        Dim sString As String * 255
        
        Call GetWindowText(hwnd, ByVal sString, 255)
        If left(sString, 1) <> Chr(0) Then
            sString = left(sString, InStr(sString, Chr(0)) - 1)
            If InStr(sString, "Notepad") Then
                Call SendMessage(hwnd, WM_CLOSE, 0, 0&)
                g_bFound = True
            End If
        End If
        
        EnumProc = hwnd
        
    End Function
    Then call the routine with this.
    Code:
        Do Until g_bFound
            Call EnumWindows(AddressOf EnumProc, 0&)
            DoEvents
        Loop

  6. #6
    Fanatic Member
    Join Date
    Jan 2000
    Location
    Nitro
    Posts
    633

    Smile

    GRAHAM, I don't know any API that Open and Close a file. I don't think it exist but someone might be able to enlighten my knowledge on this.

    As for the word wrap, it is because I place the code. Once there is code in a thread, it no longer wraps. This is what Denniswrenn told me.

    Have a nice day!
    Chemically Formulated As:
    Dr. Nitro

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Location
    CWMBRAN,WALES,UK
    Posts
    146

    Thumbs up

    Right, thats why the wordwrap didn't work. Thanks for the help Nitro.

    Thanks Martin, thats useful to know.
    I,ll add that to my collection of modules.

    Many thanks all
    GRAHAM

  8. #8
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    //

    ..I couldn't find one either..there is one
    shell that will close a dos window after the batch
    file has completed running but it doesn't pertain
    to exe as the command.com isn't involved...
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Feb 2000
    Location
    CWMBRAN,WALES,UK
    Posts
    146
    Thanks for looking Wayne.

    Strange isn't it... you'd think it would be logical to
    have a Shell call that supported Close as well as Open.

    GRAHAM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width