NetUserGetInfo/NetLocalGroupGetInfo 返回错误 1722

发布于 2024-09-18 15:46:43 字数 1862 浏览 6 评论 0原文

我有以下代码(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 服务器的名称,得到相同的结果。

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

请帮忙。我做错了什么?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

木有鱼丸 2024-09-25 15:46:43

NetUserGetInfoNetLocalGroupGetInfo 都需要 Unicode(宽)字符串参数。您能否声明 Unicode 这些方法并确认问题是否仍然存在?

另请参阅 http://www.xtremedotnettalk.com/showthread.php?t=69609< /a>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文