||Posted - Dec 13 2006 : 06:13:30 AM
I want my VB6 App to start a mini server daemon (I'm trying tiny web atm) and then terminate it when my app closes. I can activate it with... TinyPid = Shell("tiny.exe html 8080", vbNormalNoFocus) ...but how do I then close it?
I found a number of lengthy solutions for this already, but they are all too general. I'm just wondering if there is an easy method (not kill!). I have the process ID after all. I take it I'll have to use API calls
Soz if discussed already
Edit.... I've found a solution
Sub KillByPid(ByRef ProcessId As Double, ByRef Result As Integer)
Dim strWQL As String
Dim objProcess As Variant
Dim objResult As Variant
Dim wmi As Variant
Set wmi = GetObject("winmgmts:")
strWQL = "select * from win32_process where ProcessId='" & ProcessId & "'"
Set objResult = wmi.ExecQuery(strWQL)
For Each objProcess In objResult
Result = objProcess.Terminate(0)
I can concieve a problem with this where the launched process self terminates and another process is started with the same PID, so I guess I will need to terminate it by PID and Name. I think I'm there as.... strWQL = "select * from win32_process where ProcessId='" & ProcessId & "' and name='" & NameStr & "'" ...seems to work. comments still very much appriciated as I'm not at all familiar with the winmgmts: object. I'm pretty sure it's a W2K+ only object.
||Posted - Jan 21 2007 : 12:21:26 PM
Cheers for the info, here's my even later response; funnily enough I'm using SendMessage loads between my main app and the mini CGI app the server calls to transfer the user data... it makes sense to use it to close the server as well. thanks
||Posted - Dec 27 2006 : 11:04:49 AM
Kinda late... But you could possibly get the window of the program when knowing the processid, and then use the SendMessage api to send a WM_QUIT message to the window. :)