[Webinar] Streamline your web hosting managementRegister Today

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 656
  • Last Modified:

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.

  • 3
  • 2
1 Solution
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

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))
        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
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
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
Apu_ShahAuthor Commented:

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

Please give me an example.

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

Apu_ShahAuthor Commented:
Thanks, i am all set.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now