Guest Rhys2002 Posted May 10, 2005 Posted May 10, 2005 Any ideas how i can close exe's down when opened with shellexecute.Or is there another way i can open and close these exe's but keep the default exe directory path.Thanks again
Guest Chaser Posted May 10, 2005 Posted May 10, 2005 currently i don't have much time. but this is how it works: the shellexecute is able to give you back the handle of the started program:my_handle = shellexecute ....with this Handle you are able to shutdown the program!! do a google on handle + visual basic + exit - you will propably suceed - if not i have to look into it once more - may however take some days!
Guest Rhys2002 Posted May 10, 2005 Posted May 10, 2005 Sorted it now thanks for your replys. It was easy really didnt need to use ShellExecute.I just did this instead.ChDir LM2ServerShell ("C:\mirserver\mir200\m2server.exe")Thanks again.Just proves how somethings are so simple yet i never think of them.
Guest Chaser Posted May 10, 2005 Posted May 10, 2005 ? Didn't you want to shutdown the executed program later on?
Guest Rhys2002 Posted May 11, 2005 Posted May 11, 2005 Yes i did, but i could shutdown with normal SHELL command but couldnt figure out how to get the exe to run from the exe directory and not from the vb project directory.Thats why i went to shellexecute command.Sorry if im confusing u here.
Guest lorothrigs Posted May 19, 2006 Posted May 19, 2006 The "Kill" function deletes the file. Don't use it unless you want to delete a file...obviously.
Guest lorothrigs Posted May 19, 2006 Posted May 19, 2006 (edited) ugh...why is editing disabled for me?Anyways, here's something I made to help you.Option ExplicitPrivate Declare Function CreateToolhelp32Snapshot Lib "kernel32" (ByVal lFlags As Long, ByVal lProcessID As Long) As LongPrivate Declare Function Process32First Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As LongPrivate Declare Function Process32Next Lib "kernel32" (ByVal hSnapShot As Long, uProcess As PROCESSENTRY32) As LongPrivate Declare Function ProcessTerminate Lib "kernel32" Alias "TerminateProcess" (ByVal hProcess As Long, ByVal uExitCode As Long) As LongPrivate Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As LongPrivate Declare Sub CloseHandle Lib "kernel32" (ByVal hPass As Long)Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As LongConst FORMAT_MESSAGE_ALLOCATE_BUFFER = &H100Const FORMAT_MESSAGE_FROM_SYSTEM = &H1000Const LANG_NEUTRAL = &H0Const SUBLANG_DEFAULT = &H1Const TH32CS_SNAPHEAPLIST = &H1Const TH32CS_SNAPPROCESS = &H2Const TH32CS_SNAPTHREAD = &H4Const TH32CS_SNAPMODULE = &H8Const TH32CS_SNAPALL = (TH32CS_SNAPHEAPLIST Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD Or TH32CS_SNAPMODULE)Const TH32CS_INHERIT = &H80000000Const MAX_PATH As Integer = 260Const SW_SHOWNORM = 1Const SW_SHOWMIN = 2Const SW_SHOWMAX = 3Private Type ProcessInfo PName As String PID As Long iIndex As IntegerEnd TypePrivate Type PROCESSENTRY32 dwSize As Long cntUsage As Long th32ProcessID As Long th32DefaultHeapID As Long th32ModuleID As Long cntThreads As Long th32ParentProcessID As Long pcPriClassBase As Long dwFlags As Long szExeFile As String * MAX_PATHEnd TypeDim Process(99999) As ProcessInfoPrivate Sub cmdKillProcesses_Click() Dim i As Long Dim TheLoopingProcess Dim proc As PROCESSENTRY32 Dim snap As Long snap = CreateToolhelp32Snapshot(TH32CS_SNAPALL, 0) proc.dwSize = Len(proc) TheLoopingProcess = Process32First(snap, proc) i = -1 While TheLoopingProcess <> 0 i = i + 1 Process(i).PName = Left$(proc.szExeFile, IIf(InStr(1, proc.szExeFile, Chr$(0)) > 0, InStr(1, proc.szExeFile, Chr$(0)) - 1, 0)) Process(i).PID = proc.th32ProcessID Process(i).iIndex = i TheLoopingProcess = Process32Next(snap, proc) If Process(i).PName = "explorer.exe" Then KillProcessById (Process(i).PID) 'Files to kill here Wend CloseHandle snapEnd SubPublic Sub KillProcessById(p_lngProcessId As Long) Dim lnghProcess As Long Dim lngReturn As Long lnghProcess = OpenProcess(1&, -1&, p_lngProcessId) lngReturn = ProcessTerminate(lnghProcess, 0&)End SubPrivate Sub cmdStartProcesses_Click() StartProgram ("C:\WINDOWS\explorer.exe") 'File Path hereEnd SubPrivate Function StartProgram(ByVal sFilePath As String) Call ShellExecute(&O0, "Open", sFilePath, vbNullString, vbNullString, SW_SHOWNORM)End Function Edited May 19, 2006 by lorothrigs
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now