Results 1 to 1 of 1

Thread: Access a remote server's share folder using NET USE

  1. #1
    Addicted Member Sibby's Avatar
    Join Date
    Feb 01
    Location
    Milwaukee, WI *The United States of America*
    Posts
    144

    Access a remote server's share folder using NET USE

    PROBLEM:
    I wanted to logon to a remote (web) server programmatically, but I didn't want to give rights to each individual user that would run the program or make the user map a drive to the share folder on the server.

    SOLUTION:
    Created 1 master user account on the server and gave it full rights to the share folder. This prevents users from accessing files through explorer, forcing them to go through the program to do any kind of file maintenance. I then created the following function to connect/disconnect to/from the web server:

    VB Code:
    1. Public Function LogonServer(bLogon As Boolean) As Boolean
    2.     On Error GoTo Logon_Error
    3.    
    4.     LogonServer = True
    5.     If bLogon Then
    6.         'this creates a temporary (but hidden) share on the local machine to the web server
    7.         'passing the credidentials
    8.         Call ShellWait("net use \\servername\sharename masterpassword /user:domain\masterusername", 0)
    9.        
    10.         'test the connection, if it doesn't error, we're good
    11.         Call Dir$("\\servername\sharename\", vbDirectory)
    12.     Else
    13.         'removes the connection
    14.         Call ShellWait("net use \\servername\sharename /delete", 0)
    15.     End If
    16.    
    17.     Exit Function
    18. Logon_Error:
    19.     If Err.Number = 52 Then
    20.         'logon failed, return false
    21.         LogonServer = False
    22.         Resume Next
    23.     End If
    24. End Function

    Here's the ShellWait piece of this, slap this in a module:
    VB Code:
    1. Private Type STARTUPINFO
    2.     cb As Long
    3.     lpReserved As String
    4.     lpDesktop As String
    5.     lpTitle As String
    6.     dwX As Long
    7.     dwY As Long
    8.     dwXSize As Long
    9.     dwYSize As Long
    10.     dwXCountChars As Long
    11.     dwYCountChars As Long
    12.     dwFillAttribute As Long
    13.     dwFlags As Long
    14.     wShowWindow As Integer
    15.     cbReserved2 As Integer
    16.     lpReserved2 As Long
    17.     hStdInput As Long
    18.     hStdOutput As Long
    19.     hStdError As Long
    20. End Type
    21. Private Type PROCESS_INFORMATION
    22.     hProcess As Long
    23.     hThread As Long
    24.     dwProcessID As Long
    25.     dwThreadID As Long
    26. End Type
    27.  
    28. Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
    29.     hHandle As Long, ByVal dwMilliseconds As Long) As Long
    30.  
    31. Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
    32.     lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
    33.     lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
    34.     ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
    35.     ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
    36.     lpStartupInfo As STARTUPINFO, lpProcessInformation As _
    37.     PROCESS_INFORMATION) As Long
    38.  
    39. Private Declare Function CloseHandle Lib "kernel32" (ByVal _
    40.     hObject As Long) As Long
    41.  
    42. Public Function ShellWait(Pathname As String, Optional WindowStyle As Long)
    43.     Dim proc As PROCESS_INFORMATION
    44.     Dim start As STARTUPINFO
    45.     Dim ret As Long
    46.     ' Initialize the STARTUPINFO structure:
    47.     With start
    48.         .cb = Len(start)
    49.         If Not IsMissing(WindowStyle) Then
    50.             .dwFlags = &H1
    51.             .wShowWindow = WindowStyle
    52.         End If
    53.     End With
    54.     ' Start the shelled application:
    55.     ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
    56.             &H20&, 0&, 0&, start, proc)
    57.     ' Wait for the shelled application to finish:
    58.     ret& = WaitForSingleObject(proc.hProcess, -1& )
    59.     ret& = CloseHandle(proc.hProcess)
    60. End Function

    *************************
    I do not take credit for the SHELLWAIT function. The LogonServer function however I do.
    *************************
    Last edited by Sibby; Jan 12th, 2004 at 09:59 AM.
    If you can think it....you can code it....

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •