-1

My program has to read a data from a server using socket programming to kill processes in a remote pc. After i convert the netstream from the socket connection, I convert the stream into String but I'm unable to kill the processes because the input string is not converted to string. I'm puzzled since I'm able to use string methods such as .remove(), but when i pass the input variable to the function killProcess the processess is not deleted.

'socket datastream
 If NetStream.DataAvailable Then
        Dim bytes(TCPClient.ReceiveBufferSize) As Byte
        Dim input As String
        NetStream.Read(bytes, 0, CInt(TcpClient.ReceiveBufferSize))
        lblConnectStatus.Text = ("Server was disconnected ")
        input = Encoding.ASCII.GetString(bytes)

     If input.StartsWith("kill") Then
        input = input.Remove(0, 4)
            Try
                KillProcess(input)
            Catch ex As Exception

            End Try
     End if
 End if

'kill process function
'this function works when i write the program name manually eg.:"notepad"
'but it doesn't work when i pass the parameter pid for the killProcess() function
Private Sub KillProcess(ByVal pid As String)
    For Each P As Process In System.Diagnostics.Process.GetProcessesByName(pid)
        P.Kill()
    Next
End Sub
  • 1
    Because a PID isn't a name? You need `Process.GetProcessById()`... I also don't quite see how the network part is relevant here. :) – CodeCaster May 31 '17 at 17:05
  • 1
    NET might be trying to tell you there is a problem, but you are swallowing any exceptions – Ňɏssa Pøngjǣrdenlarp May 31 '17 at 17:06
  • 1
    I don't know what you are sending, but it most likely is something like this: `"kill notepad"` right? If you then use `.Remove(0, 4)` on it the result will be: `" notepad"`. Notice something? There is a space at the beginning. You could get rid of that by using `input.Trim()`. – MrPaulch May 31 '17 at 17:09
  • @MrPaulch the comments in their code talk about a PID, so I guess they send something like "kill 123". They'll have to parse that string to an int, and plenty of questions here already deal with that. – CodeCaster May 31 '17 at 17:11
  • Judging from the comment above the method, he is sending process names. The name of the parameter is indeed misleading. So is the question. – MrPaulch May 31 '17 at 17:13
  • Thanks a lot. I've found the problem, actually the stream was converted into string but i only removed the first 4 characters of the string, since it was a TCP\IP string a lot of non relevant information was still kept in the string. I just had to put a flag a keep only the meaningful characters. Thanks a lot – Rodolfo Pedro Junior Jun 01 '17 at 05:33
  • @CodeCaster sorry for the careless naming of variables, actually it was supposed to be pname. Thanks for your guidance – Rodolfo Pedro Junior Jun 01 '17 at 05:38

1 Answers1

-2
Dim p() As Process
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick

    p = Process.GetProcessesByName("taskmgr")
    If p.Count > 0 Then
        Dim pList() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcessesByName("notepad")
        For Each proc As System.Diagnostics.Process In pList
            proc.Kill()

        Next

    End If
End Sub
  • 1
    What is this answer supposed to mean? It kills all instances of Notepad, if the Task Manager is running. I don't see how this answers the OP's question. – CodeCaster Jun 01 '17 at 05:47