Пример #1
0
        public static RemoteNameInfo GetRemoteUniversalName(string localPath)
        {
            int length = 0;
            //WNetGetUniversalName don't allow buffer is null
            IntPtr dummyBuffer = Marshal.AllocCoTaskMem(5);

            try
            {
                GenerateExceptionIfError(WNet_Api.WNetGetUniversalName(localPath, WNet_Api.NameInfoLevel.Remote, dummyBuffer, ref length), WNetErrors.NoError, WNetErrors.NoMoreData);
            }
            finally
            {
                Marshal.FreeCoTaskMem(dummyBuffer);
            }
            IntPtr buffer = Marshal.AllocCoTaskMem(length);

            try
            {
                GenerateExceptionIfError(WNet_Api.WNetGetUniversalName(localPath, WNet_Api.NameInfoLevel.Remote, buffer, ref length));
                return((RemoteNameInfo)Marshal.PtrToStructure(buffer, typeof(RemoteNameInfo)));
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
            }
        }
Пример #2
0
        public static NetInfo GetNetworkInformation(string provider)
        {
            NetInfo netInfo = new NetInfo();

            netInfo.StructureSize = Marshal.SizeOf(typeof(NetInfo));
            GenerateExceptionIfError(WNet_Api.WNetGetNetworkInformation(provider, netInfo));
            return(netInfo);
        }
Пример #3
0
        public static ConnectInfo GetConnectionPerformance(NetResource netResource)
        {
            ConnectInfo info = new ConnectInfo();

            info.StructureSize = Marshal.SizeOf(typeof(ConnectInfo));
            GenerateExceptionIfError(WNet_Api.MultinetGetConnectionPerformance(netResource, info));
            return(info);
        }
Пример #4
0
//		public static DialogResult DisconnectWithDialog(DisconnectDialogInfo info)
//		{
//			info.Size = Marshal.SizeOf(typeof(DisconnectDialogInfo));
//			return MakeDialogResultOrGenerateException(WNet_Api.WNetDisconnectDialog1(info));
//		}

        public static string GetConnectionRemoteName(string localName)
        {
            int length = 0;

            GenerateExceptionIfError(WNet_Api.WNetGetConnection(localName, null, ref length), WNetErrors.NoError, WNetErrors.NoMoreData);
            StringBuilder remoteName = new StringBuilder(length);

            GenerateExceptionIfError(WNet_Api.WNetGetConnection(localName, remoteName, ref length));
            return(remoteName.ToString());
        }
Пример #5
0
        public static string GetUser(string name)
        {
            int length = 0;

            GenerateExceptionIfError(WNet_Api.WNetGetUser(name, null, ref length), WNetErrors.NoError, WNetErrors.NoMoreData);
            StringBuilder user = new StringBuilder(length);

            GenerateExceptionIfError(WNet_Api.WNetGetUser(name, user, ref length));
            return(user.ToString());
        }
Пример #6
0
        public static string GetProviderName(NetworkType netType)
        {
            int length = 0;

            GenerateExceptionIfError(WNet_Api.WNetGetProviderName(netType, null, ref length), WNetErrors.NoError, WNetErrors.NoMoreData);
            StringBuilder providerName = new StringBuilder(length);

            GenerateExceptionIfError(WNet_Api.WNetGetProviderName(netType, providerName, ref length));
            return(providerName.ToString());
        }
Пример #7
0
        public static void GetLastError(out int errorCode, out string errorText, out string providerName)
        {
            const int     maxLength = 1000;
            StringBuilder text      = new StringBuilder(maxLength);
            StringBuilder provider  = new StringBuilder(maxLength);

            GenerateExceptionIfError(WNet_Api.WNetGetLastError(out errorCode, out text, text.Capacity,
                                                               out provider, provider.Capacity));
            errorText    = text.ToString();
            providerName = provider.ToString();
        }
Пример #8
0
        public static string UseConnection(Control ownerWindow, NetResource netResource,
                                           string password, string userId, ConnectionFlags flags)
        {
            StringBuilder accessName = new StringBuilder(0);
            int           length     = 0;
            int           result     = 0;

            GenerateExceptionIfError(WNet_Api.WNetUseConnection(Common.ControlToHwnd(ownerWindow), netResource,
                                                                password, userId, flags, accessName, ref length, ref result), WNetErrors.NoError, WNetErrors.NoMoreData);
            accessName = new StringBuilder(length);
            GenerateExceptionIfError(WNet_Api.WNetUseConnection(Common.ControlToHwnd(ownerWindow), netResource,
                                                                password, userId, flags, accessName, ref length, ref result));
            return(accessName.ToString());
        }
Пример #9
0
        public static NetResource[] EnumResources(ResourceScope scope, ResourceType type,
                                                  ResourceUsage usage, NetResource target)
        {
            IntPtr enumHandle;

            GenerateExceptionIfError(WNet_Api.WNetOpenEnum(scope, type, usage, target, out enumHandle));
            try
            {
                const int maxBufferSize = 20000;
                IntPtr    buffer        = Marshal.AllocCoTaskMem(maxBufferSize);
                try
                {
                    NetResource[] results = new NetResource[] {};
                    for (;;)
                    {
                        int bufferSize = maxBufferSize;
                        int count      = -1;
                        int error      = WNet_Api.WNetEnumResource(enumHandle, ref count, buffer, ref bufferSize);
                        if (error == WNetErrors.NoMoreItems)
                        {
                            break;
                        }
                        if (error != WNetErrors.NoError)
                        {
                            GenerateException(error);
                        }
                        if (count < 0)
                        {
                            continue;
                        }
                        int index = results.Length;
                        results = Realloc(results, index + count);
                        for (int i = 0; i < count; ++i)
                        {
                            results[index + i] = (NetResource)Marshal.PtrToStructure(new IntPtr(buffer.ToInt64() + i * Marshal.SizeOf(typeof(NetResource))), typeof(NetResource));
                        }
                    }
                    return(results);
                }
                finally
                {
                    Marshal.FreeCoTaskMem(buffer);
                }
            }
            finally
            {
                WNet_Api.WNetCloseEnum(enumHandle);
            }
        }
Пример #10
0
        public static NetResource GetResourceParent(NetResource netResource)
        {
            int length = 0;

            GenerateExceptionIfError(WNet_Api.WNetGetResourceParent(netResource, IntPtr.Zero, ref length), WNetErrors.NoError, WNetErrors.NoMoreData);
            IntPtr buffer = Marshal.AllocCoTaskMem(length);

            try
            {
                GenerateExceptionIfError(WNet_Api.WNetGetResourceParent(netResource, buffer, ref length));
                return((NetResource)Marshal.PtrToStructure(buffer, typeof(NetResource)));
            }
            finally
            {
                Marshal.FreeCoTaskMem(buffer);
            }
        }
Пример #11
0
 public static DialogResult DisconnectWithDialog(Control ownerWindow, ResourceType type)
 {
     return(MakeDialogResultOrGenerateException(WNet_Api.WNetDisconnectDialog(
                                                    Common.ControlToHwnd(ownerWindow), type)));
 }
Пример #12
0
 public static DialogResult ConnectionDialog(ConnectDialogInfo info)
 {
     info.StructureSize = Marshal.SizeOf(typeof(ConnectDialogInfo));
     return(MakeDialogResultOrGenerateException(WNet_Api.WNetConnectionDialog1(info)));
 }
Пример #13
0
 public static void CancelConnection(string name, ConnectionFlags options, bool isForce)
 {
     GenerateExceptionIfError(WNet_Api.WNetCancelConnection2(name, options, isForce));
 }
Пример #14
0
 public static void AddConnection(Control ownerWindow, NetResource netResource, string password,
                                  string userName, ConnectionFlags options)
 {
     GenerateExceptionIfError(WNet_Api.WNetAddConnection3(Common.ControlToHwnd(ownerWindow),
                                                          netResource, password, userName, options));
 }
Пример #15
0
 public static void AddConnection(NetResource netResource, string password, string userName,
                                  ConnectionFlags options)
 {
     GenerateExceptionIfError(WNet_Api.WNetAddConnection2(netResource, password, userName, options));
 }
Пример #16
0
 public static void AddConnection(string remoteName, string password, string localName)
 {
     GenerateExceptionIfError(WNet_Api.WNetAddConnection(remoteName, password, localName));
 }