File size: 1,083 Bytes
8c763fb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

#nullable enable

using System.Runtime.InteropServices;

internal static partial class Interop
{
    internal static unsafe partial class Windows
    {
        [LibraryImport("mpr.dll", EntryPoint = "WNetCancelConnection2W", StringMarshalling = StringMarshalling.Utf16)]
        internal static partial int WNetCancelConnection2W(string driveName, int flags, [MarshalAs(UnmanagedType.Bool)] bool force);

        internal static int WNetCancelConnection2(string driveName, int flags, bool force)
        {
            if (s_WNetApiNotAvailable)
            {
                return ERROR_NOT_SUPPORTED;
            }

            int errorCode = ERROR_NO_NETWORK;

            try
            {
                errorCode = WNetCancelConnection2W(driveName, flags, force: true);
            }
            catch (System.DllNotFoundException)
            {
                s_WNetApiNotAvailable = true;
                return ERROR_NOT_SUPPORTED;
            }

            return errorCode;
        }
    }
}