Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provide custom marshaller for USER_INFO_1 #82350

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
#if NET7_0_OR_GREATER
using System.Runtime.InteropServices.Marshalling;
#endif
using System.Security.Cryptography;
using System.Security.Principal;
using System.Threading.Tasks;
Expand Down Expand Up @@ -118,12 +121,15 @@ private void CreateUser()
[return: MarshalAs(UnmanagedType.Bool)]
private static partial bool LogonUser(string userName, string domain, string password, int logonType, int logonProvider, out SafeAccessTokenHandle safeAccessTokenHandle);

[DllImport("netapi32.dll", SetLastError = true)]
internal static extern uint NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, uint level, ref USER_INFO_1 buf, out uint parm_err);
[LibraryImport("netapi32.dll", SetLastError = true)]
internal static partial uint NetUserAdd([MarshalAs(UnmanagedType.LPWStr)] string servername, uint level, ref USER_INFO_1 buf, out uint parm_err);

[LibraryImport("netapi32.dll")]
internal static partial uint NetUserDel([MarshalAs(UnmanagedType.LPWStr)] string servername, [MarshalAs(UnmanagedType.LPWStr)] string username);

#if NET7_0_OR_GREATER
[NativeMarshalling(typeof(USER_INFO_1.Marshaller))]
#endif
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
internal struct USER_INFO_1
{
Expand All @@ -135,6 +141,65 @@ internal struct USER_INFO_1
public string usri1_comment;
public uint usri1_flags;
public string usri1_script_path;

#if NET7_0_OR_GREATER
[CustomMarshaller(typeof(USER_INFO_1), MarshalMode.Default, typeof(Marshaller))]
public static class Marshaller
{
public static USER_INFO_1Native ConvertToUnmanaged(USER_INFO_1 managed) => new(managed);
public static USER_INFO_1 ConvertToManaged(USER_INFO_1Native native) => native.ToManaged();
public static void Free(USER_INFO_1Native native) => native.FreeNative();

[StructLayout(LayoutKind.Sequential)]
public struct USER_INFO_1Native
{
private IntPtr usri1_name;
private IntPtr usri1_password;
private uint usri1_password_age;
private uint usri1_priv;
private IntPtr usri1_home_dir;
private IntPtr usri1_comment;
private uint usri1_flags;
private IntPtr usri1_script_path;

public USER_INFO_1Native(USER_INFO_1 managed)
{
usri1_name = Marshal.StringToCoTaskMemUni(managed.usri1_name);
usri1_password = Marshal.StringToCoTaskMemUni(managed.usri1_password);
usri1_password_age = managed.usri1_password_age;
usri1_priv = managed.usri1_priv;
usri1_home_dir = Marshal.StringToCoTaskMemUni(managed.usri1_home_dir);
usri1_comment = Marshal.StringToCoTaskMemUni(managed.usri1_comment);
usri1_flags = managed.usri1_flags;
usri1_script_path = Marshal.StringToCoTaskMemUni(managed.usri1_script_path);
}

public USER_INFO_1 ToManaged()
{
return new USER_INFO_1
{
usri1_name = Marshal.PtrToStringUni(usri1_name),
usri1_password = Marshal.PtrToStringUni(usri1_password),
usri1_password_age = usri1_password_age,
usri1_priv = usri1_priv,
usri1_home_dir = Marshal.PtrToStringUni(usri1_home_dir),
usri1_comment = Marshal.PtrToStringUni(usri1_comment),
usri1_flags = usri1_flags,
usri1_script_path = Marshal.PtrToStringUni(usri1_script_path)
};
}

public void FreeNative()
{
Marshal.FreeCoTaskMem(usri1_name);
Marshal.FreeCoTaskMem(usri1_password);
Marshal.FreeCoTaskMem(usri1_home_dir);
Marshal.FreeCoTaskMem(usri1_comment);
Marshal.FreeCoTaskMem(usri1_script_path);
}
}
}
#endif
}

public void Dispose()
Expand Down