Skip to content

Commit

Permalink
samples: Make winapi samples unmount drives when exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
kosmas12 committed Jul 16, 2020
1 parent f73fe04 commit b66d362
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
46 changes: 39 additions & 7 deletions samples/winapi_drivelist/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,27 @@ int main(void)
{
XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);

// Create a variable for WinAPI error checking
DWORD error;
// Make a variable to keep track of how many errors we have
int errorCount = 0;
// Mount some drives for demonstration purposes
BOOL ret;
ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\");
if (!ret) {
debugPrint("Failed to mount C: drive!\n");
errorCount++;
// Additional error info can be retrieved with GetLastError()
error = GetLastError();
debugPrint("Failed to mount C: drive! Reason: %s\n", error);
// Give the user some time to read the message
Sleep(5000);
return 1;
}
ret = nxMountDrive('E', "\\Device\\Harddisk0\\Partition1\\");
if (!ret) {
debugPrint("Failed to mount E: drive!\n");
errorCount++;
error = GetLastError();
debugPrint("Failed to mount E: drive! Reason: %s\n", error);
Sleep(5000);
return 1;
}
Expand All @@ -37,8 +47,9 @@ int main(void)
// IMPORTANT: The size passed to GetLogicalDriveStringsA is WITHOUT the null-terminator, even though it gets written
DWORD charsWritten = GetLogicalDriveStringsA(sizeof(buffer)-1, buffer);
if (charsWritten == 0) {
// Additional error info can be retrieved with GetLastError()
debugPrint("Failed to retrieve drive strings!\n");
errorCount++;
error = GetLastError();
debugPrint("Failed to retrieve drive strings! Reason: %s\n", error);
Sleep(5000);
return 1;
}
Expand All @@ -56,10 +67,31 @@ int main(void)
debugPrint("%s\n", drive);
while(*drive++);
}
debugPrint("\ndone");
debugPrint("\nDone! Exiting in 10 seconds...");

Sleep(10000);

ret = nxUnmountDrive('C');
// If there was an error while unmounting (nxUnmountDrive() returns false)
if (!ret) {
error = GetLastError();
errorCount++;
debugPrint("Couldn't unmount C: drive Reason: %s\n. Trying to unmount E: drive...\n", error);
// Leave some time for the user to read the error
Sleep(5000);
}

ret = nxUnmountDrive('E');
if (!ret) {
error = GetLastError();
errorCount++;
debugPrint("Couldn't unmount E: drive. Reason: %s\n", error);
Sleep (5000);
}

while(1) {
Sleep(2000);
if (errorCount > 0) {
debugPrint("Total number of errors: %d. Exiting in 5 seconds", errorCount);
return 1;
}

return 0;
Expand Down
18 changes: 14 additions & 4 deletions samples/winapi_filefind/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ int main(void)
{
XVideoSetMode(640, 480, 32, REFRESH_DEFAULT);

// Create a variable for WinAPI error checking
DWORD error;
// Mount C:
BOOL ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\");
if (!ret) {
Expand Down Expand Up @@ -44,17 +46,25 @@ int main(void)

debugPrint("\n");

DWORD error = GetLastError();
error = GetLastError();
if (error == ERROR_NO_MORE_FILES) {
debugPrint("Done!\n");
debugPrint("Done! Exiting in 10 seconds...\n");
} else {
debugPrint("error: %x\n", error);
}

FindClose(hFind);

while (1) {
Sleep(2000);
Sleep(10000);

ret = nxUnmountDrive('C');
// If there was an error while unmounting (nxUnmountDrive() returns false)
if (!ret) {
error = GetLastError();
debugPrint("Couldn't unmount C: drive Reason: %s\n. Exiting in 5 seconds...", error);
// Leave some time for the user to read the error
Sleep (5000);
return 1;
}

return 0;
Expand Down

0 comments on commit b66d362

Please sign in to comment.