NetUserGetInfo / NetLocalGroupGetInfo return error 1722

I have the following code (VB.NET) that is designed to determine if a given account name is in a local group or user account. This will only be called for accounts / groups on the machine, not on the 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 calls to NetUserGetInfo / NetLocalGroupGetInfo always return error code 1722 (RPC server unavailable). I tried using the local machine name and the name of the remote windows servers on which I have administrator rights, with the same result.

If I replace "\\" & MachineName

with Nothing

, then I get error 2221/2220 (User / Group not found), regardless of whether the account / group that is being referenced exists or not AccountName

.

Please, help. What am I doing wrong?

Update: Not sure if it helps, but I tried the above on both Win 7 and Win XP SP3. My compilation targets the .NET 4.0 Client framework.

+1


source to share


1 answer


NetUserGetInfo

and NetLocalGroupGetInfo

both expect Unicode (wide) string options. Can you Declare Unicode

use these methods and confirm if the problem persists?



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

+1


source







All Articles