C # obtain all user groups and user names of the Local Machine

Source: Internet
Author: User

Internal class Win32API
{
# Region Win32 API Interfaces
[DllImport ("netapi32.dll", EntryPoint = "NetApiBufferFree")]
Internal static extern void NetApiBufferFree (IntPtr bufptr );

[DllImport ("netapi32.dll", EntryPoint = "NetLocalGroupGetMembers")]
Internal static extern uint NetLocalGroupGetMembers (
IntPtr ServerName,
IntPtr GrouprName,
Uint level,
Ref IntPtr siPtr,
Uint prefmaxlen,
Ref uint entriesread,
Ref uint totalentries,
IntPtr resumeHandle );

[DllImport ("netapi32.dll", EntryPoint = "NetLocalGroupEnum")]
Internal static extern uint NetLocalGroupEnum (
IntPtr ServerName,
Uint level,
Ref IntPtr siPtr,
Uint prefmaxlen,
Ref uint entriesread,
Ref uint totalentries,
IntPtr resumeHandle );

[StructLayoutAttribute (LayoutKind. Sequential, CharSet = CharSet. Auto)]
Internal struct LOCALGROUP_MEMBERS_INFO_1
{
Public IntPtr lgrmi1_sid;
Public IntPtr lgrmi1_sidusage;
Public IntPtr lgrmi1_name;

}

[StructLayoutAttribute (LayoutKind. Sequential, CharSet = CharSet. Auto)]
Internal struct LOCALGROUP_INFO_1
{
Public IntPtr lpszGroupName;
Public IntPtr lpszComment;
}
# Endregion
}
 

Private void groupbtn_Click (object sender, System. EventArgs e)
{
// Defining values for getting group names
Uint level = 1, prefmaxlen = 0 xFFFFFFFF,
Entriesread = 0, totalentries = 0;

// Values that will receive information.
IntPtr GroupInfoPtr, UserInfoPtr;
GroupInfoPtr = IntPtr. Zero;
UserInfoPtr = IntPtr. Zero;

Win32API. NetLocalGroupEnum (
IntPtr. Zero, // Server name. it must be null
Level, // level can be 0 or 1 for groups.
// For more information see LOCALGROUP_INFO_0
// And LOCALGROUP_INFO_1
Ref GroupInfoPtr, // Value that will be receive information
Prefmaxlen, // maximum length
Ref entriesread, // value that sums es the count
// Elements actually enumerated.
Ref totalentries, // value that sums es the approximate total
// Number of entries that cocould have been
//

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.