Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
WNetAddConnection2.cs74 linesDownload Raw Back to Windows
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4#nullable enable5 6using System.Runtime.InteropServices;7 8internal static partial class Interop9{10    internal static partial class Windows11    {12        internal const int CONNECT_NOPERSIST = 0x00000000;13        internal const int CONNECT_UPDATE_PROFILE = 0x00000001;14        internal const int RESOURCE_GLOBALNET = 0x00000002;15        internal const int RESOURCETYPE_ANY = 0x00000000;16        internal const int RESOURCEDISPLAYTYPE_GENERIC = 0x00000000;17        internal const int RESOURCEUSAGE_CONNECTABLE = 0x00000001;18 19        [StructLayout(LayoutKind.Sequential)]20        internal unsafe struct NETRESOURCEW21        {22            public int Scope;23            public int Type;24            public int DisplayType;25            public int Usage;26            public char* LocalName;27            public char* RemoteName;28            public char* Comment;29            public char* Provider;30        }31 32        [LibraryImport("mpr.dll", EntryPoint = "WNetAddConnection2W", StringMarshalling = StringMarshalling.Utf16)]33        internal static partial int WNetAddConnection2(ref NETRESOURCEW netResource, byte[] password, string userName, int flags);34 35        internal static unsafe int WNetAddConnection2(string localName, string remoteName, byte[] password, string userName, int connectType)36        {37            if (s_WNetApiNotAvailable)38            {39                return ERROR_NOT_SUPPORTED;40            }41 42            int errorCode = ERROR_NO_NETWORK;43 44            fixed (char* pinnedLocalName = localName)45            fixed (char* pinnedRemoteName = remoteName)46            {47                NETRESOURCEW resource = new NETRESOURCEW()48                {49                    Comment = null,50                    DisplayType = RESOURCEDISPLAYTYPE_GENERIC,51                    LocalName = pinnedLocalName,52                    Provider = null,53                    RemoteName = pinnedRemoteName,54                    Scope = RESOURCE_GLOBALNET,55                    Type = RESOURCETYPE_ANY,56                    Usage = RESOURCEUSAGE_CONNECTABLE57                };58 59                try60                {61                    errorCode = WNetAddConnection2(ref resource, password, userName, connectType);62                }63                catch (System.DllNotFoundException)64                {65                    s_WNetApiNotAvailable = true;66                    return ERROR_NOT_SUPPORTED;67                }68            }69 70            return errorCode;71        }72    }73}74