NCTETech
asked on
Launch application AFTER Cisco VPN client is successfully connected
I usually develop ASP.NET pages, so moving to Windows forms has left me with a couple of questions. I'll describe what I'm trying to achieve and then describe what I've already done.
GOAL:
Our remote users connect using the Cisco VPN Client for Windows. I want to automatically launch an EXE that I've written for mapping their drives once they successfully connect. Preferably, this would use Windows credentials that they used to connect to the VPN, but I can live with making them log in twice if necessary.
SO FAR:
I've written an EXE that deletes two mapped drives if they exist and then maps two drives after the user enters a valid username and password. I have also used the Application Launcher part of the Cisco VPN client to successfully execute this program. The problem is that the Application Launcher doesn't wait for the VPN connection to be connected prior to executing the program. Initially I wanted to try to make my program pause for a certain number of seconds prior to execution, but I wasn't able to figure this out and it is really a hack of a solution, so I wanted to turn to the experts to see if you can think of a way to handle this better. Even if my program launched underneath (i.e. my program would be behind the Cisco VPN Client and thus wouldn't be visible until the VPN Client was minimized), that would be fine. I'm just not sure how to force the focus to be on the VPN Client instead of my application.
I'm also totally open to any ideas on how to handle this better.
I've posted the code I'm using below. I've borrowed some from sources on the net, so you may recognize it.
GOAL:
Our remote users connect using the Cisco VPN Client for Windows. I want to automatically launch an EXE that I've written for mapping their drives once they successfully connect. Preferably, this would use Windows credentials that they used to connect to the VPN, but I can live with making them log in twice if necessary.
SO FAR:
I've written an EXE that deletes two mapped drives if they exist and then maps two drives after the user enters a valid username and password. I have also used the Application Launcher part of the Cisco VPN client to successfully execute this program. The problem is that the Application Launcher doesn't wait for the VPN connection to be connected prior to executing the program. Initially I wanted to try to make my program pause for a certain number of seconds prior to execution, but I wasn't able to figure this out and it is really a hack of a solution, so I wanted to turn to the experts to see if you can think of a way to handle this better. Even if my program launched underneath (i.e. my program would be behind the Cisco VPN Client and thus wouldn't be visible until the VPN Client was minimized), that would be fine. I'm just not sure how to force the focus to be on the VPN Client instead of my application.
I'm also totally open to any ideas on how to handle this better.
I've posted the code I'm using below. I've borrowed some from sources on the net, so you may recognize it.
Public Class Form1
Public Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
(ByRef lpNetResource As NETRESOURCE, ByVal lpPassword As String, _
ByVal lpUserName As String, ByVal dwFlags As Integer) As Integer
Public Declare Function WNetCancelConnection2 Lib "mpr" Alias "WNetCancelConnection2A" _
(ByVal lpName As String, ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer
Public Structure NETRESOURCE
Public dwScope As Integer
Public dwType As Integer
Public dwDisplayType As Integer
Public dwUsage As Integer
Public lpLocalName As String
Public lpRemoteName As String
Public lpComment As String
Public lpProvider As String
End Structure
Public Const ForceDisconnect As Integer = 1
Public Const RESOURCETYPE_DISK As Long = &H1
Public Function MapDrive(ByVal DriveLetter As String, ByVal UNCPath As String) As Boolean
Dim nr As NETRESOURCE
Dim strUsername As String
Dim strPassword As String
nr = New NETRESOURCE
nr.lpRemoteName = UNCPath
nr.lpLocalName = DriveLetter & ":"
strUsername = "tld\" & Me.TextBox1.Text '(add parameters to pass this if necessary)
strPassword = Me.TextBox2.Text '(add parameters to pass this if necessary)
nr.dwType = RESOURCETYPE_DISK
Dim result As Integer
result = WNetAddConnection2(nr, strPassword, strUsername, 0)
If result = 0 Then
Return True
Else
Return False
End If
End Function
Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
Dim rc As Integer
rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)
If rc = 0 Then
Return True
Else
Return False
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
UnMapDrive("H")
UnMapDrive("G")
MapDrive("G", "\\fileserver.tld.com\users\" & TextBox1.Text & "\")
MapDrive("H", "\\fileserver.tld.com\common\")
Me.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Me.Close()
End Sub
End Class
One method would be to have your script run in startup, ping the server your mapping drives to, basic loop until ping response is valid (after client connects) then run rest of your script - just a thought.....
ASKER
Yes, that is what I'd like to do, but I don't know how to do that. Sorry for the long delay in responding - I've been out of town.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.