ProjectArcade

Форк
0
227 строк · 14.0 Кб
1
using System;
2
using System.Runtime.InteropServices;
3

4
/// <summary>
5
/// Namespace for structures and classes related to native API.
6
/// </summary>
7
namespace DokanNet.Native
8
{
9
    /// <summary>
10
    /// Native API to the kernel Dokan driver.
11
    /// </summary>
12
    internal static class NativeMethods
13
    {
14
        private const string DOKAN_DLL = "dokan2.dll";
15

16
        /// <summary>
17
        /// Initialize all required Dokan internal resources.
18
        /// 
19
        /// This needs to be called only once before trying to use <see cref="DokanMain"/> or <see cref="DokanCreateFileSystem"/> for the first time.
20
        /// Otherwise both will fail and raise an exception.
21
        /// </summary>
22
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
23
        public static extern void DokanInit();
24

25
        /// <summary>
26
        /// Release all allocated resources by <see cref="DokanInit"/> when they are no longer needed.
27
        ///
28
        /// This should be called when the application no longer expects to create a new FileSystem with
29
        /// <see cref="DokanMain"/> or <see cref="DokanCreateFileSystem"/> and after all devices are unmount.
30
        /// </summary>
31
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
32
        public static extern void DokanShutdown();
33

34
        /// <summary>
35
        /// Mount a new Dokan Volume.
36
        /// This function block until the device is unmount.
37
        /// If the mount fail, it will directly return an error.
38
        /// </summary>
39
        /// <param name="options">A <see cref="DOKAN_OPTIONS"/> that describe the mount.</param>
40
        /// <param name="operations">Instance of <see cref="DOKAN_OPERATIONS"/> that will be called for each request made by the kernel.</param>
41
        /// <returns><see cref="DokanStatus"/></returns>
42
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
43
        public static extern DokanStatus DokanMain([In] DOKAN_OPTIONS options, [In] DOKAN_OPERATIONS operations);
44

45
        /// <summary>
46
        /// Mount a new Dokan Volume.
47
        /// 
48
        /// It is mandatory to have called <see cref="DokanInit"/> previously to use this API.
49
        /// This function returns directly on device mount or on failure.
50
        /// <see cref="DokanWaitForFileSystemClosed"/> can be used to wait until the device is unmount.
51
        /// </summary>
52
        /// <param name="options">A <see cref="NativeStructWrapper&lt;DOKAN_OPTIONS&gt;"/> that describe the mount.</param>
53
        /// <param name="operations">Instance of <see cref="NativeStructWrapper&lt;DOKAN_OPERATIONS&gt;"/> that will be called for each request made by the kernel.</param>
54
        /// <param name="dokanInstance">Dokan mount instance context that can be used for related instance calls like <see cref="DokanIsFileSystemRunning"/>.</param>
55
        /// <returns><see cref="DokanStatus"/></returns>
56
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
57
        public static extern DokanStatus DokanCreateFileSystem(SafeBuffer options, SafeBuffer operations, out DokanHandle dokanInstance);
58

59
        /// <summary>
60
        /// Check if the FileSystem is still running or not.
61
        /// </summary>
62
        /// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/>.</param>
63
        /// <returns>Whether the FileSystem is still running or not.</returns>
64
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
65
        public static extern bool DokanIsFileSystemRunning(DokanHandle dokanInstance);
66

67
        /// <summary>
68
        /// Wait until the FileSystem is unmount.
69
        /// </summary>
70
        /// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/>.</param>
71
        /// <param name="milliSeconds">The time-out interval, in milliseconds. If a nonzero value is specified, the function waits until the object is signaled or the interval elapses. If <param name="milliSeconds"> is zero,
72
        /// the function does not enter a wait state if the object is not signaled; it always returns immediately. If <param name="milliSeconds"> is INFINITE, the function will return only when the object is signaled.</param>
73
        /// <returns>See <a href="https://docs.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject">WaitForSingleObject</a> for a description of return values.</returns>
74
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
75
        public static extern uint DokanWaitForFileSystemClosed(DokanHandle dokanInstance, uint milliSeconds);
76

77
        /// <summary>
78
        /// Unmount and wait until all resources of the \c DokanInstance are released.
79
        /// </summary>
80
        /// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/>.</param>
81
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
82
        public static extern void DokanCloseHandle(IntPtr dokanInstance);
83

84
        /// <summary>
85
        /// Unmount a dokan device from a driver letter.
86
        /// </summary>
87
        /// <param name="driveLetter">Dokan driver letter to unmount.</param>
88
        /// <returns><c>True</c> if device was unmount or <c>false</c> in case of failure or device not found.</returns>
89
        [DllImport(DOKAN_DLL, ExactSpelling = true, CharSet = CharSet.Unicode)]
90
        public static extern bool DokanUnmount(char driveLetter);
91

92
        /// <summary>
93
        /// Get the version of Dokan.
94
        /// The returned <see cref="uint"/> is the version number without the dots.
95
        /// </summary>
96
        /// <returns>The version of Dokan</returns>
97
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
98
        public static extern uint DokanVersion();
99

100
        /// <summary>
101
        /// Get the version of the Dokan driver.
102
        /// The returned <see cref="uint"/> is the version number without the dots.
103
        /// </summary>
104
        /// <returns>The version of Dokan driver.</returns>
105
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
106
        public static extern uint DokanDriverVersion();
107

108
        /// <summary>
109
        /// Unmount a dokan device from a mount point
110
        /// </summary>
111
        /// <param name="mountPoint">MountPoint Mount point to unmount ("<c>Z</c>", 
112
        /// "<c>Z:</c>", "<c>Z:\\</c>", "<c>Z:\MyMountPoint</c>").</param>
113
        /// <returns><c>True</c> if device was unmount or <c>false</c> in case of failure or device not found.</returns>
114
        [DllImport(DOKAN_DLL, ExactSpelling = true, CharSet = CharSet.Unicode)]
115
        public static extern bool DokanRemoveMountPoint([MarshalAs(UnmanagedType.LPWStr)] string mountPoint);
116

117
        /// <summary>
118
        /// Extends the time out of the current IO operation in driver.
119
        /// </summary>
120
        /// <param name="timeout">Extended time in milliseconds requested.</param>
121
        /// <param name="rawFileInfo"><see cref="DokanFileInfo"/> of the operation to extend.</param>
122
        /// <returns>If the operation was successful.</returns>
123
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
124
        [return: MarshalAs(UnmanagedType.Bool)]
125
        public static extern bool DokanResetTimeout(uint timeout, DokanFileInfo rawFileInfo);
126

127
        /// <summary>
128
        /// Get the handle to Access Token.
129
        /// This method needs be called in <see cref="IDokanOperations.CreateFile"/> callback.
130
        /// The caller must call <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/ms724211(v=vs.85).aspx">CloseHandle (MSDN)</a> for the returned handle.
131
        /// </summary>
132
        /// <param name="rawFileInfo">
133
        /// A <see cref="DokanFileInfo"/> of the operation to extend.
134
        /// </param>
135
        /// <returns>
136
        /// A handle to the account token for the user on whose behalf the code is running.
137
        /// </returns>
138
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
139
        public static extern IntPtr DokanOpenRequestorToken(DokanFileInfo rawFileInfo);
140

141
        /// <summary>
142
        /// Convert <see cref="DokanOperationProxy.ZwCreateFileDelegate"/> parameters to <see cref="IDokanOperations.CreateFile"/> parameters.
143
        /// </summary>
144
        /// <param name="desiredAccess">DesiredAccess from <see cref="DokanOperationProxy.ZwCreateFileDelegate"/>.</param>
145
        /// <param name="fileAttributes">FileAttributes from <see cref="DokanOperationProxy.ZwCreateFileDelegate"/>.</param>
146
        /// <param name="createOptions">CreateOptions from <see cref="DokanOperationProxy.ZwCreateFileDelegate"/>.</param>
147
        /// <param name="createDisposition">CreateDisposition from <see cref="DokanOperationProxy.ZwCreateFileDelegate"/>.</param>
148
        /// <param name="outDesiredAccess">New <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx">CreateFile (MSDN)</a> dwDesiredAccess.</param>
149
        /// <param name="outFileAttributesAndFlags">New <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx">CreateFile (MSDN)</a> dwFlagsAndAttributes.</param>
150
        /// <param name="outCreationDisposition">New <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx">CreateFile (MSDN)</a> dwCreationDisposition.</param>
151
        /// \see <a href="https://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx">CreateFile function (MSDN)</a>
152
        [DllImport(DOKAN_DLL, ExactSpelling = true)]
153
        public static extern void DokanMapKernelToUserCreateFileFlags(
154
            uint desiredAccess,
155
            uint fileAttributes,
156
            uint createOptions,
157
            uint createDisposition,
158
            ref uint outDesiredAccess,
159
            ref int outFileAttributesAndFlags,
160
            ref int outCreationDisposition);
161

162
        /*
163
        [DllImport(DOKAN_DLL, CharSet = CharSet.Unicode)]
164
        public static extern bool DokanIsNameInExpression([MarshalAs(UnmanagedType.LPWStr)] string expression,
165
                                                          // matching pattern
166
                                                          [MarshalAs(UnmanagedType.LPWStr)] string name, // file name
167
                                                          [MarshalAs(UnmanagedType.Bool)] bool ignoreCase);*/
168

169
        /// <summary>
170
        /// Notify Dokan that a file or directory has been created.
171
        /// </summary>
172
        /// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
173
        /// <param name="filePath">Full path to the file or directory, including mount point.</param>
174
        /// <param name="isDirectory">Indicates if the path is a directory.</param>
175
        /// <returns>true if the notification succeeded.</returns>
176
        [DllImport(DOKAN_DLL, CharSet = CharSet.Unicode)]
177
        public static extern bool DokanNotifyCreate(DokanHandle dokanInstance,
178
                                                    [MarshalAs(UnmanagedType.LPWStr)] string filePath,
179
                                                    [MarshalAs(UnmanagedType.Bool)] bool isDirectory);
180

181
        /// <summary>
182
        /// Notify Dokan that a file or directory has been deleted.
183
        /// </summary>
184
        /// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
185
        /// <param name="filePath">Full path to the file or directory, including mount point.</param>
186
        /// <param name="isDirectory">Indicates if the path is a directory.</param>
187
        /// <returns>true if notification succeeded.</returns>
188
        [DllImport(DOKAN_DLL, CharSet = CharSet.Unicode)]
189
        public static extern bool DokanNotifyDelete(DokanHandle dokanInstance, [MarshalAs(UnmanagedType.LPWStr)] string filePath,
190
                                                    [MarshalAs(UnmanagedType.Bool)] bool isDirectory);
191

192
        /// <summary>
193
        /// Notify Dokan that file or directory attributes have changed.
194
        /// </summary>
195
        /// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
196
        /// <param name="filePath">Full path to the file or directory, including mount point.</param>
197
        /// <returns>true if notification succeeded.</returns>
198
        [DllImport(DOKAN_DLL, CharSet = CharSet.Unicode)]
199
        public static extern bool DokanNotifyUpdate(DokanHandle dokanInstance, [MarshalAs(UnmanagedType.LPWStr)] string filePath);
200

201
        /// <summary>
202
        /// Notify Dokan that file or directory extended attributes have changed.
203
        /// </summary>
204
        /// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
205
        /// <param name="filePath">Full path to the file or directory, including mount point.</param>
206
        /// <returns>true if notification succeeded.</returns>
207
        [DllImport(DOKAN_DLL, CharSet = CharSet.Unicode)]
208
        public static extern bool DokanNotifyXAttrUpdate(DokanHandle dokanInstance, [MarshalAs(UnmanagedType.LPWStr)] string filePath);
209

210
        /// <summary>
211
        /// Notify Dokan that a file or directory has been renamed.
212
        /// </summary>
213
        /// <remarks>This method supports in-place rename for file/directory within the same parent.</remarks>
214
        /// <param name="dokanInstance">The dokan mount context created by <see cref="DokanCreateFileSystem"/></param>
215
        /// <param name="OldPath">Old path to the file or directory, including mount point.</param>
216
        /// <param name="newPath">New path to the file or directory, including mount point.</param>
217
        /// <param name="isDirectory">Indicates if the path is a directory.</param>
218
        /// <param name="isInSameDirectory">Indicates if OldPath and NewPath have the same parent.</param>
219
        /// <returns>true if notification succeeded.</returns>
220
        [DllImport(DOKAN_DLL, CharSet = CharSet.Unicode)]
221
        public static extern bool DokanNotifyRename(DokanHandle dokanInstance, [MarshalAs(UnmanagedType.LPWStr)] string OldPath,
222
                                                    [MarshalAs(UnmanagedType.LPWStr)] string newPath,
223
                                                    [MarshalAs(UnmanagedType.Bool)] bool isDirectory,
224
                                                    [MarshalAs(UnmanagedType.Bool)] bool isInSameDirectory);
225

226
    }
227
}

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.