Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than netapi32, prefix the name with the module name and a period.
NetLocalGroupAddMembers (netapi32)
.
C# Signature:
[DllImport("NetApi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
private static extern Int32 NetLocalGroupAddMembers(
string servername, //server name
string groupname, //group name
UInt32 level, //info level
ref LOCALGROUP_MEMBERS_INFO_3 buf, //Group info structure
UInt32 totalentries //number of entries
);
VB Signature:
Declare Function NetLocalGroupAddMembers Lib "netapi32.dll" (TODO) As TODO
User-Defined Types:
None.
Alternative Managed API:
Here's some sample code that can do this using System.DirectoryServices
//Error codes associated with 'NetLocalGroupAddMembers'
//The local group specified by the groupname parameter does not exist.
private const int NERR_GroupNotFound = 2220;
//The user does not have access to the requested information.
private const int ERROR_ACCESS_DENIED = 5;
// One or more of the members specified do not exist. Therefore, no new members were added.
private const int ERROR_NO_SUCH_MEMBER = 1387;
// One or more of the members specified were already members of the local group. No new members were added.
private const int ERROR_MEMBER_IN_ALIAS = 1378;
// One or more of the members cannot be added because their account type is invalid. No new members were added.
private const int ERROR_INVALID_MEMBER = 1388;
struct LOCALGROUP_MEMBERS_INFO_3
{
[MarshalAs(UnmanagedType.LPWStr)]
public string Domain;
}
[StructLayout(LayoutKind.Sequential)]
private struct LOCALGROUP_MEMBERS_INFO_0
{
[MarshalAs(UnmanagedType.SysInt)]
public IntPtr pSID;
if (!LookupAccountSid(null, baSid, sbName, ref uiName, sbReferencedDomainName, ref uiReferencedDomainNameCount, out eUse))
return bOk;
// prepare user name
LOCALGROUP_MEMBERS_INFO_3 info;
info.Domain = UserName;
int iRetVal = 0;
// add the user to the administrators group
if ((iRetVal = NetLocalGroupAddMembers(null, sbName.ToString(), 3, ref info, 1)) != 0)
return bOk;
bOk = true;
return bOk;
}
public static void AddMemberToLocalGroup(string groupName, SecurityIdentifier sid)
{
var sidBytes = new byte[sid.BinaryLength];
sid.GetBinaryForm(sidBytes, 0);
var info3 = new LOCALGROUP_MEMBERS_INFO_0
{
pSID = Marshal.AllocHGlobal(sidBytes.Length)
};
public static void AddMemberToLocalGroup(string groupName, SecurityIdentifier sid)
{
var sidBytes = new byte[sid.BinaryLength];
sid.GetBinaryForm(sidBytes, 0);
try
var info3 = new LOCALGROUP_MEMBERS_INFO_0
{
Marshal.Copy(sidBytes, 0, info3.pSID, sidBytes.Length);
pSID = Marshal.AllocHGlobal(sidBytes.Length)
};
var result = NetLocalGroupAddMembers(null, groupName, 0, ref info3, 1);
if (result == Win32ErrorCodes.NERR_Success || result == Win32ErrorCodes.MemberInAlias)
{
return;
}
var result = NetLocalGroupAddMembers(null, groupName, 0, ref info3, 1);
if (result == Win32ErrorCodes.NERR_Success || result == Win32ErrorCodes.MemberInAlias)
{
return;
}
throw new Win32Exception(result);
}
finally
{
Marshal.FreeHGlobal(info3.pSID);
}
}
}
}
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).