Extend the Component
Actual coding examples for DFS seem remarkably sparse. But, a little experimentation quickly yielded declares for four of the DFS APIs: NetDfsAddStdRoot, NetDfsAddFtRoot, NetDfsAdd, and NetDfsRemove. The NetDfsAddFtRoot adds a new domain-based DFS root (or adds a new server and share to an existing domain-based DFS implementation). NetDfsAddStdRoot creates a root for a new stand-alone DFS implementation. TheNetDfsAdd and NetDfsRemove functions add and delete a DFS link respectively. Note that both the NetDfsAddStdRoot and NetDfsAddFtRoot are marked in the MSDN with the following warning:
Note: This function is currently being evaluated and may or may not be supported in future versions.
I've implemented the four functions above in a class (cDFS), along with a form to act as a test framework for exercising the functions. The methods in this class are fairly simple wrappers around the actual API calls. For example (see Figure 1), the CreateDfsRootStandAlone method calls the API:
Public Sub CreateDfsRootStandAlone
(ByVal xi_strServerName As String, _
ByVal xi_strRootShare As String,
Optional ByVal xi_strComment As String = "")
If Left$(xi_strServerName, 2) <> "\\" Then
xi_strServerName = "\\" & xi_strServerName
End If
p_lngRtn = NetDfsAddStdRoot(StrConv(xi_strServerName, _
vbUnicode), StrConv(xi_strRootShare, vbUnicode), _
StrConv(xi_strComment, vbUnicode), 0&)
If p_lngRtn <> NERR_Success Then
' Raise an error
End If
End Sub
The only interesting note here is that the server name must be prepended with dual backslashes.
From the UI side, the call is equally simple:
Private Sub cmdCreateStandAloneDfsRoot_Click()
m_objDFS.CreateDfsRootStandAlone Me.txtServerName.Text, _
Me.txtRootShare.Text, Trim$(Me.txtComment.Text)
End Sub
The AddDfsLink, while it has a little more code, is also fairly simple. I broke up the DfsEntryPath parameter for the API call into three variables (xi_strDomainName, xi_strDfsRootName, xi_strNewDfsSharename) because I found it confusing to enter as a combined string. In the procedure, I stitch them back together separated by backslashes. Another interesting note is that the documentation states that the server name needs to be prefixed by double backslashes, but it doesn't seem to work unless you don't include the backslashes.
Public Sub AddDfsLink(ByVal xi_strDomainName As String, _
ByVal xi_strDfsRootName As String, _
ByVal xi_strNewDfsShareName As String, _
ByVal xi_strServerName As String, _
ByVal xi_strSharename As String, _
Optional ByVal xi_strComment As String = "")
Dim p_strDfsEntryPath As String
If Left$(xi_strDomainName, 2) <> "\\" Then
xi_strDomainName = "\\" & xi_strDomainName
End If
If Left$(xi_strServerName, 2) = "\\" Then
xi_strServerName = Mid$(xi_strServerName, 3)
End If
p_strDfsEntryPath = xi_strDomainName & "\" & _
xi_strDfsRootName & "\" & _
xi_strNewDfsShareName
p_lngRtn = NetDfsAdd
(DFSEntryPath:=StrConv(p_strDfsEntryPath, vbUnicode), _
ServerName:=StrConv(xi_strServerName, vbUnicode), _
ShareName:=StrConv(xi_strSharename, vbUnicode), _
Comment:=StrConv(xi_strComment, vbUnicode), _
Flags:=DFS_ADD_VOLUME)
If p_lngRtn <> NERR_Success Then
' Raise an error
End If
End Sub
DFS can be a little quirky at times, and it isn't a general answer to all your administrative problems. But, it is a tool that can be useful to both administrators and users. Keeping up with hundreds of shares on multiple directories is, I have found, a totally non-rewarding occupation. The DFS helps, at least a little bit, with this annoyance. Also, allowing the users to find the shares they need quickly cuts down on some of the phone calls and messages.
L.J. Johnson is the owner of Slightly Tilted Software in Dallas, Texas. He has been programming in Visual Basic since 1.0 and is a Microsoft Most Valuable Professional (MVP). Besides being the Windows NT/2000 Pro, L.J. leads some of the Visual Basic discussions on DevX. Reach him at LJJohnson@SlightlyTiltedSoftware.com or visit his Web site at www.SlightlyTiltedSoftware.com.