Windows用户相关操作

时间:2023-03-10 05:46:09
Windows用户相关操作
  • 获取所有用户
NET_API_STATUS NetUserEnum(
LPCWSTR servername,
DWORD level,
DWORD filter,
LPBYTE* bufptr,
DWORD prefmaxlen,
LPDWORD entriesread,
LPDWORD totalentries,
LPDWORD resume_handle
);
 #ifndef UNICODE
#define UNICODE
#endif #include <stdio.h>
#include <assert.h>
#include <windows.h>
#include <lm.h> int wmain(int argc, wchar_t *argv[])
{
LPUSER_INFO_0 pBuf = NULL;
LPUSER_INFO_0 pTmpBuf;
DWORD dwLevel = ;
DWORD dwPrefMaxLen = MAX_PREFERRED_LENGTH;
DWORD dwEntriesRead = ;
DWORD dwTotalEntries = ;
DWORD dwResumeHandle = ;
DWORD i;
DWORD dwTotalCount = ;
NET_API_STATUS nStatus;
LPTSTR pszServerName = NULL; if (argc > )
{
fwprintf(stderr, L"Usage: %s [\\\\ServerName]\n", argv[]);
exit();
}
// The server is not the default local computer.
//
if (argc == )
pszServerName = argv[];
wprintf(L"\nUser account on %s: \n", pszServerName);
//
// Call the NetUserEnum function, specifying level 0;
// enumerate global user account types only.
//
do // begin do
{
nStatus = NetUserEnum(pszServerName,
dwLevel,
FILTER_NORMAL_ACCOUNT, // global users
(LPBYTE*)&pBuf,
dwPrefMaxLen,
&dwEntriesRead,
&dwTotalEntries,
&dwResumeHandle);
//
// If the call succeeds,
//
if ((nStatus == NERR_Success) || (nStatus == ERROR_MORE_DATA))
{
if ((pTmpBuf = pBuf) != NULL)
{
//
// Loop through the entries.
//
for (i = ; (i < dwEntriesRead); i++)
{
assert(pTmpBuf != NULL); if (pTmpBuf == NULL)
{
fprintf(stderr, "An access violation has occurred\n");
break;
}
//
// Print the name of the user account.
//
wprintf(L"\t-- %s\n", pTmpBuf->usri0_name); pTmpBuf++;
dwTotalCount++;
}
}
}
//
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus);
//
// Free the allocated buffer.
//
if (pBuf != NULL)
{
NetApiBufferFree(pBuf);
pBuf = NULL;
}
}
// Continue to call NetUserEnum while
// there are more entries.
//
while (nStatus == ERROR_MORE_DATA); // end do
//
// Check again for allocated memory.
//
if (pBuf != NULL)
NetApiBufferFree(pBuf);
//
// Print the final count of users enumerated.
//
fprintf(stderr, "\nTotal of %d entries enumerated\n", dwTotalCount); return ;
}
  • 获取用户信息
NET_API_STATUS NetUserGetInfo(
LPCWSTR servername,
LPCWSTR username,
DWORD level,
LPBYTE* bufptr
);
 #ifndef UNICODE
#define UNICODE
#endif #include <stdio.h>
#include <windows.h>
#include <lm.h> int wmain(int argc, wchar_t *argv[])
{
DWORD dwLevel = ;
LPUSER_INFO_10 pBuf = NULL;
NET_API_STATUS nStatus; if (argc != )
{
fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[]);
exit();
}
//
// Call the NetUserGetInfo function; specify level 10.
//
nStatus = NetUserGetInfo(argv[],
argv[],
dwLevel,
(LPBYTE *)&pBuf);
//
// If the call succeeds, print the user information.
//
if (nStatus == NERR_Success)
{
if (pBuf != NULL)
{
wprintf(L"\n\tAccount: %s\n", pBuf->usri10_name);
wprintf(L"\tComment: %s\n", pBuf->usri10_comment);
wprintf(L"\tUser comment: %s\n", pBuf->usri10_usr_comment);
wprintf(L"\tFull name: %s\n", pBuf->usri10_full_name);
}
}
// 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 ;
}
  • 修改用户信息
NET_API_STATUS NetUserSetInfo(
LPCWSTR servername,
LPCWSTR username,
DWORD level,
LPBYTE buf,
LPDWORD parm_err
);
 #ifndef UNICODE
#define UNICODE
#endif #include <stdio.h>
#include <windows.h>
#include <lm.h> int wmain(int argc, wchar_t *argv[])
{
DWORD dwLevel = ;
USER_INFO_1008 ui;
NET_API_STATUS nStatus; if (argc != )
{
fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[]);
exit();
}
// Fill in the USER_INFO_1008 structure member.
// UF_SCRIPT: required for LAN Manager 2.0 and
// Windows NT and later.
//
ui.usri1008_flags = UF_SCRIPT | UF_ACCOUNTDISABLE;
//
// Call the NetUserSetInfo function
// to disable the account, specifying level 1008.
//
nStatus = NetUserSetInfo(argv[],
argv[],
dwLevel,
(LPBYTE)&ui,
NULL);
//
// Display the result of the call.
//
if (nStatus == NERR_Success)
fwprintf(stderr, L"User account %s has been disabled\n", argv[]);
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus); return ;
}
  • 增加用户
NET_API_STATUS NetUserAdd(
LMSTR servername,
DWORD level,
LPBYTE buf,
LPDWORD parm_err
);
 #ifndef UNICODE
#define UNICODE
#endif #include <stdio.h>
#include <windows.h>
#include <lm.h> int wmain(int argc, wchar_t *argv[])
{
USER_INFO_1 ui;
DWORD dwLevel = ;
DWORD dwError = ;
NET_API_STATUS nStatus; if (argc != )
{
fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[]);
exit();
}
//
// Set up the USER_INFO_1 structure.
// USER_PRIV_USER: name identifies a user,
// rather than an administrator or a guest.
// UF_SCRIPT: required for LAN Manager 2.0 and
// Windows NT and later.
//
ui.usri1_name = argv[];
ui.usri1_password = argv[];
ui.usri1_priv = USER_PRIV_USER;
ui.usri1_home_dir = NULL;
ui.usri1_comment = NULL;
ui.usri1_flags = UF_SCRIPT;
ui.usri1_script_path = NULL;
//
// Call the NetUserAdd function, specifying level 1.
//
nStatus = NetUserAdd(argv[],
dwLevel,
(LPBYTE)&ui,
&dwError);
//
// If the call succeeds, inform the user.
//
if (nStatus == NERR_Success)
fwprintf(stderr, L"User %s has been successfully added on %s\n",
argv[], argv[]);
//
// Otherwise, print the system error.
//
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus); return ;
}
  • 用户删除
NET_API_STATUS NetUserDel(
LPCWSTR servername,
LPCWSTR username
);
 #ifndef UNICODE
#define UNICODE
#endif #include <stdio.h>
#include <windows.h>
#include <lm.h> int wmain(int argc, wchar_t *argv[])
{
DWORD dwError = ;
NET_API_STATUS nStatus;
//
// All parameters are required.
//
if (argc != )
{
fwprintf(stderr, L"Usage: %s \\\\ServerName UserName\n", argv[]);
exit();
}
//
// Call the NetUserDel function to delete the share.
//
nStatus = NetUserDel(argv[], argv[]);
//
// Display the result of the call.
//
if (nStatus == NERR_Success)
fwprintf(stderr, L"User %s has been successfully deleted on %s\n",
argv[], argv[]);
else
fprintf(stderr, "A system error has occurred: %d\n", nStatus); return ;
}