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

Posted on 2006-04-27
Last Modified: 2013-11-25
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.

Question by:Apu_Shah
    LVL 28

    Expert Comment

    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
    LVL 2

    Author Comment

    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
    LVL 2

    Author Comment


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

    Please give me an example.

    LVL 28

    Accepted Solution

    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

    LVL 2

    Author Comment

    Thanks, i am all set.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now