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.