这段代码如何从C#转到VB.Net

[StructLayout(LayoutKind.Sequential)]
protected struct SHARE_INFO_1
{
[MarshalAs(UnmanagedType.LPWStr)]
public string shi1_netname;
[MarshalAs(UnmanagedType.U4)]
public uint shi1_type;
[MarshalAs(UnmanagedType.LPWStr)]
public string shi1_remark;
}
[DllImport("Netapi32.dll", EntryPoint = "NetShareEnum")]
protected static extern int NetShareEnum(
[MarshalAs(UnmanagedType.LPWStr)] string servername,
[MarshalAs(UnmanagedType.U4)] uint level,
out IntPtr bufptr,
[MarshalAs(UnmanagedType.U4)] int prefmaxlen,
[MarshalAs(UnmanagedType.U4)] out uint entriesread,
[MarshalAs(UnmanagedType.U4)] out uint totalentries,
[MarshalAs(UnmanagedType.U4)] out uint resume_handle
);
private string[] NetShareList(string server)
{

        IntPtr buffer;
        uint entriesread;
        uint totalentries;
        uint resume_handle;

        //-1应该是获取所有的share,返回0表示成功
        if (NetShareEnum(server, 1, out buffer, -1, out entriesread, out totalentries, out resume_handle) == 0)
        {
            Int32 ptr = buffer.ToInt32();
            ArrayList alShare = new ArrayList();
            for (int i = 0; i < entriesread; i++)
            {

                SHARE_INFO_1 shareInfo = (SHARE_INFO_1)Marshal.PtrToStructure(new IntPtr(ptr), typeof(SHARE_INFO_1));
                if (shareInfo.shi1_type == 0)//Disk drive类型
                {
                    alShare.Add(shareInfo.shi1_netname);
                }
                ptr += Marshal.SizeOf(shareInfo);
            }
            string[] share = new string[alShare.Count];
            for (int i = 0; i < alShare.Count; i++)
            {
                share[i] = alShare[i].ToString();
            }
            return share;
        }
        else
            return null;

    }
net

1个回答

<StructLayout(LayoutKind.Sequential)>
Protected Structure SHARE_INFO_1
    <MarshalAs(UnmanagedType.LPWStr)> _
    Public shi1_netname As String
    <MarshalAs(UnmanagedType.U4)> _
    Public shi1_type As System.UInt32
    <MarshalAs(UnmanagedType.LPWStr)> _
    Public shi1_remark As String
End Structure

<DllImport("Netapi32.dll", EntryPoint:="NetShareEnum")> _
Protected Shared Function NetShareEnum(<MarshalAs(UnmanagedType.LPWStr)> ByVal servername As String, <MarshalAs(UnmanagedType.U4)> ByVal level As UInteger, ByRef bufptr As IntPtr, _
                                       <MarshalAs(UnmanagedType.U4)> ByVal prefmaxlen As Integer, <MarshalAs(UnmanagedType.U4)> ByRef entriesread As UInteger, _
                                       <MarshalAs(UnmanagedType.U4)> ByRef totalentries As UInteger, <MarshalAs(UnmanagedType.U4)> ByRef resume_handle As UInteger) As Integer
End Function


Private Function NetShareList(ByVal server As String) As String()
    Dim buffer As IntPtr
    Dim entriesread As UInteger
    Dim totalentries As UInteger
    Dim resume_handle As UInteger
    '-1应该是获取所有的share,返回0表示成功
    If (NetShareEnum(server, 1, buffer, -1, entriesread, totalentries, resume_handle) = 0) Then
        Dim ptr As Integer = buffer.ToInt32
        Dim alShare As ArrayList = New ArrayList()
        For i As Integer = 0 To entriesread - 1
            Dim shareInfo As SHARE_INFO_1 = DirectCast(Marshal.PtrToStructure(New IntPtr(ptr), GetType(SHARE_INFO_1)), SHARE_INFO_1)
            If (shareInfo.shi1_type = 0) Then    'Disk drive类型
                alShare.Add(shareInfo.shi1_netname)
            End If
            ptr += Marshal.SizeOf(shareInfo)
        Next
        Dim share As String() = New String(alShare.Count) {}
        For i As Integer = 0 To alShare.Count - 1
            share(i) = alShare(i).ToString()
        Next
        Return share
    Else
        Return Nothing
    End If
End Function
    已经转换成功
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问