-
Notifications
You must be signed in to change notification settings - Fork 70
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
samples: Make winapi samples unmount drives when exiting #382
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably be done after handling the error, to not disturb the readability of the important bits. Also if this is later turned into a |
||
// Additional error info can be retrieved with GetLastError() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment doesn't add anything |
||
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..."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where does this "re-running" happen? |
||
|
||
// Give some time to the user to read the drives | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The user would be reading the output, but not 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; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This wait doesn't make any sense to me. |
||
|
||
ret = nxUnmountDrive('C'); | ||
// If there was an error while unmounting (nxUnmountDrive() returns false) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This claims that it will ~"return false". This isn't actually happening anywhere. |
||
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; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: This is a poor variable name in my opinion.