NetUserGetInfo/NetLocalGroupGetInfo returning error 1722

对着背影说爱祢 提交于 2019-12-23 03:15:26

问题


I have the following code (VB.NET) which is designed to determine if a given account name refers to a local group or a user account. This will only be called for accounts/groups on a machine, not a domain.

Module netapi
    Private Declare Function NetUserGetInfo Lib "Netapi32.dll" ( _
         ByVal ServerName As String, _
         ByVal UserName As String, _
         ByVal level As Integer, _
         ByRef BufPtr As IntPtr) As Integer

    Private Declare Function NetLocalGroupGetInfo Lib "Netapi32.dll" ( _
         ByVal ServerName As String, _
         ByVal GroupName As String, _
         ByVal level As Integer, _
         ByRef BufPtr As IntPtr) As Integer

    Declare Unicode Function NetApiBufferFree Lib "Netapi32.dll" _
    (ByRef buffer As IntPtr) As Long

    Public Function GetPrincipalType(ByVal MachineName As String, ByVal AccountName As String) As String
        Dim bufPtr As IntPtr
        Dim lngReturn As Integer = NetUserGetInfo("\\" & MachineName, AccountName, 0, bufPtr)
        Console.WriteLine("NetUserGetInfo return value = " & lngReturn)
        Call NetApiBufferFree(bufPtr)
        bufPtr = IntPtr.Zero
        If lngReturn = 0 Then
            Return "User"
        End If
        lngReturn = NetLocalGroupGetInfo("\\" & MachineName, AccountName, 0, bufPtr)
        Console.WriteLine("NetLocalGroupGetInfo return value = " & lngReturn)
        Call NetApiBufferFree(bufPtr)
        bufPtr = IntPtr.Zero
        If lngReturn = 0 Then
            Return "Group"
        End If
        Return "NotFound"
    End Function
End Module

My problem is that the NetUserGetInfo/NetLocalGroupGetInfo calls always return error code 1722 (RPC Server unavailable). I've tried using the local machine name and the name of remote Windows servers, on which I have admin rights, with the same result.

If I replace "\\" & MachineName with Nothing then I get error 2221/2220 (User/Group not found) regardless of whether or not the account/group referenced by AccountName actually exists.

Please help. What am I doing wrong?

Update: Not sure if this helps, but I've tried running the above on both Win 7 and Win XP SP3. My compilation is targetting the .NET 4.0 Client framework.


回答1:


NetUserGetInfo and NetLocalGroupGetInfo both expect Unicode (wide) string parameters. Can you Declare Unicode these methods and confirm whether the problem persists?

Also see http://www.xtremedotnettalk.com/showthread.php?t=69609



来源:https://stackoverflow.com/questions/3663571/netusergetinfo-netlocalgroupgetinfo-returning-error-1722

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!