I wrote some code below to open a command prompt and initiate a VPN connection as a process.
If I pass in the correct credentials, it works great as is. If I pass in bad credentials, it processes all the way through and tells me it was successful when it wasn't. My code fails later when I try to pass data through the VPN connection. I really want to trap and stop earlier if possible.
Exit code of process is reading zero all the time.
Attempts to read ErrorDataReceived yields nothing
Attempts to read OutputDataReceived actual shows me something.
First event reads attempting connection
Second event reads invalid credentials
When I pass in good credentials,
first event reads attempting connection
second event reads connection successful
My question(s) are these:
1) Is there any way for my to trap and end the process by using Outputdata vs error?
2) Am I missing something in my code that should technically make this process wait?
Code:Private Sub ConnectNE() Dim pc As Process = New Process Dim pci As ProcessStartInfo = New ProcessStartInfo() txtMsg.Clear() With pci .WorkingDirectory = "c:\Program Files (x86)\SonicWALL\SSL-VPN\NetExtender\" .FileName = "cmd.exe" .Arguments = "/c NECLI.exe connect -s gblVariables.myNetwork -d gblVariables.myDomain -u " & gblVariables.myUKName & " -p " & gblVariables.myUKPswd .WindowStyle = ProcessWindowsStyle.Normal .CreateNoWindow = True .UseShellExecute = False .RedirectStandardOutput = True .RedirectStandardError = True End With pc.StartInfo = pci pc.EnableRaisingEvents = True AddHandler pc.OutputDataReceived, AddressOf myProcessError Dim myCount As Integer = 1 Try txtMsg.AppendText(String.Concat("-- ", "Netextender Initializing Connection", Environment.NewLine)) pc.Start() pc.BeginOutputReadLine() Do Until pc.HasExited txtMsg.AppendText(String.Concat("-- ", "Netextender Connecting " & myCount, Environment.NewLine)) pc.WaitForExit(5000) myCount = myCount + 1 Loop pc.Close() Catch ex As Exception txtMsg.AppendText(String.Concat("-- ", "Netextender Connection ERROR: " & ex.Message, Environment.NewLine)) pc.Close() End Try txtMsg.AppendText(String.Concat("-- ", "Netextender Connected Successfully", Environment.NewLine)) End Sub Private Sub myProcessError(ByVal sender as Object, ByVal e as DataReceivedEventAurgs) MsgBox(e.Data) End Sub




Reply With Quote
