Skip to content

Commit ca09cdd

Browse files
committed
samples: Make winapi samples unmount drives when exiting
1 parent 9ca54b5 commit ca09cdd

2 files changed

Lines changed: 53 additions & 27 deletions

File tree

samples/winapi_drivelist/main.c

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,45 +9,47 @@ int main(void)
99

1010
// Mount some drives for demonstration purposes
1111
BOOL ret;
12+
DWORD error;
1213
ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\");
1314
if (!ret) {
14-
debugPrint("Failed to mount C: drive!\n");
15+
// Additional error info can be retrieved with GetLastError()
16+
error = GetLastError();
17+
debugPrint("Failed to mount C: drive! Error code: %x\n", error);
1518
Sleep(5000);
1619
return 1;
1720
}
21+
1822
ret = nxMountDrive('E', "\\Device\\Harddisk0\\Partition1\\");
1923
if (!ret) {
20-
debugPrint("Failed to mount E: drive!\n");
21-
Sleep(5000);
22-
return 1;
24+
error = GetLastError();
25+
debugPrint("Failed to mount E: drive! Error code: %x\n", error);
26+
goto unmount_c;
2327
}
2428

25-
// Retrieve drive bitmaks. Every bit represents one drive letter
29+
// Retrieve drive bitmasks. Every bit represents one drive letter
2630
DWORD driveBits = GetLogicalDrives();
2731
if (driveBits == 0 && GetLastError() != ERROR_SUCCESS) {
28-
debugPrint("Failed to retrieve drive bitmask!\n");
29-
Sleep(5000);
30-
return 1;
32+
error = GetLastError();
33+
debugPrint("Failed to retrieve drive bitmask! Error code: %x\n", error);
34+
goto unmount_e;
3135
}
32-
debugPrint("Drive bitmask: 0x%x\n\n", driveBits);
3336

37+
debugPrint("Drive bitmask: 0x%x\n\n", driveBits);
3438

3539
// Reserve buffer long enough for all possible drive strings plus null-terminator
3640
char buffer[26 * 4 + 1];
3741
// IMPORTANT: The size passed to GetLogicalDriveStringsA is WITHOUT the null-terminator, even though it gets written
3842
DWORD charsWritten = GetLogicalDriveStringsA(sizeof(buffer)-1, buffer);
3943
if (charsWritten == 0) {
40-
// Additional error info can be retrieved with GetLastError()
41-
debugPrint("Failed to retrieve drive strings!\n");
42-
Sleep(5000);
43-
return 1;
44+
error = GetLastError();
45+
debugPrint("Failed to retrieve drive strings! Error code: %x\n", error);
46+
goto unmount_e;
4447
}
4548

4649
if (charsWritten > sizeof(buffer) - 1) {
4750
// Can't happen here as our buffer is large enough to cover all possibilities
4851
debugPrint("Buffer for GetLogicalDriveStringsA too small!\n");
49-
Sleep(5000);
50-
return 1;
52+
goto unmount_e;
5153
}
5254

5355
debugPrint("Drives found:\n");
@@ -56,11 +58,25 @@ int main(void)
5658
debugPrint("%s\n", drive);
5759
while(*drive++);
5860
}
59-
debugPrint("\ndone");
6061

61-
while(1) {
62-
Sleep(2000);
62+
debugPrint("\nDone!");
63+
64+
goto unmount_e;
65+
66+
unmount_e:
67+
ret = nxUnmountDrive('E');
68+
// If there was an error while unmounting
69+
if (!ret) {
70+
error = GetLastError();
71+
debugPrint("\nCouldn't unmount E: drive! Error code: %x\n", error);
6372
}
6473

74+
unmount_c:
75+
ret = nxUnmountDrive('C');
76+
if (!ret) {
77+
error = GetLastError();
78+
debugPrint("\nCouldn't unmount C: drive! Error code: %x\n. Trying to unmount E: drive...\n", error);
79+
}
80+
Sleep(5000);
6581
return 0;
6682
}

samples/winapi_filefind/main.c

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,15 @@ int main(void)
1010
{
1111
XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);
1212

13+
// Create a variable for WinAPI error checking
14+
DWORD error;
15+
1316
// Mount C:
1417
BOOL ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\");
1518
if (!ret) {
16-
debugPrint("Failed to mount C: drive!\n");
19+
// There was an error. We can get more information about an error from WinAPI code using GetLastError()
20+
error = GetLastError();
21+
debugPrint("Failed to mount C: drive! Reason: %x\n", error);
1722
Sleep(5000);
1823
return 1;
1924
}
@@ -27,9 +32,9 @@ int main(void)
2732
// no matter whether they contain a dot or not
2833
hFind = FindFirstFile("C:\\*.*", &findFileData);
2934
if (hFind == INVALID_HANDLE_VALUE) {
30-
debugPrint("FindFirstHandle() failed!\n");
31-
Sleep(5000);
32-
return 1;
35+
error = GetLastError();
36+
debugPrint("FindFirstHandle() failed! Reason: %x\n", error);
37+
goto unmount_c;
3338
}
3439

3540
do {
@@ -38,13 +43,12 @@ int main(void)
3843
} else {
3944
debugPrint("File : ");
4045
}
41-
4246
debugPrint("%s\n", findFileData.cFileName);
4347
} while (FindNextFile(hFind, &findFileData) != 0);
4448

4549
debugPrint("\n");
4650

47-
DWORD error = GetLastError();
51+
error = GetLastError();
4852
if (error == ERROR_NO_MORE_FILES) {
4953
debugPrint("Done!\n");
5054
} else {
@@ -53,9 +57,15 @@ int main(void)
5357

5458
FindClose(hFind);
5559

56-
while (1) {
57-
Sleep(2000);
58-
}
60+
goto unmount_c;
5961

62+
unmount_c:
63+
ret = nxUnmountDrive('C');
64+
// If there was an error while unmounting
65+
if (!ret) {
66+
error = GetLastError();
67+
debugPrint("Couldn't unmount C: drive! Reason: %x", error);
68+
}
69+
Sleep(5000);
6070
return 0;
6171
}

0 commit comments

Comments
 (0)