【问题标题】:Can I use NetUserGetInfo in C#?我可以在 C# 中使用 NetUserGetInfo 吗?
【发布时间】:2012-07-02 21:59:18
【问题描述】:

我想在我的代码中使用http://msdn.microsoft.com/en-us/library/aa370654%28VS.85%29.aspx。但由于某种原因,我找不到为其使用的命名空间。我认为可行的三个是

using System.DirectoryServices.AccountManagement;
using System.Runtime.InteropServices;
using System.DirectoryServices;

但这些都不起作用。我能找到的所有使用 NetUserGetInfo 的示例都是用 C++ 编写的,而不是 C#。这让我觉得也许我不能在 C# 中使用它。我可以吗?如果是这样,我应该使用什么命名空间来访问 NetUserGetInfo 函数?任何帮助表示赞赏。

【问题讨论】:

    标签: c# namespaces using


    【解决方案1】:

    您在寻找什么命名空间?命名空间是 .NET 特定的概念。 NetUserGetInfo 是一个 Win32 非托管函数。如果您想从托管 .NET 代码中调用它,您需要编写一个托管包装器并通过 P/Invoke 调用它。

    这里有一个useful site,它说明了以下托管包装器:

    [DllImport("Netapi32.dll", CharSet=CharSet.Unicode, ExactSpelling=true)]
    private extern static int NetUserGetInfo(
        [MarshalAs(UnmanagedType.LPWStr)] string ServerName,
        [MarshalAs(UnmanagedType.LPWStr)] string UserName, 
        int level, 
        out IntPtr BufPtr
    );
    

    用户定义的结构:

    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    public struct USER_INFO_10
    {
        [MarshalAs(UnmanagedType.LPWStr)]
        public string usri10_name;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string usri10_comment;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string usri10_usr_comment;
        [MarshalAs(UnmanagedType.LPWStr)]
        public string usri10_full_name;
    }
    

    和一个示例调用:

    public bool AccountGetFullName(string MachineName, string AccountName, ref string FullName) 
    {
        if (MachineName.Length == 0 ) 
        {
            throw new ArgumentException("Machine Name is required");
        }
        if (AccountName.Length == 0 ) 
        {
            throw new ArgumentException("Account Name is required");
        }
        try 
        {
            // Create an new instance of the USER_INFO_1 struct
            USER_INFO_10 objUserInfo10 = new USER_INFO_10();
            IntPtr bufPtr; // because it's an OUT, we don't need to Alloc
            int lngReturn = NetUserGetInfo(MachineName, AccountName, 10, out bufPtr ) ;
            if (lngReturn == 0) 
            {
                objUserInfo10 = (USER_INFO_10) Marshal.PtrToStructure(bufPtr, typeof(USER_INFO_10) );
                FullName = objUserInfo10.usri10_full_name;
            }
            NetApiBufferFree( bufPtr );
            bufPtr = IntPtr.Zero;
            if (lngReturn == 0 ) 
            {
                return true;
            }
            else 
            {
                //throw new System.ApplicationException("Could not get user's Full Name.");
                return false;
            }
        } 
        catch (Exception exp)
        {
            Debug.WriteLine("AccountGetFullName: " + exp.Message);
            return false;
        }
    }
    

    【讨论】:

      【解决方案2】:

      NetUserGetInfo 是一个需要 P/Invoked 的 Win32 API。使用 .NET 时,最好使用 .NET 字典服务 API。 UserPrincipal 类可能是一个很好的起点。

      【讨论】:

        猜你喜欢
        • 2012-07-09
        • 2011-06-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多