Använder du dig av API'n så får du först ta reda på handle (hWnd) programmet du vill stänga har. Kan göras på olika sätt här är ett: det jag vill göra är att kunna stänga av ett program som redan är igång.. Så här kanske?Sv: stänga av prog.
http://www.allapi.net/apilist/example.php?example=EnumWindows
<code>
Option Explicit
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_SYSCOMMAND = &H112
Private Const SC_CLOSE = &HF060&
Private Sub Command1_Click()
Dim lngDummy As Long
lngDummy = SendMessage(Me.hwnd, WM_SYSCOMMAND, SC_CLOSE, 0&)
'Eller så här
lngDummy = Shell("NOTEPAD.EXE", 1)
AppActivate lngDummy
SendKeys "%{F4}", True
End Sub
</code>Sv: stänga av prog.
(C:\LCD smartie\smartie.exe).. därför att datorn inte kan stänga av sig när det programmet är igång.. då vill jag skapa ett prog. i vb som först stänger av det och sen länkar till windows\system 32\shutdown.exe
kan man fixa det på något sätt??Sv: stänga av prog.
<code>
Option Explicit
Private Declare Function EnumProcesses Lib "psapi.dll" (lpidProcess As Long, ByVal cb As Long, cbNeeded As Long) 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 EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, lphModule As Long, ByVal cb As Long, lpcbNeeded As Long) As Long
Private Declare Function GetModuleBaseName Lib "psapi.dll" Alias "GetModuleBaseNameA" (ByVal hProcess As Long, ByVal hModule As Long, ByVal lpBaseName As String, ByVal nSize 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 Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Const EWX_SHUTDOWN = 1
Private Const EWX_FORCE = 4
Private Const PROCESS_ALL_ACCESS As Long = 2035711
Private Const MaxItems As Long = 4096
Private Sub Command1_Click()
Dim ProcessIDs(0 To MaxItems - 1) As Long
Dim PIDBytesUsed As Long
Dim ModuleHandles(0 To MaxItems - 1) As Long
Dim ModBytesUsed As Long
Dim i As Long
Dim hProcess As Long
Dim ModuleName As String
Dim BuffSize As Long
Dim lngDummy As Long
'Lista alla processers ID
If EnumProcesses(ProcessIDs(0), 4 * MaxItems, PIDBytesUsed) Then
BuffSize = 260
ModuleName = String(BuffSize, 1)
'Loop igenom alla processer
For i = 0 To PIDBytesUsed \ 4
hProcess = OpenProcess(PROCESS_ALL_ACCESS, False, ProcessIDs(i))
If hProcess Then
If EnumProcessModules(hProcess, ModuleHandles(0), 4 * MaxItems, ModBytesUsed) Then
'Vilken process vill vi ta död på
If LCase$(Left$(ModuleName, GetModuleBaseName(hProcess, ModuleHandles(0), ModuleName, BuffSize))) = "smartie.exe" Then
'Bye-bye baby
Call TerminateProcess(hProcess, 0)
End If
End If
Call CloseHandle(hProcess)
End If
Next i
End If
If MsgBox("Good morning, Mr. Tyler. Going doooown?", vbQuestion + vbYesNo, "Shut Down") = vbOK Then
lngDummy = ExitWindowsEx(EWX_FORCE Or EWX_SHUTDOWN, 0)
End If
End Sub
</code>