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:
Public Function LogonServer(bLogon As Boolean) As Boolean
On Error GoTo Logon_Error
LogonServer = True
If bLogon Then
'this creates a temporary (but hidden) share on the local machine to the web server
'passing the credidentials
Call ShellWait("net use \\servername\sharename masterpassword /user:domain\masterusername", 0)
'test the connection, if it doesn't error, we're good
Call Dir$("\\servername\sharename\", vbDirectory)
Else
'removes the connection
Call ShellWait("net use \\servername\sharename /delete", 0)
End If
Exit Function
Logon_Error:
If Err.Number = 52 Then
'logon failed, return false
LogonServer = False
Resume Next
End If
End Function
Here's the ShellWait piece of this, slap this in a module:
VB Code:
Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type
Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long
Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal _
hObject As Long) As Long
Public Function ShellWait(Pathname As String, Optional WindowStyle As Long)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim ret As Long
' Initialize the STARTUPINFO structure:
With start
.cb = Len(start)
If Not IsMissing(WindowStyle) Then
.dwFlags = &H1
.wShowWindow = WindowStyle
End If
End With
' Start the shelled application:
ret& = CreateProcessA(0&, Pathname, 0&, 0&, 1&, _
&H20&, 0&, 0&, start, proc)
' Wait for the shelled application to finish:
ret& = WaitForSingleObject(proc.hProcess, -1& )
ret& = CloseHandle(proc.hProcess)
End Function
*************************
I do not take credit for the SHELLWAIT function. The LogonServer function however I do.
*************************