使用NetDfsGetInfo查找DFS目标的活动unc路径

时间:2014-04-21 20:07:08

标签: vb.net dfs

我正在使用pinvoke.net中的NetDfsGetInfo示例!找到DFS目标的活动unc路径。如果我使用具有两个目标的DFS路径调用子DFSInfo,则返回两个目标都是活动的。我已经搜索了互联网,并发现了一个建议,用vbNullString替换vbNull来调用ServerName和ShareName,但这没什么区别。

有人可以解释一下我做错了什么吗?以下是我使用的代码:

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

0 个答案:

没有答案