PDA

Click to See Complete Forum and Search --> : how do i close a file using Api


StupidPanda
Jan 3rd, 2002, 04:11 AM
closing a file using Api ?

Pls help me

Thank you

StupidPanda
Jan 3rd, 2002, 04:30 AM
i mean ending the program like doing a (end task from a Ctrl_Alt_Del command)

StupidPanda
Jan 3rd, 2002, 04:42 AM
up !

Hack
Jan 3rd, 2002, 06:04 AM
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, "Caption Of Window To Be Closed")
PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0)
End Sub

StupidPanda
Jan 3rd, 2002, 08:05 AM
if i wanna close c:\windows\notepad.exe

how should i do it ?

StupidPanda
Jan 3rd, 2002, 08:56 AM
can anyone show me a example on
using api to close C:\windows\notepad.exe ??

pls

thank you

jim mcnamara
Jan 3rd, 2002, 09:28 AM
Hack showed you one way. This works for Notepad open with an untitled document.

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:

'Add this code to a form
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: KPDTeam@Allapi.net
'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