Implement "net use" in visual basic. ( Shell )

EE Team,

I am trying to implement "net use" functionality using vb.

This is what i want.

1. Use net use to check if there is a connection already available. If available kill that connection.
2. Use net use to make a new connection to that resource.
3. Use net use to Disconnect the connection from that resource.

Key : I want to implement using the shell command. and should get any dos popup's.

Also, I tried to implement this using WSAddConnection ( mpr.dll ) . its working. i dont want to use this. i want to use the "shell"

Let me know any weblinks or your views.

Thanks.
LVL 2
Apu_ShahAsked:
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.

vinnyd79Commented:
Here is an example that uses a modified shell function called ShellWait. The function works exactly as Shell. Try pasting this onto a form with a command button,then modify the drive letter and sharename under the command button click event:


Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function GetExitCodeProcess Lib "Kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long
Private Declare Sub Sleep Lib "Kernel32" (ByVal dwMilliseconds As Long)
Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400

Private Function ShellWait(PathName, Optional WindowStyle As VbAppWinStyle = vbNormalFocus) As Double
Dim hProcess As Long, RetVal As Long
    hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(PathName, WindowStyle))
    Do
        GetExitCodeProcess hProcess, RetVal
        DoEvents: Sleep 100
    Loop While RetVal = STILL_ACTIVE
End Function


Private Function CheckConn(DriveLetter As String) As Boolean
Dim TmpFile As String, ff As Integer, Ln As String
TmpFile = Environ("Temp") & "\TmpConn.tmp"
ShellWait Environ("ComSpec") & " /c net use > " & TmpFile, vbHide
ff = FreeFile
Open TmpFile For Input As #ff
Do Until EOF(ff)
Line Input #ff, Ln
If InStr(Ln, DriveLetter & ":") > 0 Then
    CheckConn = True
    Close #ff
    Kill TmpFile
    Exit Function
End If
Loop
Close #ff
Kill TmpFile
CheckConn = False
End Function

Private Sub Command1_Click()
Dim DriveLetter As String
DriveLetter = "P"
' check if share exists
If CheckConn(DriveLetter) = True Then
    ' if so, delete it
    ShellWait Environ("ComSpec") & " /c net use " & DriveLetter & ": /del", vbHide
End If
' remap it
ShellWait Environ("ComSpec") & " /c net use " & DriveLetter & ": \\server\sharename", vbHide

End Sub
0
Apu_ShahAuthor Commented:
First of all thank you for the code,

The code is working like a charm, but i have two buttons, connect and disconnect.

How would i disconnect from a n/w drive.

Let me know, Thanks
0
Apu_ShahAuthor Commented:
vinnyd79

I implemented the disconnect feature, what i need is to pass user id and password thru code.

Please give me an example.

Thanks.
0
vinnyd79Commented:
Give this a try:

Dim User As String
Dim Passw As String
Dim Domain As String

User = "vinnyd"
Passw = "MyPassword"
Domain = "MyDomain"

ShellWait Environ("ComSpec") & " /c net use " & DriveLetter & ": \\server\sharename " & Passw & " /user:" & Domain & "\" & User, vbHide

0

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
Apu_ShahAuthor Commented:
Thanks, i am all set.
0
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
Microsoft Development

From novice to tech pro — start learning today.