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
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
Public Function UnMapDrive(ByVal DriveLetter As String) As Boolean
Dim rc As Integer
rc = WNetCancelConnection2(DriveLetter & ":", 0, ForceDisconnect)
If rc = 0 Then
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MapDrive("G", "\\fileserver.tld.com\users\" & TextBox1.Text & "\")
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click