我想使用Windows API中的NetLocalGroupAddMembers函数将用户添加到本地Administrators组。
NET_API_STATUS nStatus;
DWORD dwLevel = 0;
LOCALGROUP_INFO_0 localGroupInformation;
localGroupInformation.lgrpi0_name = username;
nStatus = NetLocalGroupAddMembers(
NULL,
TEXT("Administrators"),
dwLevel,
(LPBYTE)& localGroupInformation,
1
);
但是,当我使用以下命令时,它会起作用:
net localgroup Administrators username /add
谢谢。
答案 0 :(得分:1)
您必须使用用户 SID 和 LOCALGROUP_MEMBERS_INFO_0 结构。
类似的东西=>
WCHAR wsName[256] = L"test";
DWORD dwSidSize = 0;
DWORD dwDomainSize = 0;
SID_NAME_USE snu;
LPWSTR pwszDomain = NULL;
PSID pSid = NULL;
LookupAccountName(NULL, wsName, NULL, &dwSidSize, NULL, &dwDomainSize, &snu);
DWORD dwErr = GetLastError();
if (dwErr == ERROR_INSUFFICIENT_BUFFER)
{
pSid = (PSID)LocalAlloc(LPTR, dwSidSize);
pwszDomain = (PWSTR)LocalAlloc(LPTR, dwDomainSize * sizeof(WCHAR));
if (!LookupAccountName(NULL, wsName, pSid, &dwSidSize, pwszDomain, &dwDomainSize, &snu))
{
// Handle error
}
}
if (pwszDomain != NULL)
LocalFree(pwszDomain);
if (pSid)
{
NET_API_STATUS nStatus;
DWORD dwLevel = 0;
LOCALGROUP_MEMBERS_INFO_0 localGroupMemberInfo;
localGroupMemberInfo.lgrmi0_sid = pSid;
nStatus = NetLocalGroupAddMembers(
NULL,
TEXT("Administrators"),
dwLevel,
(LPBYTE)&localGroupMemberInfo,
1
);
// Test nStatus...
LocalFree(pSid);
}