diff --git a/samples/winapi_drivelist/main.c b/samples/winapi_drivelist/main.c index 0c422fb8b..390ccdfff 100644 --- a/samples/winapi_drivelist/main.c +++ b/samples/winapi_drivelist/main.c @@ -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; } @@ -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; } @@ -56,10 +67,32 @@ 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 10 seconds", errorCount); + Sleep(10000); + return 1; } return 0; diff --git a/samples/winapi_filefind/main.c b/samples/winapi_filefind/main.c index 46d1c8ff7..fdf3c654f 100644 --- a/samples/winapi_filefind/main.c +++ b/samples/winapi_filefind/main.c @@ -10,10 +10,18 @@ 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 C: BOOL ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\"); if (!ret) { - debugPrint("Failed to mount C: drive!\n"); + errorCount++; + // We can get more information about an error from WinAPI code using GetLastError() + error = GetLastError(); + debugPrint("Failed to mount C: drive! Reason: %s\n", error); + // Give the user some time to read the error Sleep(5000); return 1; } @@ -27,7 +35,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"); + errorCount++; + error = GetLastError(); + debugPrint("FindFirstHandle() failed! Reason: %s\n", error); Sleep(5000); return 1; } @@ -44,17 +54,30 @@ 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) { + errorCount++; + error = GetLastError(); + debugPrint("Couldn't unmount C: drive Reason: %s\n. Exiting in 5 seconds...", error); + Sleep (5000); + } + + if (errorCount > 0) { + debugPrint("Total number of errors: %d. Exiting in 10 seconds...\n", errorCount); + Sleep(10000); + return 1; } return 0;