public static List <string> EnumerateGroups(string userName) { List <string> result = new List <string>(); int entriesRead; int totalEntries; IntPtr bufPtr; const int Level = 0; NetUserGetLocalGroups(null, userName, Level, 0, out bufPtr, 1024, out entriesRead, out totalEntries); if (entriesRead > 0) { LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[entriesRead]; IntPtr iter = bufPtr; for (int i = 0; i < entriesRead; i++) { RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0)); iter = (IntPtr)(iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); result.Add(RetGroups[i].groupname); } NetApiBufferFree(bufPtr); } return(result); }
//获取用户全部所在本地组 public string UserGetLocalGroups(string UserName) { int EntriesRead; int TotalEntries; IntPtr bufPtr; string tempStr = "<?xml version=\"1.0\" encoding=\"gb2312\" ?>\r\n"; tempStr += "<INFO>\r\n"; if (NetUserGetLocalGroups(null, UserName.ToString(), 0, 0, out bufPtr, 1024, out EntriesRead, out TotalEntries) != 0) { throw (new Exception("读取用户所在本地组失败")); } if (EntriesRead > 0) { LOCALGROUP_USERS_INFO_0[] GroupInfo = new LOCALGROUP_USERS_INFO_0[EntriesRead]; IntPtr iter = bufPtr; for (int i = 0; i < EntriesRead; i++) { GroupInfo[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0)); iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); tempStr += "<GROUP>" + GroupInfo[i].GroupName + "</GROUP>\r\n"; } tempStr += "</INFO>"; NetApiBufferFree(bufPtr); } return(tempStr); }
public static string[] GetLocalUserGroups(string username) { int read; int total; IntPtr pbuf; int ret = netapi.NetUserGetLocalGroups(null, username, 0, 0, out pbuf, -1, out read, out total); if (ret != 0) { throw new Win32Exception(ret); } List <string> groups = new List <string>(); if (read > 0) { var g = new LOCALGROUP_USERS_INFO_0(); IntPtr pItem = pbuf; for (int i = 0; i < read; ++i) { Marshal.PtrToStructure(pItem, g); pItem = new IntPtr(pItem.ToInt64() + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); groups.Add(g.name); } } netapi.NetApiBufferFree(pbuf); return(groups.ToArray()); }
public static List <string> EnumerateGroups(string userName) { List <string> result = new List <string>(); uint entriesRead; uint totalEntries; IntPtr bufPtr; uint level = 0; uint status = NetUserGetLocalGroups(null, userName, level, 0, out bufPtr, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries); if (status != NERR_Success) { throw new Exception("NetUserGetLocalGroups failed, Error code: " + status.ToString()); } if (entriesRead > 0) { LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[entriesRead]; IntPtr iter = bufPtr; for (int i = 0; i < entriesRead; i++) { RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0)); iter = new IntPtr(iter.ToInt64() + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); result.Add(RetGroups[i].groupname); } NetApiBufferFree(bufPtr); } return(result); }
private static List <string> GetLocalGroups(string Username) { List <string> myList = new List <string>(); int EntriesRead; int TotalEntries; IntPtr bufPtr; string ServerName = null; int Flags = 0; NetUserGetLocalGroups(ServerName, Username, 0, Flags, out bufPtr, 1024, out EntriesRead, out TotalEntries); if (EntriesRead > 0) { LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[EntriesRead]; IntPtr iter = bufPtr; for (int i = 0; i < EntriesRead; i++) { RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0)); iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); myList.Add(RetGroups[i].name); } NetApiBufferFree(bufPtr); } return(myList); }
public static List <string> GetUserGroups(string userName) { List <string> myList = new List <string>(); int EntriesRead; int TotalEntries; IntPtr bufPtr; int ErrorCode = NetUserGetLocalGroups(null, userName, 0, 0, out bufPtr, 1024, out EntriesRead, out TotalEntries); do { if (EntriesRead > 0) { LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[EntriesRead]; IntPtr iter = bufPtr; for (int i = 0; i < EntriesRead; i++) { RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0)); iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); myList.Add(RetGroups[i].groupname); } NetApiBufferFree(bufPtr); } } while (ErrorCode != 0 && (ErrorCode = NetUserGetLocalGroups(null, userName, 0, 0, out bufPtr, 1024, out EntriesRead, out TotalEntries)) == 234); return(myList); }
NetGetGroups( string servername, string username, out string[] groups ) { uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS; if (String.IsNullOrEmpty(servername)) { servername = null; } if (String.IsNullOrEmpty(username)) { username = null; } groups = null; IntPtr bufPtr = new IntPtr(0); try { int entriesRead = 0; int totalEntries = 0; if (!NetApiInitCalled) { result = NetApiInit(); if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS) { return result; } NetApiInitCalled = true; } result = (uint)NetUserGetLocalGroups( servername, username, 0, 0, out bufPtr, MAX_PREFERRED_LENGTH, out entriesRead, out totalEntries); if (entriesRead > 0) { groups = new string[entriesRead]; LOCALGROUP_USERS_INFO_0 groupsStruct = new LOCALGROUP_USERS_INFO_0(); IntPtr iter = bufPtr; for (int i = 0; i < entriesRead; i++) { if (iter != IntPtr.Zero) { groupsStruct = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0)); if (!String.IsNullOrEmpty(groupsStruct.lgrui0_name)) { groups[i] = groupsStruct.lgrui0_name; } iter = (IntPtr)((long)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); } } } } catch (Exception) { result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE; } finally { if (bufPtr != IntPtr.Zero) { NetApiBufferFree(bufPtr); } } return result; }
public static ArrayList GetUserGroups(string ServerName, string Username, int level) { ArrayList myList = new ArrayList(); int EntriesRead = 0; int TotalEntries = 0; IntPtr bufPtr = IntPtr.Zero; NetUserGetGroups(ServerName, Username,0,out bufPtr,1024,out EntriesRead, out TotalEntries); if(EntriesRead> 0) { LOCALGROUP_USERS_INFO_0[] RetGroups = new LOCALGROUP_USERS_INFO_0[EntriesRead]; IntPtr iter = bufPtr; for(int i=0; i < EntriesRead; i++) { RetGroups[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0)); iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); myList.Add(RetGroups[i].groupname); } NetApiBufferFree(bufPtr); } return myList; }
//获取用户全部所在本地组 public string UserGetLocalGroups(string UserName) { int EntriesRead; int TotalEntries; IntPtr bufPtr; string tempStr = "<?xml version=\"1.0\" encoding=\"gb2312\" ?>\r\n"; tempStr += "<INFO>\r\n"; if (NetUserGetLocalGroups(null, UserName, 0, 0, out bufPtr, 1024, out EntriesRead, out TotalEntries) != 0) { //throw (new Exception("读取用户所在本地组失败")); return "读取用户所在本地组失败"; } if (EntriesRead > 0) { LOCALGROUP_USERS_INFO_0[] GroupInfo = new LOCALGROUP_USERS_INFO_0[EntriesRead]; IntPtr iter = bufPtr; for (int i = 0; i < EntriesRead; i++) { GroupInfo[i] = (LOCALGROUP_USERS_INFO_0)Marshal.PtrToStructure(iter, typeof(LOCALGROUP_USERS_INFO_0)); iter = (IntPtr)((int)iter + Marshal.SizeOf(typeof(LOCALGROUP_USERS_INFO_0))); tempStr += "<GROUP>" + GroupInfo[i].GroupName + "</GROUP>\r\n"; } tempStr += "</INFO>"; NetApiBufferFree(bufPtr); } return tempStr; }