Skip to content
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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 38 additions & 16 deletions samples/winapi_drivelist/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Member

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.

// 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;
Copy link
Member

Choose a reason for hiding this comment

The 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 mark_error(); (for whatever reason) then that function might actually change the GetLastError() value.

// Additional error info can be retrieved with GetLastError()
Copy link
Member

Choose a reason for hiding this comment

The 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);

Expand All @@ -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) {
Expand All @@ -56,10 +58,30 @@ int main(void)
debugPrint("%s\n", drive);
while(*drive++);
}
debugPrint("\ndone");
debugPrint("\nDone! Re-running in 10 seconds...");
Copy link
Member

Choose a reason for hiding this comment

The 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
Copy link
Member

Choose a reason for hiding this comment

The 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;
Expand Down
39 changes: 29 additions & 10 deletions samples/winapi_filefind/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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 {
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The 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)
Copy link
Member

Choose a reason for hiding this comment

The 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;
Expand Down