Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mapping a drive programmatically in VB 1

Status
Not open for further replies.

afryer

Programmer
Mar 9, 2004
207
GB
Hi all,

This is related to a previous query I had regarding embedding DOS sessions in a form, which I have realise I am probably not going to be able to do. What I need to do is the following:

- Attempt to map a drive to a remote machine (I know the user name and password)
- Determine whether or not this process has been successful and act accordingly.

At the moment, I am creating a new process, which contains the net use command I wish to process. I am using the CreateProcessA function to run it, which is fine and then attempt to read in each line as it is processed. I have used this on another DOS program and it works fine, but this one does. A segment of my code is as follows:

Code:
    ReturnValue = CreateProcessA(0&, p_command_line, 0&, 0&, True, _
                                NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
                                
    Do
        b_success = ReadFile(hReadPipe, l_buffer, 5000, l_bytes_read, 0&)
        
        ' if some data has been returned from the application and it indicates
        ' a failure then we need to set the return type to indicate this failure
        
        If (b_success = 1) Then
            execute_net_use = l_buffer
            Exit Function
        Else
            execute_net_use = "error"
            Exit Function
            
        End If
        
    Loop Until ReturnValue <> 258

excute_net_use is my function name and it seems to get stuck when calling the ReadFile function - I suspect this is because it gets no data it can read back and just hangs.

What I need to know is:

a) Is there any way of returning the output from a Net Use command
b) Or preferably a different way of mapping drives from the program and controlling whether it has succeeded or not.

Thanks in advance

Andrew
 
I did some testing and always got 1 as return code on success and failures. Return strings were always empty on failures, successes were "The command completed successfully." I had the same results under does by trying to output the response to a text file (just add ">c:\netret.txt" at the end of a NET USE command from command prompt to test).

Here's what I was playing with in code to test... note that it requires Windows Scripting Host so it may not be suitable for your environment.

Code:
Dim wshShell

Private Sub Form_Load()
Dim sCurPC, sPath, sLetter, sUser, sPass
sUser = "Administrator"
sPass = InputBox("Enter Password", "Enter Password")
sPath = "c$"
sCurPC = "10.0.1.20"
sLetter = "z:"
Set wshShell = CreateObject("WScript.Shell")

If isonline(sCurPC) Then
    If mapdrive(sCurPC, sPath, sLetter, sUser, sPass) <> "" Then
        MsgBox "Successfully mapped network drive " & sLetter
    Else
        MsgBox "Failed to map network drive."
    End If
Else
    MsgBox "Sorry, that host is not online."
End If


End Sub


Function mapdrive(sHost, sPath, sLetter, sUser, sPass)
        Set sRet = wshShell.Exec("net use " & sLetter & " \\" & sHost & "\" & sPath & " /USER:" & sUser & " " & sPass
        Do Until sRet.Status = 1: Loop
        mapdrive = sRet.stdout.readall
End Function


Function isonline(sHost)
    'Ping Ip Addresses
    Dim png
    Set png = wshShell.Exec("ping -n 1 -w 600 " & sHost)
    Do Until png.Status = 1: Loop
    strPing = png.stdout.readall

    'NOTE:  The string being looked for in the Instr is case sensitive.
    'Do not change the case of any character which appears on the
    'same line as a Case InStr.  As this will result in a failure.
    Select Case True
        Case InStr(strPing, "Request timed out") > 1
            strReply = "Request timed out"
            'strCname = lcase(getcName(strPing))
        Case InStr(strPing, "could not find host") > 1
            strReply = "Host not reachable"
            'strCname = lcase(getcName(strPing))
        Case InStr(strPing, "Unknown host") > 1
            strReply = "Unknown host"
            'strCname = lcase(getcName(strPing))
        Case InStr(strPing, "Reply from") > 1
            strReply = "Ping Successful"
            'strCname = lcase(getcName(strPing))
    End Select
    If strCname = "" Then
      strCname = "N/A"
    End If
    
    If LCase(strReply) = "ping successful" Then
        isonline = True
    Else
        isonline = False
    End If
    MsgBox strReply
    Set png = Nothing
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top