Option Explicit
Private Const CONNECT_UPDATE_PROFILE = &H1
Private Const RESOURCETYPE_DISK = &H1
Private Type NETRESOURCE
dwScope As Long
dwType As Long
dwDisplayType As Long
dwUsage As Long
lpLocalName As String
lpRemoteName As String
lpComment As String
lpProvider As String
End Type
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" _
(lpNetResource As NETRESOURCE, _
ByVal lpPassword As String, _
ByVal lpUserName As String, _
ByVal dwFlags As Long) As Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" _
(ByVal lpName As String, _
ByVal dwFlags As Long, _
ByVal fForce As Long) As Long
Figure 3 sGetFile
Public Function sGetFile(ByVal strDrive As String, _
ByVal strUserID As String, ByVal strPassword As String, _
ByVal strUNC As String, ByVal strDirectory As String, _
ByVal strFile As String) As Variant
Dim blnConnected As Boolean
Dim intFile As Integer
Dim strInLine As String
Dim strOutLine As String
Dim strTarget As String
Dim udtNetResource As NETRESOURCE
Dim lngRC As Long
'are we working with a UNC?
If strUNC <> "" Then
'setup the NETRESOURCE structure
udtNetResource.dwType = RESOURCETYPE_DISK
udtNetResource.lpLocalName = strDrive
udtNetResource.lpRemoteName = strUNC
'add the connection
lngRC = WNetAddConnection2(udtNetResource, strPassword, _
strUserID, CONNECT_UPDATE_PROFILE)
'make sure the connection request worked
If lngRC = 0 Then
'set connected flag
blnConnected = True
Else
'whoa! something went wrong
blnConnected = False
End If
Else
'no unc...automatically connected
blnConnected = True
End If
'make sure we have a connection
If blnConnected Then
'need to turn on error control for the Visual Basic calls
On Error GoTo GetFileERROR
intFile = FreeFile()
'put together the target path
strTarget = strDrive & strDirectory & strFile
Open strTarget For Input As #intFile
strOutLine = ""
Do While Not EOF(intFile)
Line Input #intFile, strInLine
strOutLine = strOutLine & strInLine & vbNewLine
Loop
Close #intFile
'now that we're done with the Visual Basic calls...
'turn off the error control
On Error Resume Next
sGetFile = strOutLine
'active UNC?
If strUNC <> "" Then
'disconnect from the resource
lngRC = WNetCancelConnection2(strDrive, CONNECT_UPDATE_PROFILE, &H1)
'how'd we do?
If lngRC <> 0 Then
'there was an error...send it back
sGetFile = Err.LastDllError & ":" & Err.Description
End If
End If
Else
'something happened during the connection request...
'send back bad return code
sGetFile = ""
End If
Exit Function
GetFileERROR:
'active UNC?
If strUNC <> "" Then
'disconnect from the resource
lngRC = WNetCancelConnection2(strDrive, CONNECT_UPDATE_PROFILE, &H1)
End If
'send back bad return code and error info
sGetFile = Err.Number & ":" & Err.Description
Exit Function
End Function
Figure 4 Creating Project1
Figure 6 Project1 in the Development Environment
Figure 7 GetFile.asp
<%@ LANGUAGE=VBScript %>
<%
Dim strMsg
'check to see if user submitted form
If Request.Form("ReadIt") <> "" Then
Dim objTemp
'create reference to object
Set objTemp = CreateObject("MINDSample.GetFileTest")
'get the file contents
strMsg = objTemp.sGetFile("W:","acme\mgr1","OgreaT1", _
"\\ACCT01\Sales","\","YTD_SALES_REPORT.txt")
'clean up...
Set objTemp = Nothing
Else
'default return msg
strMsg = ""
End If
%>
<html>
<body>
<%If strMsg <> "" Then%>
Return Message:<br>
<pre><%=strMsg%></pre><br>
<hr>
<br>
<%End If%>
<form action="GetFile.asp" method="post" id="form1" name="form1">
<input type="submit" name="ReadIt" value="Read File">
</form>
</body>
</html>
Figure 8 Error Handling
Dim strError As String
Dim lngErrX As Long
Dim lngErrorRC As Long
Dim strProvider As String
If lngRC = ERROR_EXTENDED_ERROR Then
'set up the return variables
strError = Space$(1024)
strProvider = Space$(255)
'get the additional info
lngErrorRC = WNetGetLastError(lngErrX, strError, 1024, strProvider, 255)
'did we get the additional info?
If lngErrorRC = NO_ERROR Then
'we got something...need to clean it up
strError = Left(strError, InStr(1, strError, Chr$(0), _
vbBinaryCompare) - 1)
'update error stuff
mlngErrorNum = Val(strError)
mstrErrorDesc = Mid(strError, InStr(strError, _
CStr(mlngErrorNum)) + Len(CStr(mlngErrorNum)) + 1)
Else
'send back previous error
mlngErrorNum = lngErr
mstrErrorDesc = strErrDesc
End If
End If