Results 1 to 7 of 7

Thread: how do i close a file using Api

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    12

    how do i close a file using Api

    closing a file using Api ?

    Pls help me

    Thank you

  2. #2

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    12
    i mean ending the program like doing a (end task from a Ctrl_Alt_Del command)

  3. #3

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    12
    up !

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    VB Code:
    1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    2. Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    3.  
    4. Private Const WM_CLOSE = &H10
    5.  
    6. Private Sub cmdCloseApp_Click()
    7. Dim CloseIt As Long
    8. CloseIt = FindWindow(vbNullString, "Caption Of Window To Be Closed")
    9. PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
    10. End Sub

  5. #5

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    12
    if i wanna close c:\windows\notepad.exe

    how should i do it ?

  6. #6

    Thread Starter
    New Member
    Join Date
    Jan 2002
    Posts
    12
    can anyone show me a example on
    using api to close C:\windows\notepad.exe ??

    pls

    thank you

  7. #7
    jim mcnamara
    Guest
    Hack showed you one way. This works for Notepad open with an untitled document.
    Code:
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Const WM_CLOSE = &H10
    
    Private Sub cmdCloseApp_Click()
    Dim CloseIt As Long
    CloseIt = FindWindow(vbNullString, "Untitled - Notepad")
    PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
    End Sub
    The other alternative is use EnumWindows with a callback routine to find the process that notepad has. Unless you really need an all-purpose Notepad killer, stick with the simple code above.
    using enumwindows:
    Code:
    'Add this code to a form
    Private Sub Form_Load()
        'KPD-Team 2000
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        'Set the form's graphics mode to persistent
        Me.AutoRedraw = True
        'call the Enumwindows-function
        EnumWindows AddressOf EnumWindowsProc, ByVal 0&
    End Sub
    'Add this code to a module
    Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Boolean
    Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
    Declare Function GetWindowTextLength Lib "user32" Alias "GetWindowTextLengthA" (ByVal hwnd As Long) As Long
    Public Function EnumWindowsProc(ByVal hwnd As Long, ByVal lParam As Long) As Boolean
        Dim sSave As String, Ret As Long
        Ret = GetWindowTextLength(hwnd)
        sSave = Space(Ret)
        GetWindowText hwnd, sSave, Ret + 1
        Form1.Print Str$(hwnd) + " " + sSave
        'continue enumeration
        EnumWindowsProc = True
    End Function

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