From 348de3fdbfa69907950bbcf067e0d31c54cc69dc Mon Sep 17 00:00:00 2001 From: Kosmas Raptis Date: Wed, 15 Jul 2020 18:45:35 +0300 Subject: [PATCH] samples: Make winapi samples unmount drives when exiting --- samples/winapi_drivelist/main.c | 54 +++++++++++++++++++++++---------- samples/winapi_filefind/main.c | 39 ++++++++++++++++++------ 2 files changed, 67 insertions(+), 26 deletions(-) diff --git a/samples/winapi_drivelist/main.c b/samples/winapi_drivelist/main.c index 0c422fb8b..46bde0b31 100644 --- a/samples/winapi_drivelist/main.c +++ b/samples/winapi_drivelist/main.c @@ -7,27 +7,30 @@ int main(void) { XVideoSetMode(640, 480, 32, REFRESH_DEFAULT); + // Create a variable to check if there was any error in the program + BOOL errored = false; // Mount some drives for demonstration purposes BOOL ret; ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\"); if (!ret) { - debugPrint("Failed to mount C: drive!\n"); - Sleep(5000); - return 1; + errored = true; + // Additional error info can be retrieved with GetLastError() + DWORD error = GetLastError(); + debugPrint("Failed to mount C: drive! Reason: %x\n", error); } ret = nxMountDrive('E', "\\Device\\Harddisk0\\Partition1\\"); if (!ret) { - debugPrint("Failed to mount E: drive!\n"); - Sleep(5000); - return 1; + errored = true; + DWORD error = GetLastError(); + debugPrint("Failed to mount E: drive! Reason: %x\n", error); } // Retrieve drive bitmaks. Every bit represents one drive letter DWORD driveBits = GetLogicalDrives(); if (driveBits == 0 && GetLastError() != ERROR_SUCCESS) { - debugPrint("Failed to retrieve drive bitmask!\n"); - Sleep(5000); - return 1; + errored = true; + DWORD error = GetLastError(); + debugPrint("Failed to retrieve drive bitmask! Reason: %x\n", error); } debugPrint("Drive bitmask: 0x%x\n\n", driveBits); @@ -37,10 +40,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"); - Sleep(5000); - return 1; + errored = true; + DWORD error = GetLastError(); + debugPrint("Failed to retrieve drive strings! Reason: %x\n", error); } if (charsWritten > sizeof(buffer) - 1) { @@ -56,10 +58,30 @@ int main(void) debugPrint("%s\n", drive); while(*drive++); } - debugPrint("\ndone"); + debugPrint("\nDone! Re-running in 10 seconds..."); + + // Give some time to the user to read the drives + Sleep(10000); + + ret = nxUnmountDrive('C'); + // If there was an error while unmounting (nxUnmountDrive() returns false) + if (!ret) { + errored = true; + DWORD error = GetLastError(); + debugPrint("Couldn't unmount C: drive! Reason: %x\n. Trying to unmount E: drive...\n", error); + } + + ret = nxUnmountDrive('E'); + if (!ret) { + errored = true; + DWORD error = GetLastError(); + debugPrint("Couldn't unmount E: drive! Reason: %s\n", error); + } - while(1) { - Sleep(2000); + if (errored) { + // Give some time to the user to read any errors + Sleep(5000); + return 1; } return 0; diff --git a/samples/winapi_filefind/main.c b/samples/winapi_filefind/main.c index 46d1c8ff7..3e938899f 100644 --- a/samples/winapi_filefind/main.c +++ b/samples/winapi_filefind/main.c @@ -10,12 +10,17 @@ int main(void) { XVideoSetMode(640, 480, 32, REFRESH_DEFAULT); + // Create a variable to check if there was an error in the program + BOOL errored = false; + // Create a variable for WinAPI error checking + DWORD error = GetLastError(); // Mount C: BOOL ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\"); if (!ret) { - debugPrint("Failed to mount C: drive!\n"); - Sleep(5000); - return 1; + errored = true; + // We can get more information about an error from WinAPI code using GetLastError() + error = GetLastError(); + debugPrint("Failed to mount C: drive! Reason: %x\n", error); } debugPrint("Content of C:\\\n"); @@ -27,9 +32,9 @@ int main(void) // no matter whether they contain a dot or not hFind = FindFirstFile("C:\\*.*", &findFileData); if (hFind == INVALID_HANDLE_VALUE) { - debugPrint("FindFirstHandle() failed!\n"); - Sleep(5000); - return 1; + errored = true; + error = GetLastError(); + debugPrint("FindFirstHandle() failed! Reason: %x\n", error); } do { @@ -44,17 +49,31 @@ int main(void) debugPrint("\n"); - DWORD error = GetLastError(); + error = GetLastError(); if (error == ERROR_NO_MORE_FILES) { - debugPrint("Done!\n"); + debugPrint("Done! Re-running in 10 seconds...\n"); } else { + errored = true; debugPrint("error: %x\n", error); } FindClose(hFind); - while (1) { - Sleep(2000); + // Give some time to the user to read the files and directories + Sleep(10000); + + ret = nxUnmountDrive('C'); + // If there was an error while unmounting (nxUnmountDrive() returns false) + if (!ret) { + errored = true; + error = GetLastError(); + debugPrint("Couldn't unmount C: drive! Reason: %x", error); + } + + if (errored) { + // Give some time to the user to read any errors + Sleep(5000); + return 1; } return 0;