closing a file using Api ?
Pls help me
Thank you
Printable View
closing a file using Api ?
Pls help me
Thank you
i mean ending the program like doing a (end task from a Ctrl_Alt_Del command)
up !
VB 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, "Caption Of Window To Be Closed") PostMessage CloseIt, WM_CLOSE, CLng(0), CLng(0) End Sub
if i wanna close c:\windows\notepad.exe
how should i do it ?
can anyone show me a example on
using api to close C:\windows\notepad.exe ??
pls
thank you
Hack showed you one way. This works for Notepad open with an untitled document.
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.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
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