diff --git a/samples/winapi_drivelist/main.c b/samples/winapi_drivelist/main.c index 0c422fb8b..4f6bd0c10 100644 --- a/samples/winapi_drivelist/main.c +++ b/samples/winapi_drivelist/main.c @@ -9,45 +9,54 @@ int main(void) // Mount some drives for demonstration purposes BOOL ret; + DWORD error; ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\"); if (!ret) { - debugPrint("Failed to mount C: drive!\n"); + // Additional error info can be retrieved with GetLastError() + error = GetLastError(); + debugPrint("Failed to mount C: drive! Error code: %x\n", error); Sleep(5000); return 1; } ret = nxMountDrive('E', "\\Device\\Harddisk0\\Partition1\\"); if (!ret) { - debugPrint("Failed to mount E: drive!\n"); + error = GetLastError(); + debugPrint("Failed to mount E: drive! Error code: %x\n", error); + goto unmount_c; Sleep(5000); return 1; } - // Retrieve drive bitmaks. Every bit represents one drive letter + // Retrieve drive bitmasks. Every bit represents one drive letter DWORD driveBits = GetLogicalDrives(); if (driveBits == 0 && GetLastError() != ERROR_SUCCESS) { - debugPrint("Failed to retrieve drive bitmask!\n"); - Sleep(5000); + error = GetLastError(); + debugPrint("Failed to retrieve drive bitmask! Error code: %x\n", error); + goto unmount_c; + goto unmount_e; return 1; } debugPrint("Drive bitmask: 0x%x\n\n", driveBits); - // Reserve buffer long enough for all possible drive strings plus null-terminator char buffer[26 * 4 + 1]; // 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); + error = GetLastError(); + debugPrint("Failed to retrieve drive strings! Error code: %x\n", error); + goto unmount_c; + goto unmount_e; return 1; } if (charsWritten > sizeof(buffer) - 1) { // Can't happen here as our buffer is large enough to cover all possibilities debugPrint("Buffer for GetLogicalDriveStringsA too small!\n"); - Sleep(5000); + goto unmount_c; + goto unmount_e; return 1; + Sleep(5000); } debugPrint("Drives found:\n"); @@ -56,11 +65,30 @@ int main(void) debugPrint("%s\n", drive); while(*drive++); } - debugPrint("\ndone"); - while(1) { + goto unmount_c; + goto unmount_e; + + debugPrint("\nDone!"); + + while (1) { Sleep(2000); } return 0; -} + + unmount_c: + ret = nxUnmountDrive('C'); + // If there was an error while unmounting + if (!ret) { + error = GetLastError(); + debugPrint("\nCouldn't unmount C: drive! Error code: %x\n. Trying to unmount E: drive...\n", error); + } + + unmount_e: + ret = nxUnmountDrive('E'); + if (!ret) { + error = GetLastError(); + debugPrint("\nCouldn't unmount E: drive! Error code: %x\n", error); + } +} \ No newline at end of file diff --git a/samples/winapi_filefind/main.c b/samples/winapi_filefind/main.c index 46d1c8ff7..bedafe2f5 100644 --- a/samples/winapi_filefind/main.c +++ b/samples/winapi_filefind/main.c @@ -10,10 +10,15 @@ 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) { - debugPrint("Failed to mount C: drive!\n"); + // There was an error. 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); Sleep(5000); return 1; } @@ -27,8 +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); + error = GetLastError(); + debugPrint("FindFirstHandle() failed! Reason: %x\n", error); + goto unmount_c; return 1; } @@ -38,13 +44,12 @@ int main(void) } else { debugPrint("File : "); } - debugPrint("%s\n", findFileData.cFileName); } while (FindNextFile(hFind, &findFileData) != 0); debugPrint("\n"); - DWORD error = GetLastError(); + error = GetLastError(); if (error == ERROR_NO_MORE_FILES) { debugPrint("Done!\n"); } else { @@ -53,9 +58,19 @@ int main(void) FindClose(hFind); + goto unmount_c; + while (1) { Sleep(2000); } return 0; + + unmount_c: + ret = nxUnmountDrive('C'); + // If there was an error while unmounting + if (!ret) { + error = GetLastError(); + debugPrint("Couldn't unmount C: drive! Reason: %x", error); + } }