Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Building a Network Service Object

This article will show you how to build a network service object that knows how to get network user name, connect a network drive, and disconnect a network drive.




Application Security Testing: An Integral Part of DevOps

bjects you build don't always have to be related to a database entity. In fact, some of the best objects are pure service objects that know how to talk to the registry, or other Windows service. This article will show you the code to build a network service object that knows how to:

1. Get network user name.
2. Connect a network drive.
3. Disconnect a network drive.

Each one of these features will be a public subroutine or function. The object will take care of error handling from the API calls by raising errors to the calling routine. While the sample code will have a basic error handler, you can expand it to check each of the error codes that the API calls might return. These error codes are documented in the MSDN library (both online and on CD-ROM), and the error code constants are defined in the API viewer. Just add them to your application and you'll be ready to go.

The first routine will be called AddConnection. The code for this routine is shown here:

Public Sub AddConnection(strLocalPath As String, _strNetworkPath As String, _ Optional strUserName As String = "", _Optional strPassword As String = "") Dim typNet As NETRESOURCE Dim lngReturn As Long With typNet .dwType = RESOURCETYPE_DISK .lpLocalName = strLocalPath .lpRemoteName = strNetworkPath .lpProvider = "" End With lngReturn = WNetAddConnection2(typNet, _strPassword, strUserName, 0) If lngReturn <> 0 Then Err.Raise vbObjectError, _"NetworkServices.AddConnection", _"Error #" & lngReturn & " occurred _during connection." End If End Sub

The routine is designed to accept a local path and a network path, and optionally, a user name and/or password to make the connection. As is required by the API call, we fill the NETRESOURCE structure with the information needed to make the connection. The constant used here is defined in the API Viewer. The error handler here is pretty basic, but could be expanded to provide text descriptions of all the errors that can occur from this API call. For now, we just raise the error and return.

The zero in the API call is a flag indicating whether the network connection should be remembered and reconnected during the next reboot. If you have a need to make a permanent connection, you can add another parameter to the method call to allow the caller to choose to save or not. The constant used here to save the connection is CONNECT_UPDATE_PROFILE.

The counterpart to this routine is DropConnection, which is somewhat simpler.

Public Sub DropConnection(strLocalPath As String, _Optional blnForce As Boolean = False) Dim lngReturn As Long lngReturn = WNetCancelConnection2 _(strLocalPath, 0, blnForce) If lngReturn <> 0 Then Err.Raise vbObjectError, _"NetworkServices.DropConnection", _ "Error #" & lngReturn & " occurred during _connection drop." End If End Sub

The blnForce parameter indicates whether Windows should force the connection closed if files are open. Be careful setting this parameter to True as it can cause most applications to go haywire if the file suddenly becomes unavailable. Other than that, the function is basically the same structure as before.

The third method allows you to retrieve the current network user name for the system, as well as any network resource.

Public Function UserName(Optional strLocalPath As String = "") Dim strBuffer As String * 255 Dim lngReturn As Long lngReturn = WNetGetUser(strLocalPath, strBuffer, 255) UserName = Trim(strBuffer) If lngReturn <> 0 Then Err.Raise vbObjectError, "NetworkServices.UserName", _ "Error #" & lngReturn & " occurred during request." End If

End Function

By setting the local path parameter, you can request the user name used to connect a given resource. If this is empty (the default), the current network user name is returned.

Calling any of the methods is done like so:

Dim objNet As NetworkServices Set objNet = New NetworkServices objNet.AddConnection "F:", "\\Server\C$" MsgBox objNet.UserName objNet.DropConnection "F:"

You can expand this object with other related functions, such as those used in the Common Dialog control (open file, save file, etc.). All the API calls are declared in the API Viewer and documented in the MSDN library.

Eric Smith has been a senior consultant with Andersen Consulting, specializing in client/server development using Oracle and Visual Basic, and was senior content developer (Visual Basic) at inquiry.com before it was purchased by Fawcette Technical Publications. He joined the Information Strategies team in September 1997 and is now an independent consultant. He has written or contributed to "Visual Basic 6 Bible" (IDG Books Worldwide, 1998), "Using Visual Basic 6" (Que), and "Inside VBScript with ActiveX" (New Riders Press, 1997). He is certified in Visual Basic 5.0 and Windows Architecture (1 and 2). Reach him here.
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date