NetUserGetInfo / NetLocalGroupGetInfo返回错误1722

时间:2010-09-07 23:49:50

标签: .net vb.net winapi pinvoke

我有以下代码(VB.NET),用于确定给定的帐户名称是指本地组还是用户帐户。这仅适用于计算机上的帐户/组,而不是域。

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

我的问题是NetUserGetInfo / NetLocalGroupGetInfo调用总是返回错误代码1722(RPC服务器不可用)。我尝试使用本地计算机名称和远程Windows服务器的名称,我有管理员权限,具有相同的结果。

如果我将"\\" & MachineName替换为Nothing,则无论AccountName引用的帐户/组是否确实存在,我都会收到错误2221/2220(未找到用户/组)。

请帮忙。我做错了什么?

更新:不确定这是否有帮助,但我已尝试在Win 7和Win XP SP3上运行上述内容。我的编译目标是.NET 4.0客户端框架。

1 个答案:

答案 0 :(得分:2)

NetUserGetInfoNetLocalGroupGetInfo都期望Unicode(宽)字符串参数。您可以Declare Unicode这些方法并确认问题是否仍然存在?

另见http://www.xtremedotnettalk.com/showthread.php?t=69609