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
kosmas12 committed Feb 7, 2021
1 parent 9ca54b5 commit 2337f43
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 32 deletions.
44 changes: 27 additions & 17 deletions samples/winapi_drivelist/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,24 @@ 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);
}
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);
}

// 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();
debugPrint("Failed to retrieve drive bitmask! Error code: %x\n", error);
}
debugPrint("Drive bitmask: 0x%x\n\n", driveBits);

Expand All @@ -37,10 +36,8 @@ 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;
error = GetLastError();
debugPrint("Failed to retrieve drive strings! Error code: %x\n", error);
}

if (charsWritten > sizeof(buffer) - 1) {
Expand All @@ -56,11 +53,24 @@ int main(void)
debugPrint("%s\n", drive);
while(*drive++);
}
debugPrint("\ndone");

while(1) {
Sleep(2000);
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);
}

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

debugPrint("\nDone!");

// Give some time to the user to read all the output
Sleep(10000);

return 0;
}
38 changes: 23 additions & 15 deletions samples/winapi_filefind/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +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");
Sleep(5000);
return 1;
// 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);
}

debugPrint("Content of C:\\\n");
Expand All @@ -27,24 +30,22 @@ 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;
error = GetLastError();
debugPrint("FindFirstHandle() failed! Reason: %x\n", error);
}

do {
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
debugPrint("Directory: ");
} else {
debugPrint("File : ");
}

debugPrint("%s\n", findFileData.cFileName);
if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
debugPrint("Directory: ");
} 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 {
Expand All @@ -53,9 +54,16 @@ int main(void)

FindClose(hFind);

ret = nxUnmountDrive('C');
// If there was an error while unmounting
if (!ret) {
error = GetLastError();
debugPrint("Couldn't unmount C: drive! Reason: %x", error);
}

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

return 0;
}

0 comments on commit 2337f43

Please sign in to comment.