|
| 1 | +using System.Net; |
| 2 | +using System.Runtime.InteropServices; |
| 3 | +using DSInternals.Common; |
| 4 | +using DSInternals.Common.Interop; |
| 5 | +using Windows.Win32.Foundation; |
| 6 | +using Windows.Win32.NetworkManagement.WNet; |
| 7 | + |
| 8 | +namespace DSInternals.SAM.Interop; |
| 9 | + |
| 10 | +/// <summary> |
| 11 | +/// Contains P/Invoke signatures for mpr.dll functions. |
| 12 | +/// </summary> |
| 13 | +internal static partial class NativeMethods |
| 14 | +{ |
| 15 | + private const string Mpr = "mpr.dll"; |
| 16 | + |
| 17 | + /// <summary> |
| 18 | + /// Makes a connection to a network resource using the specified credentials. |
| 19 | + /// </summary> |
| 20 | + /// <param name="shareName">The remote network resource to connect to (e.g., \\server\IPC$).</param> |
| 21 | + /// <param name="credential">The credentials to use for the connection, or <c>null</c> to use the default credentials.</param> |
| 22 | + /// <param name="flags">A set of connection options.</param> |
| 23 | + internal static unsafe Win32ErrorCode WNetAddConnection2(string shareName, NetworkCredential? credential, NET_CONNECT_FLAGS flags) |
| 24 | + { |
| 25 | + fixed (char* remoteNamePtr = shareName) |
| 26 | + { |
| 27 | + NETRESOURCEW resource = new() |
| 28 | + { |
| 29 | + dwScope = NET_RESOURCE_SCOPE.RESOURCE_GLOBALNET, |
| 30 | + dwType = NET_RESOURCE_TYPE.RESOURCETYPE_ANY, |
| 31 | + lpRemoteName = new PWSTR(remoteNamePtr) |
| 32 | + }; |
| 33 | + |
| 34 | + string? userName = credential?.GetLogonName(); |
| 35 | + IntPtr passwordPtr = credential != null |
| 36 | + ? Marshal.SecureStringToGlobalAllocUnicode(credential.SecurePassword) |
| 37 | + : IntPtr.Zero; |
| 38 | + |
| 39 | + try |
| 40 | + { |
| 41 | + return WNetAddConnection2(resource, passwordPtr, userName, flags); |
| 42 | + } |
| 43 | + finally |
| 44 | + { |
| 45 | + if (passwordPtr != IntPtr.Zero) |
| 46 | + { |
| 47 | + Marshal.ZeroFreeGlobalAllocUnicode(passwordPtr); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + /// <see>https://learn.microsoft.com/windows/win32/api/winnetwk/nf-winnetwk-wnetaddconnection2w</see> |
| 54 | + [DllImport(Mpr, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "WNetAddConnection2W")] |
| 55 | + private static extern Win32ErrorCode WNetAddConnection2(in NETRESOURCEW netResource, IntPtr password, string? userName, NET_CONNECT_FLAGS flags); |
| 56 | + |
| 57 | + /// <summary> |
| 58 | + /// The WNetCancelConnection2 function cancels an existing network connection. You can also call the function to remove remembered network connections that are not currently connected. |
| 59 | + /// </summary> |
| 60 | + /// <param name="name">The name of either the redirected local device or the remote network resource to disconnect from.</param> |
| 61 | + /// <param name="flags">Connection type.</param> |
| 62 | + /// <param name="force">Specifies whether the disconnection should occur if there are open files or jobs on the connection.</param> |
| 63 | + /// <see>https://learn.microsoft.com/windows/win32/api/winnetwk/nf-winnetwk-wnetcancelconnection2w</see> |
| 64 | + [DllImport(Mpr, CharSet = CharSet.Unicode, SetLastError = true, EntryPoint = "WNetCancelConnection2W")] |
| 65 | + internal static extern Win32ErrorCode WNetCancelConnection2(string name, NetCancelOptions flags, [MarshalAs(UnmanagedType.Bool)] bool force); |
| 66 | +} |
0 commit comments