0

My application starts openvpn from the command line like this:

Dim cmdInfo As New ProcessStartInfo("cmd", "/K openvpn.exe " & Chr(34) & ovpnPath & Chr(34))
cmdInfo.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(cmdInfo)

This works great but I have no way of closing openvpn gracefully. I could kill the process but then it doesn't remove the routes etc.

Now I'm looking for a way to send Ctrl+C or F4 to my hidden cmd window to make OpenVPN exit.

Niel
  • 131
  • 1
  • 11
  • I'm not sure if this will work when you hide the window, but you could try getting the `Process` object returned from `Process.Start()` and use its `MainWindowHandle` property as `hWnd` as described in this answer of mine: https://stackoverflow.com/a/49603724/3740093 (you'd have to change the key combinations, and you don't need to send them twice). – Visual Vincent May 12 '18 at 22:31
  • Works when the window is not hidden only. Thanks for the suggestion though. – Niel May 13 '18 at 00:36

1 Answers1

0
Dim opwnfilepath As String = "Opvn file path"

      Dim Params As String = " --config """ & opwnfilepath & """
            Dim p As New ProcessStartInfo
            p.FileName = "C:\Program Files\OpenVPN\bin\openvpn.exe" ' add here openvpn.exe path 
            p.Arguments = Params
            p.WindowStyle = ProcessWindowStyle.Hidden

            Process.Start(p)

I use this code work good. if need show what happend in cmd change ProcessWindowStyle.Hidden to ProcessWindowStyle.Normal

Sorry I understand wrong :D

if you close openvpn in task manager , network gone back to your own

try this code

   Dim arrProcessx() As Process = System.Diagnostics.Process.GetProcessesByName("openvpn")

        For Each psx As Process In arrProcessx
            psx.Kill()
        Next

tested work for me.

  • Thanks for your answer. However, this does the same as my code. I need to be able to send "Ctrl+C" or "F4" to make openvpn exit. – Niel May 12 '18 at 22:32
  • sorry I understand wrong now I add new code for kill openvpn. I use this code and work good. – Robinex Robin May 12 '18 at 22:36
  • This way openvpn will not disconnect properly and clean up the routes etc – Niel May 13 '18 at 00:22