获取登录域列表

来源:互联网 发布:找工作网站 知乎 编辑:程序博客网 时间:2024/05/21 15:05
 

获取本机可以登录的位置(本机、域),在做windows登录的相关产品的时候比较有用,

Windows API 

NET_API_STATUS NetWkstaUserGetInfo(  LPWSTR reserved,    DWORD level,        LPBYTE *bufptr    );

可以完成这一操作.

 

reserved 为保留 总是为NULL

level为希望获取数据的级别,level值为 0、1、1101中的一个,分别对应着一个结构:

typedef struct _WKSTA_USER_INFO_0 {  LPWSTR    wkui0_username;}WKSTA_USER_INFO_0, *PWKSTA_USER_INFO_0, *LPWKSTA_USER_INFO_0;
typedef struct _WKSTA_USER_INFO_1 {  LPWSTR    wkui1_username;  LPWSTR    wkui1_logon_domain;  LPWSTR    wkui1_oth_domains;  LPWSTR    wkui1_logon_server;}WKSTA_USER_INFO_1, *PWKSTA_USER_INFO_1, *LPWKSTA_USER_INFO_1;

typedef struct _WKSTA_USER_INFO_1101 { LPWSTR wkui1101_oth_domains;} WKSTA_USER_INFO_1101, *PWKSTA_USER_INFO_1101, *LPWKSTA_USER_INFO_1101;

 

结构中wkuil_logon_domain 为当前登录的domain 或计算机名(如果用户没有加入域而是登录本机)

wkuil_oth_domains 为可以使用的其他域列表,域名用空格隔开

 

下面是MSDN上的一段事例代码

#ifndef UNICODE
#define UNICODE
#endif

#include <stdio.h>
#include <windows.h>
#include <lm.h>
#pragma comment(lib, "Netapi32.lib")


int wmain(void)
{
    DWORD dwLevel = 1;
    LPWKSTA_USER_INFO_1 pBuf = NULL;
    NET_API_STATUS nStatus;
    //
    // Call the NetWkstaUserGetInfo function;
    //  specify level 1.
    //
    nStatus = NetWkstaUserGetInfo(NULL,
        dwLevel,
        (LPBYTE *)&pBuf);
    //
    // If the call succeeds, print the information
    //  about the logged-on user.
    //
    if (nStatus == NERR_Success)
    {
        if (pBuf != NULL)
        {
            wprintf(L"\n\tUser:          %s\n", pBuf->wkui1_username);
            wprintf(L"\tDomain:        %s\n", pBuf->wkui1_logon_domain);
            wprintf(L"\tOther Domains: %s\n", pBuf->wkui1_oth_domains);
            wprintf(L"\tLogon Server:  %s\n", pBuf->wkui1_logon_server);
        }
    }
    // Otherwise, print the system error.
    //
    else
        fprintf(stderr, "A system error has occurred: %d\n", nStatus);
    //
    // Free the allocated memory.
    //
    if (pBuf != NULL)
        NetApiBufferFree(pBuf);
   
    return 0;
}

原创粉丝点击