I'm using the NetDfsGetInfo example from pinvoke.net! to find the active unc path for a DFS target. If I call the sub DFSInfo with a DFS path which has two targets I get back that both targets are active. I have searched the internet and found a suggestion to replace the vbNull for calling the ServerName and the ShareName with vbNullString but this makes no difference.
Can someone explain me what I'm doing wrong? Here is the code I'm using:
Option Explicit On
'Option Strict On
Imports System
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Module modDFS
Declare Unicode Function NetDfsGetInfo Lib "NETAPI32.DLL" ( _
ByVal DfsEntryPath As String, _
ByVal ServerName As String, _
ByVal ShareName As String, _
ByVal Level As Integer, _
ByRef Buffer As IntPtr) As Integer
Declare Unicode Function NetApiBufferFree Lib "netapi32.dll" _
(ByVal buffer As IntPtr) As Long
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure _DFS_INFO_3
<MarshalAs(UnmanagedType.LPWStr)> Public EntryPath As String
<MarshalAs(UnmanagedType.LPWStr)> Public Comment As String
Public State As Int32
Public NumberOfStorages As Int32
Public storage As Int32
End Structure
<StructLayout(LayoutKind.Sequential, CharSet:=CharSet.Auto)> _
Public Structure _DFS_STORAGE_INFO
Public State As Integer
<MarshalAs(UnmanagedType.LPWStr)> Public ServerName As String
<MarshalAs(UnmanagedType.LPWStr)> Public ShareName As String
End Structure
Public Sub DFSInfo(ByVal DfsEntryPath)
Dim DfsInfo3 As New _DFS_INFO_3
Dim DfsStorageInfo As New _DFS_STORAGE_INFO
Dim err_code As Integer
Dim bufPtf As IntPtr
Dim bufPtf2 As IntPtr
err_code = NetDfsGetInfo(DfsEntryPath, vbNullString, vbNullString, 3, bufPtf)
DfsInfo3 = CType(Marshal.PtrToStructure(bufPtf, GetType(_DFS_INFO_3)), _DFS_INFO_3)
bufPtf2 = DfsInfo3.storage
For i As Integer = 0 To DfsInfo3.NumberOfStorages - 1
'Increment the buffer and re-cast for each server in the list
DfsStorageInfo = CType(Marshal.PtrToStructure(bufPtf2, GetType(_DFS_STORAGE_INFO)), _DFS_STORAGE_INFO)
DfsInfo3.storage += Marshal.SizeOf(DfsStorageInfo)
bufPtf2 = DfsInfo3.storage
Debug.Print("\\" & DfsStorageInfo.ServerName & "\" & DfsStorageInfo.ShareName & " : " & DfsStorageInfo.State)
Next
NetApiBufferFree(bufPtf)
End Sub
End Module