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.

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.

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
            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
            Return False
        End If
    End Function
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MapDrive("G", "\\\users\" & TextBox1.Text & "\")
        MapDrive("H", "\\\common\")
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    End Sub
End Class

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.....
NCTETechAuthor Commented:
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.
Sorry I dont have a VB ping script to hand. There are plenty on the Net, keep pinging in a loop until you get a reply code then run the rest of your script.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.