NetAddUser( string servername, string username, string password, string fullname, string description, uint flags ) { uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS; if (String.IsNullOrEmpty(servername)) { servername = null; } if (String.IsNullOrEmpty(username)) { username = null; } if (String.IsNullOrEmpty(password)) { password = null; } if (String.IsNullOrEmpty(fullname)) { fullname = null; } if (String.IsNullOrEmpty(description)) { description = null; } USER_INFO_1 ui1 = new USER_INFO_1(); USER_INFO_1011 ui11 = new USER_INFO_1011(); UInt32 parm_err = 0; ui1.sUsername = username; ui1.sPassword = password; ui1.uiPasswordAge = 0; ui1.uiPriv = 1; // USER_PRIV_USER ui1.sHome_Dir = ""; ui1.sComment = description; ui1.uiFlags = flags | UF_NORMAL_ACCOUNT; ui1.sScript_Path = ""; ui11.usri1011_full_name = fullname; IntPtr bufptr_1 = Marshal.AllocHGlobal(Marshal.SizeOf(ui1)); IntPtr bufptr_1011 = Marshal.AllocHGlobal(Marshal.SizeOf(ui11)); IntPtr bufptr_parm_err = Marshal.AllocHGlobal(Marshal.SizeOf(parm_err)); try { if (!NetApiInitCalled) { result = NetApiInit(); if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS) { return result; } NetApiInitCalled = true; } Marshal.StructureToPtr(ui1, bufptr_1, false); Marshal.StructureToPtr(ui11, bufptr_1011, false); Marshal.StructureToPtr(parm_err, bufptr_parm_err, false); result = (uint)NetUserAdd(servername, 1, bufptr_1, bufptr_parm_err); if (result != 0) { return result; } if (fullname != null) { result = (uint)NetUserSetInfo(servername, username, 1011, bufptr_1011, bufptr_parm_err); if (result != 0) { return result; } } } catch (Exception) { result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE; } finally { try { Marshal.DestroyStructure(bufptr_1, ui1.GetType()); Marshal.FreeHGlobal(bufptr_1); Marshal.DestroyStructure(bufptr_1011, ui11.GetType()); Marshal.FreeHGlobal(bufptr_1011); //If this is uncommented, it results in a crash //TODO: figure out why it's not possible to free bufptr_parm_err //Marshal.FreeHGlobal(bufptr_parm_err); } catch (Exception) { result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE; } } return result; }
NetEditUserFullName( string servername, string username, string fullname ) { uint result = (uint)LUGAPI.WinError.ERROR_SUCCESS; if (String.IsNullOrEmpty(servername)) { servername = null; } if (String.IsNullOrEmpty(username)) { username = null; } if (String.IsNullOrEmpty(fullname)) { fullname = null; } USER_INFO_1011 userinfo_1011 = new USER_INFO_1011(); userinfo_1011.usri1011_full_name = fullname; IntPtr bufptr1011 = IntPtr.Zero; bufptr1011 = Marshal.AllocHGlobal(Marshal.SizeOf(userinfo_1011)); try { if (!NetApiInitCalled) { result = NetApiInit(); if (result != (uint)LUGAPI.WinError.ERROR_SUCCESS) { return result; } NetApiInitCalled = true; } Marshal.StructureToPtr(userinfo_1011, bufptr1011, false); result = (uint)NetUserSetInfo(servername, username, 1011, bufptr1011, IntPtr.Zero); } catch (Exception) { result = (uint)LUGAPI.WinError.ERROR_EXCEPTION_IN_SERVICE; } finally { Marshal.DestroyStructure(bufptr1011, userinfo_1011.GetType()); Marshal.FreeHGlobal(bufptr1011); } return result; }