Repository navigation
Expand file tree
/
Copy pathdokan.c
More file actions
750 lines (626 loc) · 18.6 KB
/
Copy pathdokan.c
File metadata and controls
750 lines (626 loc) · 18.6 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
8000
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
/*
Dokan : user-mode file system library for Windows
Copyright (C) 2008 Hiroki Asakawa info@dokan-dev.net
http://dokan-dev.net/en
This program is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 3 of the License, or (at your option) any
later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define _UNICODE
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
#include <tchar.h>
#include <process.h>
#include <locale.h>
#include "fileinfo.h"
#include "dokani.h"
#include "list.h"
// DokanOptions->DebugMode is ON?
BOOL g_DebugMode = TRUE;
// DokanOptions->UseStdErr is ON?
BOOL g_UseStdErr = FALSE;
CRITICAL_SECTION g_InstanceCriticalSection;
LIST_ENTRY g_InstanceList;
PDOKAN_INSTANCE
NewDokanInstance()
{
PDOKAN_INSTANCE instance = (PDOKAN_INSTANCE)malloc(sizeof(DOKAN_INSTANCE));
ZeroMemory(instance, sizeof(DOKAN_INSTANCE));
#if _MSC_VER < 1300
InitializeCriticalSection(&instance->CriticalSection);
#else
InitializeCriticalSectionAndSpinCount(
&instance->CriticalSection, 0x80000400);
#endif
InitializeListHead(&instance->ListEntry);
EnterCriticalSection(&g_InstanceCriticalSection);
InsertTailList(&g_InstanceList, &instance->ListEntry);
LeaveCriticalSection(&g_InstanceCriticalSection);
return instance;
}
VOID
DeleteDokanInstance(PDOKAN_INSTANCE Instance)
{
DeleteCriticalSection(&Instance->CriticalSection);
EnterCriticalSection(&g_InstanceCriticalSection);
RemoveEntryList(&Instance->ListEntry);
LeaveCriticalSection(&g_InstanceCriticalSection);
free(Instance);
}
BOOL
IsValidDriveLetter(WCHAR DriveLetter)
{
return (L'd' <= DriveLetter && DriveLetter <= L'z') ||
(L'D' <= DriveLetter && DriveLetter <= L'Z');
}
int
CheckMountPoint(LPCWSTR MountPoint)
{
ULONG length = wcslen(MountPoint);
if ((length == 1) ||
(length == 2 && MountPoint[1] == L':') ||
(length == 3 && MountPoint[1] == L':' && MountPoint[2] == L'\\')) {
WCHAR driveLetter = MountPoint[0];
if (IsValidDriveLetter(driveLetter)) {
return DOKAN_SUCCESS;
} else {
DokanDbgPrintW(L"Dokan Error: bad drive letter %s\n", MountPoint);
return DOKAN_DRIVE_LETTER_ERROR;
}
} else if (length > 3) {
HANDLE handle = CreateFile(
MountPoint, GENERIC_WRITE, 0, NULL, OPEN_EXISTING,
FILE_FLAG_OPEN_REPARSE_POINT | FILE_FLAG_BACKUP_SEMANTICS, NULL);
if (handle == INVALID_HANDLE_VALUE) {
DokanDbgPrintW(L"Dokan Error: bad mount point %s\n", MountPoint);
return DOKAN_MOUNT_POINT_ERROR;
}
CloseHandle(handle);
return DOKAN_SUCCESS;
}
return DOKAN_MOUNT_POINT_ERROR;
}
int DOKANAPI
DokanMain(PDOKAN_OPTIONS DokanOptions, PDOKAN_OPERATIONS DokanOperations)
{
ULONG threadNum = 0;
ULONG i;
BOOL status;
int error;
HANDLE device;
HANDLE threadIds[DOKAN_MAX_THREAD];
ULONG returnedLength;
char buffer[1024];
BOOL useMountPoint = FALSE;
PDOKAN_INSTANCE instance;
g_DebugMode = DokanOptions->Options & DOKAN_OPTION_DEBUG;
g_UseStdErr = DokanOptions->Options & DOKAN_OPTION_STDERR;
if (g_DebugMode) {
DbgPrintW(L"Dokan: debug mode on\n");
}
if (g_UseStdErr) {
DbgPrintW(L"Dokan: use stderr\n");
g_DebugMode = TRUE;
}
if (DokanOptions->ThreadCount == 0) {
DokanOptions->ThreadCount = 5;
} else if (DOKAN_MAX_THREAD-1 < DokanOptions->ThreadCount) {
// DOKAN_MAX_THREAD includes DokanKeepAlive thread, so
// available thread is DOKAN_MAX_THREAD -1
DokanDbgPrintW(L"Dokan Error: too many thread count %d\n",
DokanOptions->ThreadCount);
DokanOptions->ThreadCount = DOKAN_MAX_THREAD-1;
}
if (DOKAN_MOUNT_POINT_SUPPORTED_VERSION <= DokanOptions->Version &&
DokanOptions->MountPoint) {
error = CheckMountPoint(DokanOptions->MountPoint);
if (error != DOKAN_SUCCESS) {
return error;
}
useMountPoint = TRUE;
} else if (!IsValidDriveLetter((WCHAR)DokanOptions->Version)) {
// Older versions use the first 2 bytes of DokanOptions struct as DriveLetter.
DokanDbgPrintW(L"Dokan Error: bad drive letter %wc\n", (WCHAR)DokanOptions->Version);
return DOKAN_DRIVE_LETTER_ERROR;
}
device = CreateFile(
DOKAN_GLOBAL_DEVICE_NAME, // lpFileName
GENERIC_READ|GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ|FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
if (device == INVALID_HANDLE_VALUE){
DokanDbgPrintW(L"Dokan Error: CreatFile Failed %s: %d\n",
DOKAN_GLOBAL_DEVICE_NAME, GetLastError());
return DOKAN_DRIVER_INSTALL_ERROR;
}
DbgPrint("device opened\n");
instance = NewDokanInstance();
instance->DokanOptions = DokanOptions;
instance->DokanOperations = DokanOperations;
if (useMountPoint) {
wcscpy_s(instance->MountPoint, sizeof(instance->MountPoint) / sizeof(WCHAR),
DokanOptions->MountPoint);
} else {
// Older versions use the first 2 bytes of DokanOptions struct as DriveLetter.
instance->MountPoint[0] = (WCHAR)DokanOptions->Version;
instance->MountPoint[1] = L':';
instance->MountPoint[2] = L'\\';
}
if (!DokanStart(instance)) {
return DOKAN_START_ERROR;
}
if (DokanMount(instance->MountPoint, instance->DeviceName,DokanOptions->Options)) {
if(DOKAN_MOUNT_CALLBACK_SUPPORTED_VERSION <= DokanOptions->Version)
DokanOperations->Mount(NULL);
}else{
SendReleaseIRP(instance->DeviceName);
DokanDbgPrint("Dokan Error: DefineDosDevice Failed\n");
return DOKAN_MOUNT_ERROR;
}
DbgPrintW(L"mounted: %s -> %s\n", instance->MountPoint, instance->DeviceName);
if (DokanOptions->Options & DOKAN_OPTION_KEEP_ALIVE) {
threadIds[threadNum++] = (HANDLE)_beginthreadex(
NULL, // Security Atributes
0, //stack size
DokanKeepAlive,
instance, // param
0, // create flag
NULL);
}
for (i = 0; i < DokanOptions->ThreadCount; ++i) {
threadIds[threadNum++] = (HANDLE)_beginthreadex(
NULL, // Security Atributes
0, //stack size
DokanLoop,
(PVOID)instance, // param
0, // create flag
NULL);
}
// wait for thread terminations
WaitForMultipleObjects(threadNum, threadIds, TRUE, INFINITE);
for (i = 0; i < threadNum; ++i) {
CloseHandle(threadIds[i]);
}
CloseHandle(device);
Sleep(1000);
DbgPrint("\nunload\n");
DeleteDokanInstance(instance);
return DOKAN_SUCCESS;
}
LPCWSTR
GetRawDeviceName(LPCWSTR DeviceName)
{
static WCHAR rawDeviceName[MAX_PATH];
wcscpy_s(rawDeviceName, MAX_PATH, L"\\\\.");
wcscat_s(rawDeviceName, MAX_PATH, DeviceName);
return rawDeviceName;
}
DWORD WINAPI
DokanLoop(
PDOKAN_INSTANCE DokanInstance
)
{
HANDLE device;
char buffer[EVENT_CONTEXT_MAX_SIZE];
ULONG count = 0;
BOOL status;
ULONG returnedLength;
DWORD result = 0;
RtlZeroMemory(buffer, sizeof(buffer));
device = CreateFile(
GetRawDeviceName(DokanInstance->DeviceName), // lpFileName
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
if (device == INVALID_HANDLE_VALUE) {
DbgPrint("Dokan Error: CreateFile failed %ws: %d\n",
GetRawDeviceName(DokanInstance->DeviceName), GetLastError());
result = -1;
_endthreadex(result);
return result;
}
while(1) {
status = DeviceIoControl(
device, // Handle to device
IOCTL_EVENT_WAIT, // IO Control code
NULL, // Input Buffer to driver.
0, // Length of input buffer in bytes.
buffer, // Output Buffer from driver.
sizeof(buffer), // Length of output buffer in bytes.
&returnedLength, // Bytes placed in buffer.
NULL // synchronous call
);
if (!status) {
DbgPrint("Ioctl failed with code %d\n", GetLastError());
result = -1;
break;
}
//printf("#%d got notification %d\n", (ULONG)Param, count++);
if(returnedLength > 0) {
PEVENT_CONTEXT context = (PEVENT_CONTEXT)buffer;
if (context->MountId != DokanInstance->MountId) {
DbgPrint("Dokan Error: Invalid MountId (expected:%d, acctual:%d)\n",
DokanInstance->MountId, context->MountId);
continue;
}
switch (context->MajorFunction) {
case IRP_MJ_CREATE:
DispatchCreate(device, context, DokanInstance);
break;
case IRP_MJ_CLEANUP:
DispatchCleanup(device, context, DokanInstance);
break;
case IRP_MJ_CLOSE:
DispatchClose(device, context, DokanInstance);
break;
case IRP_MJ_DIRECTORY_CONTROL:
DispatchDirectoryInformation(device, context, DokanInstance);
break;
case IRP_MJ_READ:
DispatchRead(device, context, DokanInstance);
break;
case IRP_MJ_WRITE:
DispatchWrite(device, context, DokanInstance);
break;
case IRP_MJ_QUERY_INFORMATION:
DispatchQueryInformation(device, context, DokanInstance);
break;
case IRP_MJ_QUERY_VOLUME_INFORMATION:
DispatchQueryVolumeInformation(device ,context, DokanInstance);
break;
case IRP_MJ_LOCK_CONTROL:
DispatchLock(device, context, DokanInstance);
break;
case IRP_MJ_SET_INFORMATION:
DispatchSetInformation(device, context, DokanInstance);
break;
case IRP_MJ_FLUSH_BUFFERS:
DispatchFlush(device, context, DokanInstance);
break;
case IRP_MJ_QUERY_SECURITY:
DispatchQuerySecurity(device, context, DokanInstance);
break;
case IRP_MJ_SET_SECURITY:
DispatchSetSecurity(device, context, DokanInstance);
break;
case IRP_MJ_SHUTDOWN:
// this cass is used before unmount not shutdown
DispatchUnmount(device, context, DokanInstance);
break;
default:
break;
}
} else {
DbgPrint("ReturnedLength %d\n", returnedLength);
}
}
CloseHandle(device);
_endthreadex(result);
return result;
}
VOID
SendEventInformation(
HANDLE Handle,
PEVENT_INFORMATION EventInfo,
ULONG EventLength,
PDOKAN_INSTANCE DokanInstance)
{
BOOL status;
ULONG returnedLength;
//DbgPrint("###EventInfo->Context %X\n", EventInfo->Context);
if (DokanInstance != NULL) {
ReleaseDokanOpenInfo(EventInfo, DokanInstance);
}
// send event info to driver
status = DeviceIoControl(
Handle, // Handle to device
IOCTL_EVENT_INFO, // IO Control code
EventInfo, // Input Buffer to driver.
EventLength, // Length of input buffer in bytes.
NULL, // Output Buffer from driver.
0, // Length of output buffer in bytes.
&returnedLength, // Bytes placed in buffer.
NULL // synchronous call
);
if (!status) {
DWORD errorCode = GetLastError();
DbgPrint("Dokan Error: Ioctl failed with code %d\n", errorCode );
}
}
VOID
CheckFileName(
LPWSTR FileName)
{
// if the begining of file name is "\\",
// replace it with "\"
if (FileName[0] == L'\\' && FileName[1] == L'\\') {
int i;
for (i = 0; FileName[i+1] != L'\0'; ++i) {
FileName[i] = FileName[i+1];
}
FileName[i] = L'\0';
}
}
PEVENT_INFORMATION
DispatchCommon(
PEVENT_CONTEXT EventContext,
ULONG SizeOfEventInfo,
PDOKAN_INSTANCE DokanInstance,
PDOKAN_FILE_INFO DokanFileInfo,
PDOKAN_OPEN_INFO* DokanOpenInfo)
{
PEVENT_INFORMATION eventInfo = (PEVENT_INFORMATION)malloc(SizeOfEventInfo);
RtlZeroMemory(eventInfo, SizeOfEventInfo);
RtlZeroMemory(DokanFileInfo, sizeof(DOKAN_FILE_INFO));
eventInfo->BufferLength = 0;
eventInfo->SerialNumber = EventContext->SerialNumber;
DokanFileInfo->ProcessId = EventContext->ProcessId;
DokanFileInfo->DokanOptions = DokanInstance->DokanOptions;
if (EventContext->FileFlags & DOKAN_DELETE_ON_CLOSE) {
DokanFileInfo->DeleteOnClose = 1;
}
if (EventContext->FileFlags & DOKAN_PAGING_IO) {
DokanFileInfo->PagingIo = 1;
}
if (EventContext->FileFlags & DOKAN_WRITE_TO_END_OF_FILE) {
DokanFileInfo->WriteToEndOfFile = 1;
}
if (EventContext->FileFlags & DOKAN_SYNCHRONOUS_IO) {
DokanFileInfo->SynchronousIo = 1;
}
if (EventContext->FileFlags & DOKAN_NOCACHE) {
DokanFileInfo->Nocache = 1;
}
*DokanOpenInfo = GetDokanOpenInfo(EventContext, DokanInstance);
if (*DokanOpenInfo == NULL) {
DbgPrint("error openInfo is NULL\n");
return eventInfo;
}
DokanFileInfo->Context = (ULONG64)(*DokanOpenInfo)->UserContext;
DokanFileInfo->IsDirectory = (UCHAR)(*DokanOpenInfo)->IsDirectory;
DokanFileInfo->DokanContext = (ULONG64)(*DokanOpenInfo);
eventInfo->Context = (ULONG64)(*DokanOpenInfo);
return eventInfo;
}
PDOKAN_OPEN_INFO
GetDokanOpenInfo(
PEVENT_CONTEXT EventContext,
PDOKAN_INSTANCE DokanInstance)
{
PDOKAN_OPEN_INFO openInfo;
EnterCriticalSection(&DokanInstance->CriticalSection);
openInfo = (PDOKAN_OPEN_INFO)EventContext->Context;
if (openInfo != NULL) {
openInfo->OpenCount++;
openInfo->EventContext = EventContext;
openInfo->DokanInstance = DokanInstance;
}
LeaveCriticalSection(&DokanInstance->CriticalSection);
return openInfo;
}
VOID
ReleaseDokanOpenInfo(
PEVENT_INFORMATION EventInformation,
PDOKAN_INSTANCE DokanInstance)
{
PDOKAN_OPEN_INFO openInfo;
EnterCriticalSection(&DokanInstance->CriticalSection);
openInfo = (PDOKAN_OPEN_INFO)EventInformation->Context;
if (openInfo != NULL) {
openInfo->OpenCount--;
if (openInfo->OpenCount < 1) {
if (openInfo->DirListHead != NULL) {
ClearFindData(openInfo->DirListHead);
free(openInfo->DirListHead);
openInfo->DirListHead = NULL;
}
free(openInfo);
EventInformation->Context = 0;
}
}
LeaveCriticalSection(&DokanInstance->CriticalSection);
}
VOID
DispatchUnmount(
HANDLE Handle,
PEVENT_CONTEXT EventContext,
PDOKAN_INSTANCE DokanInstance)
{
DOKAN_FILE_INFO fileInfo;
static int count = 0;
// Unmount is called only once
EnterCriticalSection(&DokanInstance->CriticalSection);
if (count > 0) {
LeaveCriticalSection(&DokanInstance->CriticalSection);
return;
}
count++;
RtlZeroMemory(&fileInfo, sizeof(DOKAN_FILE_INFO));
fileInfo.ProcessId = EventContext->ProcessId;
if (DokanInstance->DokanOperations->Unmount) {
// ignore return value
DokanInstance->DokanOperations->Unmount(&fileInfo);
}
LeaveCriticalSection(&DokanInstance->CriticalSection);
// do not notice enything to the driver
return;
}
// ask driver to release all pending IRP to prepare for Unmount.
BOOL
SendReleaseIRP(
LPCWSTR DeviceName)
{
ULONG returnedLength;
DbgPrint("send release\n");
if (!SendToDevice(
GetRawDeviceName(DeviceName),
IOCTL_EVENT_RELEASE,
NULL,
0,
NULL,
0,
&returnedLength) ) {
DbgPrint("Failed to unmount device:%ws\n", DeviceName);
return FALSE;
}
return TRUE;
}
BOOL
DokanStart(PDOKAN_INSTANCE Instance)
{
EVENT_START eventStart;
EVENT_DRIVER_INFO driverInfo;
ULONG returnedLength = 0;
ZeroMemory(&eventStart, sizeof(EVENT_START));
ZeroMemory(&driverInfo, sizeof(EVENT_DRIVER_INFO));
eventStart.UserVersion = DOKAN_DRIVER_VERSION;
if (Instance->DokanOptions->Options & DOKAN_OPTION_ALT_STREAM) {
eventStart.Flags |= DOKAN_EVENT_ALTERNATIVE_STREAM_ON;
}
if (Instance->DokanOptions->Options & DOKAN_OPTION_KEEP_ALIVE) {
eventStart.Flags |= DOKAN_EVENT_KEEP_ALIVE_ON;
}
if (Instance->DokanOptions->Options & DOKAN_OPTION_NETWORK) {
eventStart.DeviceType = DOKAN_NETWORK_FILE_SYSTEM;
}
if (Instance->DokanOptions->Options & DOKAN_OPTION_REMOVABLE) {
eventStart.Flags |= DOKAN_EVENT_REMOVABLE;
}
SendToDevice(
DOKAN_GLOBAL_DEVICE_NAME,
IOCTL_EVENT_START,
&eventStart,
sizeof(EVENT_START),
&driverInfo,
sizeof(EVENT_DRIVER_INFO),
&returnedLength);
if (driverInfo.Status == DOKAN_START_FAILED) {
if (driverInfo.DriverVersion != eventStart.UserVersion) {
DokanDbgPrint(
"Dokan Error: driver version mismatch, driver %X, dll %X\n",
driverInfo.DriverVersion, eventStart.UserVersion);
} else {
DokanDbgPrint("Dokan Error: driver start error\n");
}
return FALSE;
} else if (driverInfo.Status == DOKAN_MOUNTED) {
Instance->MountId = driverInfo.MountId;
Instance->DeviceNumber = driverInfo.DeviceNumber;
wcscpy_s(Instance->DeviceName,
sizeof(Instance->DeviceName) / sizeof(WCHAR),
driverInfo.DeviceName);
return TRUE;
}
return FALSE;
}
BOOL
DokanSetDebugMode(
ULONG Mode)
{
ULONG returnedLength;
return SendToDevice(
DOKAN_GLOBAL_DEVICE_NAME,
IOCTL_SET_DEBUG_MODE,
&Mode,
sizeof(ULONG),
NULL,
0,
&returnedLength);
}
BOOL
SendToDevice(
LPCWSTR DeviceName,
DWORD IoControlCode,
PVOID InputBuffer,
ULONG InputLength,
PVOID OutputBuffer,
ULONG OutputLength,
PULONG ReturnedLength)
{
HANDLE device;
BOOL status;
ULONG returnedLength;
device = CreateFile(
DeviceName, // lpFileName
GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
NULL, // lpSecurityAttributes
OPEN_EXISTING, // dwCreationDistribution<
7273
/div>
0, // dwFlagsAndAttributes
NULL // hTemplateFile
);
if (device == INVALID_HANDLE_VALUE) {
DWORD dwErrorCode = GetLastError();
DbgPrint("Dokan Error: Failed to open %ws with code %d\n",
DeviceName, dwErrorCode);
return FALSE;
}
status = DeviceIoControl(
device, // Handle to device
IoControlCode, // IO Control code
InputBuffer, // Input Buffer to driver.
InputLength, // Length of input buffer in bytes.
OutputBuffer, // Output Buffer from driver.
OutputLength, // Length of output buffer in bytes.
ReturnedLength, // Bytes placed in buffer.
NULL // synchronous call
);
CloseHandle(device);
if (!status) {
DbgPrint("DokanError: Ioctl failed with code %d\n", GetLastError());
return FALSE;
}
return TRUE;
}
BOOL WINAPI DllMain(
HINSTANCE Instance,
DWORD Reason,
LPVOID Reserved)
{
switch(Reason) {
case DLL_PROCESS_ATTACH:
{
#if _MSC_VER < 1300
InitializeCriticalSection(&g_InstanceCriticalSection);
#else
InitializeCriticalSectionAndSpinCount(
&g_InstanceCriticalSection, 0x80000400);
#endif
InitializeListHead(&g_InstanceList);
}
break;
case DLL_PROCESS_DETACH:
{
EnterCriticalSection(&g_InstanceCriticalSection);
while(!IsListEmpty(&g_InstanceList)) {
PLIST_ENTRY entry = RemoveHeadList(&g_InstanceList);
PDOKAN_INSTANCE instance =
CONTAINING_RECORD(entry, DOKAN_INSTANCE, ListEntry);
DokanRemoveMountPoint(instance->MountPoint);
free(instance);
}
LeaveCriticalSection(&g_InstanceCriticalSection);
DeleteCriticalSection(&g_InstanceCriticalSection);
}
break;
}
return TRUE;
}
You can’t perform that action at this time.