NetEnumUsers( string servername, int resumeHandle, out LUGEnumStatus enumStatus ) { uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS; if (String.IsNullOrEmpty(servername)) { servername = null; } enumStatus = new LUGEnumStatus(); enumStatus.initializeToNull(); enumStatus.type = LUGType.User; IntPtr bufPtr = IntPtr.Zero; try { int localResumeHandle = resumeHandle; int totalEntries = 0; int entriesRead = 0; if (!NetApiInitCalled) { result = NetApiInit(); if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS) { return result; } NetApiInitCalled = true; } result = apiNetUserEnum( servername, 20, 2, out bufPtr, MAX_PREFERRED_LENGTH, ref entriesRead, ref totalEntries, ref localResumeHandle ); if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS && result != (uint)LUGAPI.WinError.ERROR_MORE_DATA) { return result; } enumStatus.entriesRead = entriesRead; enumStatus.totalEntries = totalEntries; enumStatus.resumeHandle = localResumeHandle; if (entriesRead > 0) { USER_INFO_20[] users = new USER_INFO_20[entriesRead]; IntPtr iter = bufPtr; enumStatus.entries = new List<string[]>(); for (int i = 0; i < entriesRead; i++) { users[i] = (USER_INFO_20)Marshal.PtrToStructure(iter, typeof(USER_INFO_20)); iter = (IntPtr)((long)iter + Marshal.SizeOf(typeof(USER_INFO_20))); string[] userInfo ={"",(users[i].usri20_flags & UF_ACCOUNTDISABLE) != 0 ? DISABLED : "",users[i].usri20_name, users[i].usri20_full_name,users[i].usri20_comment }; enumStatus.entries.Add(userInfo); } if (result == (int)LUGAPI.WinError.ERROR_MORE_DATA) { enumStatus.moreEntries = true; } } } catch (Exception) { result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE; } finally { if (bufPtr != IntPtr.Zero) { NetApiBufferFree(bufPtr); } } return result; }
NetGetUserInfo( string servername, string username, out LUGInfo userInfo ) { IntPtr bufPtr = IntPtr.Zero; if (String.IsNullOrEmpty(servername)) { servername = null; } if (String.IsNullOrEmpty(username)) { username = null; } uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS; userInfo = new LUGInfo(); userInfo.initializeToNull(); userInfo.fullname = ""; userInfo.description = ""; userInfo.flags = 0; try { if (!NetApiInitCalled) { result = NetApiInit(); if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS) { return result; } NetApiInitCalled = true; } result = (uint)NetUserGetInfo(servername, username, 20, out bufPtr); if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS) { return result; } USER_INFO_20 userinfo_20 = new USER_INFO_20(); userinfo_20 = (USER_INFO_20)Marshal.PtrToStructure(bufPtr, typeof(USER_INFO_20)); userInfo.fullname = userinfo_20.usri20_full_name; userInfo.description = userinfo_20.usri20_comment; userInfo.flags = userinfo_20.usri20_flags; } catch (Exception) { result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE; } finally { if (bufPtr != IntPtr.Zero) { NetApiBufferFree(bufPtr); } } return result; }