Repository navigation
Expand file tree
/
Copy pathdevices.dart
More file actions
93 lines (78 loc) · 2.64 KB
/
Copy pathdevices.dart
File metadata and controls
93 lines (78 loc) · 2.64 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
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Finds physical volumes on the system
import 'dart:ffi';
import 'package:ffi/ffi.dart';
import 'package:win32/win32.dart';
final volumeHandles = <int, String>{};
void displayVolumePaths(String volumeName) {
var error = 0;
// Could be arbitrarily long, but 4*MAX_PATH is a reasonable default.
// More sophisticated solutions can be found online
final pathNamePtr = wsalloc(MAX_PATH * 4);
final charCount = calloc<DWORD>();
final volumeNamePtr = volumeName.toNativeUtf16();
try {
charCount.value = MAX_PATH;
error = GetVolumePathNamesForVolumeName(
volumeNamePtr, pathNamePtr, charCount.value, charCount);
if (error != 0) {
if (charCount.value > 1) {
for (final path in pathNamePtr.unpackStringArray(charCount.value)) {
print(path);
}
} else {
print('[none]');
}
} else {
error = GetLastError();
print('GetVolumePathNamesForVolumeName failed with error code $error');
}
} finally {
free(volumeNamePtr);
free(pathNamePtr);
free(charCount);
}
}
void main() {
var error = 0;
final volumeNamePtr = wsalloc(MAX_PATH);
final hFindVolume = FindFirstVolume(volumeNamePtr, MAX_PATH);
if (hFindVolume == INVALID_HANDLE_VALUE) {
error = GetLastError();
print('FindFirstVolume failed with error code $error');
return;
}
while (true) {
final volumeName = volumeNamePtr.toDartString();
// Skip the \\?\ prefix and remove the trailing backslash.
final shortVolumeName = volumeName.substring(4, volumeName.length - 1);
final shortVolumeNamePtr = TEXT(shortVolumeName);
final deviceName = wsalloc(MAX_PATH);
final charCount = QueryDosDevice(shortVolumeNamePtr, deviceName, MAX_PATH);
if (charCount == 0) {
error = GetLastError();
print('QueryDosDevice failed with error code $error');
break;
}
print('\nFound a device:\n${deviceName.toDartString()}');
print('Volume name: $volumeName');
print('Paths:');
displayVolumePaths(volumeName);
final success = FindNextVolume(hFindVolume, volumeNamePtr, MAX_PATH);
if (success == 0) {
error = GetLastError();
if (error != ERROR_NO_MORE_FILES && error != ERROR_SUCCESS) {
print('FindNextVolume failed with error code $error');
break;
} else {
error = ERROR_SUCCESS;
break;
}
}
free(shortVolumeNamePtr);
}
free(volumeNamePtr);
FindVolumeClose(hFindVolume);
}