@@ -20,6 +20,7 @@ with this program. If not, see <http://www.gnu.org/licenses/>.
20
20
#include " stdafx.h"
21
21
#include < windows.h>
22
22
#include < winioctl.h>
23
+ #include < string>
23
24
#include < stdio.h>
24
25
#include < string.h>
25
26
#include < stdlib.h>
@@ -245,144 +246,150 @@ DokanMain(PDOKAN_OPTIONS DokanOptions, PDOKAN_OPERATIONS DokanOperations)
245
246
246
247
Sleep (1000 );
247
248
248
- logw (L" \n unload " );
249
+ logw (L" unload " );
249
250
250
251
DeleteDokanInstance (instance);
251
252
252
253
return DOKAN_SUCCESS;
253
254
}
254
255
255
- LPCWSTR
256
- GetRawDeviceName (LPCWSTR DeviceName)
256
+ BOOL GetRawDeviceName (LPCWSTR pDeviceName, LPWSTR pRawDeviceNameBuffer, size_t cchBuffer)
257
257
{
258
- static WCHAR rawDeviceName[MAX_PATH];
259
- wcscpy_s (rawDeviceName, MAX_PATH, L" \\\\ ." );
260
- wcscat_s (rawDeviceName, MAX_PATH, DeviceName);
261
- return rawDeviceName;
258
+ std::wstring raw = std::wstring (L" \\\\ ." ) + std::wstring (pDeviceName);
259
+ HRESULT hr = StringCchCopyW (pRawDeviceNameBuffer, cchBuffer, raw.c_str ());
260
+ return SUCCEEDED (hr);
262
261
}
263
262
264
- UINT WINAPI
265
- DokanLoop (
266
- PVOID pDokanInstance
267
- )
263
+ UINT WINAPI DokanLoop (PVOID pInstance)
268
264
{
269
- PDOKAN_INSTANCE DokanInstance = (PDOKAN_INSTANCE)pDokanInstance;
270
- HANDLE device;
271
- char buffer[EVENT_CONTEXT_MAX_SIZE];
272
- // ULONG count = 0;
273
- BOOL status;
274
- ULONG returnedLength;
275
- DWORD result = 0 ;
265
+ logw (L" Start" );
266
+ PDOKAN_INSTANCE pDokanInstance = (PDOKAN_INSTANCE)pInstance;
267
+ CHAR buffer[EVENT_CONTEXT_MAX_SIZE] = { 0 };
268
+ ULONG returnedLength;
269
+ DWORD result = ERROR_SUCCESS;
270
+ WCHAR szRawDeviceName[MAX_PATH];
276
271
277
- RtlZeroMemory (buffer, sizeof (buffer));
272
+ if (!GetRawDeviceName (pDokanInstance->DeviceName , szRawDeviceName, _countof (szRawDeviceName)))
273
+ {
274
+ logw (L" Failed to get raw device name from <%s>" , pDokanInstance->DeviceName );
275
+ result = ERROR_INVALID_PARAMETER;
276
+ _endthreadex (result);
277
+ return result;
278
+ }
278
279
279
- device = CreateFile (
280
- GetRawDeviceName (DokanInstance->DeviceName ), // lpFileName
281
- GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
282
- FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
283
- NULL , // lpSecurityAttributes
284
- OPEN_EXISTING, // dwCreationDistribution
285
- 0 , // dwFlagsAndAttributes
286
- NULL // hTemplateFile
287
- );
280
+ logw (L" RawDeviceName: <%s>" , szRawDeviceName);
288
281
289
- if (device == INVALID_HANDLE_VALUE) {
290
- logw (L" Dokan Error: CreateFile failed %ws: %d\n " ,
291
- GetRawDeviceName (DokanInstance->DeviceName ), GetLastError ());
292
- result = (DWORD)-1 ;
282
+ HANDLE hDevice = CreateFile (
283
+ szRawDeviceName,
284
+ GENERIC_READ | GENERIC_WRITE, // dwDesiredAccess
285
+ FILE_SHARE_READ | FILE_SHARE_WRITE, // dwShareMode
286
+ NULL , // lpSecurityAttributes
287
+ OPEN_EXISTING, // dwCreationDistribution
288
+ 0 , // dwFlagsAndAttributes
289
+ NULL // hTemplateFile
290
+ );
291
+
292
+ if (hDevice == INVALID_HANDLE_VALUE)
293
+ {
294
+ DWORD dwLastError = GetLastError ();
295
+ logw (L" Dokan Error: CreateFile failed %s(%d)" , szRawDeviceName, dwLastError);
296
+ result = dwLastError;
293
297
_endthreadex (result);
294
298
return result;
295
299
}
296
300
297
- for ( ; ; ) {
298
-
299
- status = DeviceIoControl (
300
- device, // Handle to device
301
- IOCTL_EVENT_WAIT, // IO Control code
302
- NULL , // Input Buffer to driver.
303
- 0 , // Length of input buffer in bytes.
304
- buffer, // Output Buffer from driver.
305
- sizeof (buffer), // Length of output buffer in bytes.
306
- &returnedLength, // Bytes placed in buffer.
307
- NULL // synchronous call
308
- );
301
+ for ( ; ; )
302
+ {
303
+ BOOL fOk = DeviceIoControl (
304
+ hDevice, // Handle to device
305
+ IOCTL_EVENT_WAIT, // IO Control code
306
+ NULL , // Input Buffer to driver.
307
+ 0 , // Length of input buffer in bytes.
308
+ buffer, // Output Buffer from driver.
309
+ sizeof (buffer), // Length of output buffer in bytes.
310
+ &returnedLength, // Bytes placed in buffer.
311
+ NULL // synchronous call
312
+ );
309
313
310
- if (!status) {
311
- logw (L" Ioctl failed with code %d\n " , GetLastError ());
312
- result = (DWORD)-1 ;
314
+ if (!fOk )
315
+ {
316
+ DWORD dwLastError = GetLastError ();
317
+ logw (L" Ioctl failed with code (%d). rawDeviceName is <%s>" , dwLastError, szRawDeviceName);
318
+ result = dwLastError;
313
319
break ;
314
320
}
315
321
316
322
// printf("#%d got notification %d\n", (ULONG)Param, count++);
317
323
318
- if (returnedLength > 0 ) {
324
+ if (returnedLength > 0 )
325
+ {
319
326
PEVENT_CONTEXT context = (PEVENT_CONTEXT)buffer;
320
- if (context->MountId != DokanInstance ->MountId ) {
321
- logw ( L" Dokan Error: Invalid MountId (expected:%d, acctual:%d) \n " ,
322
- DokanInstance ->MountId , context->MountId );
327
+ if (context->MountId != pDokanInstance ->MountId )
328
+ {
329
+ logw ( L" Dokan Error: Invalid MountId (expected:%d, acctual:%d) " , pDokanInstance ->MountId , context->MountId );
323
330
continue ;
324
331
}
325
332
326
- switch (context->MajorFunction ) {
333
+ switch (context->MajorFunction )
334
+ {
327
335
case IRP_MJ_CREATE:
328
- DispatchCreate (device , context, DokanInstance );
336
+ DispatchCreate (hDevice , context, pDokanInstance );
329
337
break ;
330
338
case IRP_MJ_CLEANUP:
331
- DispatchCleanup (device , context, DokanInstance );
339
+ DispatchCleanup (hDevice , context, pDokanInstance );
332
340
break ;
333
341
case IRP_MJ_CLOSE:
334
- DispatchClose (device , context, DokanInstance );
342
+ DispatchClose (hDevice , context, pDokanInstance );
335
343
break ;
336
344
case IRP_MJ_DIRECTORY_CONTROL:
337
- DispatchDirectoryInformation (device , context, DokanInstance );
345
+ DispatchDirectoryInformation (hDevice , context, pDokanInstance );
338
346
break ;
339
347
case IRP_MJ_READ:
340
- DispatchRead (device , context, DokanInstance );
348
+ DispatchRead (hDevice , context, pDokanInstance );
341
349
break ;
342
350
case IRP_MJ_WRITE:
343
- DispatchWrite (device , context, DokanInstance );
351
+ DispatchWrite (hDevice , context, pDokanInstance );
344
352
break ;
345
353
case IRP_MJ_QUERY_INFORMATION:
346
- DispatchQueryInformation (device , context, DokanInstance );
354
+ DispatchQueryInformation (hDevice , context, pDokanInstance );
347
355
break ;
348
356
case IRP_MJ_QUERY_VOLUME_INFORMATION:
349
- DispatchQueryVolumeInformation (device ,context, DokanInstance );
357
+ DispatchQueryVolumeInformation (hDevice ,context, pDokanInstance );
350
358
break ;
351
359
case IRP_MJ_LOCK_CONTROL:
352
- DispatchLock (device , context, DokanInstance );
360
+ DispatchLock (hDevice , context, pDokanInstance );
353
361
break ;
354
362
case IRP_MJ_SET_INFORMATION:
355
- DispatchSetInformation (device , context, DokanInstance );
363
+ DispatchSetInformation (hDevice , context, pDokanInstance );
356
364
break ;
357
365
case IRP_MJ_FLUSH_BUFFERS:
358
- DispatchFlush (device , context, DokanInstance );
366
+ DispatchFlush (hDevice , context, pDokanInstance );
359
367
break ;
360
368
case IRP_MJ_QUERY_SECURITY:
361
- DispatchQuerySecurity (device , context, DokanInstance );
369
+ DispatchQuerySecurity (hDevice , context, pDokanInstance );
362
370
break ;
363
371
case IRP_MJ_SET_SECURITY:
364
- DispatchSetSecurity (device , context, DokanInstance );
372
+ DispatchSetSecurity (hDevice , context, pDokanInstance );
365
373
break ;
366
374
case IRP_MJ_SHUTDOWN:
367
375
// this cass is used before unmount not shutdown
368
- DispatchUnmount (device , context, DokanInstance );
376
+ DispatchUnmount (hDevice , context, pDokanInstance );
369
377
break ;
370
378
default :
371
379
break ;
372
380
}
373
-
374
- } else {
375
- logw (L" ReturnedLength %d\n " , returnedLength);
381
+ }
382
+ else
383
+ {
384
+ logw (L" ReturnedLength (%d)" , returnedLength);
376
385
}
377
386
}
378
387
379
- CloseHandle (device );
388
+ CloseHandle (hDevice );
380
389
_endthreadex (result);
381
390
return result;
382
391
}
383
392
384
-
385
-
386
393
VOID
387
394
SendEventInformation (
388
395
HANDLE Handle,
@@ -566,22 +573,27 @@ DispatchUnmount(
566
573
567
574
568
575
// ask driver to release all pending IRP to prepare for Unmount.
569
- BOOL
570
- SendReleaseIRP (
571
- LPCWSTR DeviceName)
576
+ BOOL SendReleaseIRP (LPCWSTR DeviceName)
572
577
{
573
- ULONG returnedLength;
574
-
578
+ ULONG returnedLength;
575
579
logw (L" send release" );
576
580
581
+ WCHAR rawDeviceName[MAX_PATH];
582
+ if (GetRawDeviceName (DeviceName, rawDeviceName, _countof (rawDeviceName)))
583
+ {
584
+ return FALSE ;
585
+ }
586
+
577
587
if (!SendToDevice (
578
- GetRawDeviceName (DeviceName),
579
- IOCTL_EVENT_RELEASE,
580
- NULL ,
581
- 0 ,
582
- NULL ,
583
- 0 ,
584
- &returnedLength) ) {
588
+ rawDeviceName,
589
+ IOCTL_EVENT_RELEASE,
590
+ NULL ,
591
+ 0 ,
592
+ NULL ,
593
+ 0 ,
594
+ &returnedLength)
595
+ )
596
+ {
585
597
586
598
logw (L" Failed to unmount device:%ws\n " , DeviceName);
587
599
return FALSE ;
0 commit comments