我可以在C#中使用NetUserGetInfo吗?

我可以在C#中使用NetUserGetInfo吗?,c#,namespaces,using,C#,Namespaces,Using,我想在我的代码中使用。但由于某些原因,我找不到用于它的名称空间。我认为有效的三个是 using System.DirectoryServices.AccountManagement; using System.Runtime.InteropServices; using System.DirectoryServices; 但这些都不管用。所有使用NETSoeServInt的例子我都可以找到,而不是C++。这让我觉得也许我不能在C#中使用它。我可以吗?如果是这样,我应该使用什么名称空间来访问Ne

我想在我的代码中使用。但由于某些原因,我找不到用于它的名称空间。我认为有效的三个是

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

但这些都不管用。所有使用NETSoeServInt的例子我都可以找到,而不是C++。这让我觉得也许我不能在C#中使用它。我可以吗?如果是这样,我应该使用什么名称空间来访问NetUserGetInfo函数?非常感谢您的帮助。

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

在本例中,下面是一个示例,说明了以下托管包装器:

[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;
    }
}

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

在本例中,下面是一个示例,说明了以下托管包装器:

[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;
    }
}

NetUserGetInfo
是一个需要p/调用的Win32 API。使用.NET时,最好使用.NET diectory服务API。该类可能是一个很好的起点。

NetUserGetInfo
是一个需要p/调用的Win32 API。使用.NET时,最好使用.NET diectory服务API。这门课可能是一个很好的起点