Skip to content

Commit

Permalink
samples: Make winapi samples unmount drives when exiting
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryzee119 authored and kosmas12 committed Feb 2, 2023
1 parent fdb6e38 commit 1ccc6cb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 33 deletions.
10 changes: 9 additions & 1 deletion lib/usb/libusbohci_xbox/xid_driver.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,16 @@ static xid_dev_t *alloc_xid_device(void) {
}

static void free_xid_device(xid_dev_t *xid_dev) {
//Find the device head in the linked list
xid_dev_t *head = pxid_list;

//If the xid is at the head of the list, remove now.
if (xid_dev == head)
{
pxid_list = head->next;
head = NULL;
}

//Find the device head in the linked list
while (head != NULL && head->next != xid_dev)
{
head = head->next;
Expand Down
59 changes: 38 additions & 21 deletions samples/winapi_drivelist/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,45 +9,46 @@ 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");
Sleep(5000);
return 1;
// Additional error info can be retrieved with GetLastError()
error = GetLastError();
debugPrint("Failed to mount C: drive! Error code: %x\n", error);
goto sleepForever;
}

ret = nxMountDrive('E', "\\Device\\Harddisk0\\Partition1\\");
if (!ret) {
debugPrint("Failed to mount E: drive!\n");
Sleep(5000);
return 1;
error = GetLastError();
debugPrint("Failed to mount E: drive! Error code: %x\n", error);
goto unmount_c;
}

// 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);
return 1;
error = GetLastError();
if (driveBits == 0 && error != ERROR_SUCCESS) {
debugPrint("Failed to retrieve drive bitmask! Error code: %x\n", error);
goto cleanup;
}
debugPrint("Drive bitmask: 0x%lx\n\n", driveBits);

debugPrint("Drive bitmask: 0xl%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);
return 1;
error = GetLastError();
debugPrint("Failed to retrieve drive strings! Error code: %x\n", error);
goto cleanup;
}

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);
return 1;
goto cleanup;
}

debugPrint("Drives found:\n");
Expand All @@ -56,11 +57,27 @@ int main(void)
debugPrint("%s\n", drive);
while(*drive++);
}
debugPrint("\ndone");

while(1) {
debugPrint("\nDone!");

cleanup:
ret = nxUnmountDrive('E');
if (!ret) {
error = GetLastError();
debugPrint("\nFailed to unmount E: drive! Error code: %x\n", error);
}

unmount_c:
ret = nxUnmountDrive('C');
if (!ret) {
error = GetLastError();
debugPrint("\nFailed to unmount C: drive! Error code: %x\n", error);
}

sleepForever:
while (1) {
Sleep(2000);
}

return 0;
}
}
31 changes: 20 additions & 11 deletions samples/winapi_filefind/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ int main(void)
// Mount C:
BOOL ret = nxMountDrive('C', "\\Device\\Harddisk0\\Partition2\\");
if (!ret) {
debugPrint("Failed to mount C: drive!\n");
Sleep(5000);
return 1;
// There was an error. We can get more information about an error from WinAPI code using GetLastError()
DWORD mountError = GetLastError();
debugPrint("Failed to mount C: drive! Error code: %x\n", mountError);
goto sleepForever;
}

debugPrint("Content of C:\\\n");
Expand All @@ -26,10 +27,11 @@ int main(void)
// Like on Windows, "*.*" and "*" will both list all files,
// no matter whether they contain a dot or not
hFind = FindFirstFile("C:\\*.*", &findFileData);
DWORD findFileError;
if (hFind == INVALID_HANDLE_VALUE) {
debugPrint("FindFirstHandle() failed!\n");
Sleep(5000);
return 1;
findFileError = GetLastError();
debugPrint("FindFirstHandle() failed! Error code: %x\n", findFileError);
goto cleanup;
}

do {
Expand All @@ -38,24 +40,31 @@ int main(void)
} else {
debugPrint("File : ");
}

debugPrint("%s\n", findFileData.cFileName);
} while (FindNextFile(hFind, &findFileData) != 0);

debugPrint("\n");

DWORD error = GetLastError();
if (error == ERROR_NO_MORE_FILES) {
findFileError = GetLastError();
if (findFileError == ERROR_NO_MORE_FILES) {
debugPrint("Done!\n");
} else {
debugPrint("error: %lx\n", error);
debugPrint("error: %lx\n", findFileError);
}

FindClose(hFind);

cleanup:
ret = nxUnmountDrive('C');
// If there was an error while unmounting
if (!ret) {
DWORD unmountError = GetLastError();
debugPrint("Failed to unmount C: drive! Error code: %x", unmountError);
}
sleepForever:
while (1) {
Sleep(2000);
}

return 0;
}
}

0 comments on commit 1ccc6cb

Please sign in to comment.