Пример #1
0
        public static void AddConnection
            (IntPtr owner_window,
            string remote_name,
            string provider,
            ResourceType resource_type,
            bool remember)
        {
            var net_struct = new NETRESOURCE();

            net_struct.dwType       = resource_type;
            net_struct.lpProvider   = provider;
            net_struct.lpRemoteName = remote_name;

            var opts = WNetConnectOptions.INTERACTIVE;

            if (remember)
            {
                opts = opts | WNetConnectOptions.UPDATE_PROFILE;
            }

            var res = WinApiWNET.WNetAddConnection3
                          (owner_window,
                          ref net_struct,
                          string.Empty,
                          string.Empty,
                          opts);

            if (res != WinApiWNET.NO_ERROR)
            {
                throw new Win32Exception(res);
            }
        }
Пример #2
0
            private void open_enum()
            {
                uint res = 0;

                res = WinApiWNET.WNetOpenEnum
                          (inittial_scope,
                          initial_type,
                          initial_usage,
                          ref resource_root,
                          ref enum_handle);

                //check result
                if (res != WinApiWNET.NO_ERROR)
                {
                    if (res == WinApiWNET.ERROR_EXTENDED_ERROR)
                    {
                        WNetException.ThrowOnError();
                    }
                    else
                    {
                        throw new Win32Exception((int)res);
                    }
                }
                //allocate buffer
                buffer = Marshal.AllocHGlobal(buffer_size);
            }
Пример #3
0
        public static NETINFOSTRUCT GetNetworkInfo(string provider)
        {
            var ret = NETINFOSTRUCT.Prepare();
            var res = WinApiWNET.WNetGetNetworkInformation(provider, ref ret);

            if (res != WinApiWNET.NO_ERROR)
            {
                throw new Win32Exception(res);
            }
            return(ret);
        }
Пример #4
0
 public void Dispose()
 {
     if (buffer != IntPtr.Zero)
     {
         Marshal.FreeHGlobal(buffer);
     }
     if (enum_handle != IntPtr.Zero)
     {
         WinApiWNET.WNetCloseEnum(enum_handle);
     }
 }
Пример #5
0
        private static NETRESOURCE GetResourceInfo(NETRESOURCE resource, int buffer_size)
        {
            var info_buffer = IntPtr.Zero;
            //int buffer_size = 512;
            var system_ptr = IntPtr.Zero;

            try
            {
                info_buffer = Marshal.AllocHGlobal(buffer_size);

                var res = WinApiWNET.WNetGetResourceInformation
                              (ref resource,
                              info_buffer,
                              ref buffer_size,
                              ref system_ptr);
                //check res
                switch (res)
                {
                case WinApiWNET.ERROR_EXTENDED_ERROR:
                    WNetException.ThrowOnError();
                    return(new NETRESOURCE());

                case WinApiWNET.ERROR_MORE_DATA:
                    //alloc new buffer
                    Marshal.FreeHGlobal(info_buffer);
                    return(GetResourceInfo(resource, buffer_size));

                case WinApiWNET.NO_ERROR:
                    var ret = new NETRESOURCE();
                    NETRESOURCE.FromBuffer(info_buffer, 0, ref ret);
                    return(ret);

                default:
                    throw new Win32Exception((int)res);
                }
            }
            finally
            {
                if (info_buffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(info_buffer);
                }
            }
        }
Пример #6
0
            private bool continue_enum()
            {
                int req_entries = requested_entries;

                uint res = WinApiWNET.WNetEnumResource
                               (enum_handle,
                               ref req_entries,
                               buffer,
                               ref buffer_size);

                //check result
                switch (res)
                {
                case WinApiWNET.NO_ERROR:
                    //success
                    current_entries_in_buffer        = req_entries;
                    current_entries_in_buffer_readed = 0;
                    return(true);

                case WinApiWNET.ERROR_NO_MORE_ITEMS:
                    //enumerate comletes
                    current_entries_in_buffer        = 0;
                    current_entries_in_buffer_readed = 0;
                    return(false);

                case WinApiWNET.ERROR_MORE_DATA:
                    //buffer small?
                    //allocate new buffer
                    Marshal.FreeHGlobal(buffer);
                    buffer = Marshal.AllocHGlobal(buffer_size);
                    //and recall
                    return(continue_enum());

                case WinApiWNET.ERROR_EXTENDED_ERROR:
                    WNetException.ThrowOnError();
                    return(false);

                default:
                    throw new Win32Exception((int)res);
                    //return false;
                }
            }
Пример #7
0
        public static void ThrowOnError()
        {
            uint errCode               = 0;
            var  errBuffer             = IntPtr.Zero;
            var  nameBuffer            = IntPtr.Zero;
            ApplicationException netEx = null;

            try
            {
                errBuffer  = Marshal.AllocHGlobal(buffer_size * Marshal.SystemDefaultCharSize);
                nameBuffer = Marshal.AllocHGlobal(buffer_size * Marshal.SystemDefaultCharSize);
                var res = WinApiWNET.WNetGetLastError
                              (ref errCode,
                              errBuffer,
                              buffer_size,
                              nameBuffer,
                              buffer_size);
                if (res == WinApiWNET.NO_ERROR)
                {
                    netEx        = new ApplicationException(Marshal.PtrToStringAuto(errBuffer));
                    netEx.Source = Marshal.PtrToStringAuto(nameBuffer);
                    throw netEx;
                }
                else
                {
                    throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error());
                }
            }
            finally
            {
                if (errBuffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(errBuffer);
                }
                if (nameBuffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(nameBuffer);
                }
            }
        }
Пример #8
0
        public static NETRESOURCE GetParentResource(NETRESOURCE resource)
        {
            var buffer      = IntPtr.Zero;
            var buffer_size = 256;
            var ret         = new NETRESOURCE();

            try
            {
                buffer = Marshal.AllocHGlobal(buffer_size);
                var res = WinApiWNET.WNetGetResourceParent
                              (ref resource,
                              buffer,
                              ref buffer_size);
                if (res == WinApiWNET.ERROR_MORE_DATA)
                {
                    res = WinApiWNET.WNetGetResourceParent
                              (ref resource,
                              buffer,
                              ref buffer_size);
                }
                if (res == WinApiWNET.ERROR_EXTENDED_ERROR)
                {
                    WNetException.ThrowOnError();
                }
                if (res != WinApiWNET.NO_ERROR)
                {
                    throw new Win32Exception((int)res);
                }
                NETRESOURCE.FromBuffer(buffer, 0, ref ret);
                return(ret);
            }
            finally
            {
                if (buffer != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(buffer);
                }
            }
        }