@@ -9,45 +9,47 @@ int main(void)
99
1010 // Mount some drives for demonstration purposes
1111 BOOL ret ;
12+ DWORD error ;
1213 ret = nxMountDrive ('C' , "\\Device\\Harddisk0\\Partition2\\" );
1314 if (!ret ) {
14- debugPrint ("Failed to mount C: drive!\n" );
15+ // Additional error info can be retrieved with GetLastError()
16+ error = GetLastError ();
17+ debugPrint ("Failed to mount C: drive! Error code: %x\n" , error );
1518 Sleep (5000 );
1619 return 1 ;
1720 }
21+
1822 ret = nxMountDrive ('E' , "\\Device\\Harddisk0\\Partition1\\" );
1923 if (!ret ) {
20- debugPrint ( "Failed to mount E: drive!\n" );
21- Sleep ( 5000 );
22- return 1 ;
24+ error = GetLastError ( );
25+ debugPrint ( "Failed to mount E: drive! Error code: %x\n" , error );
26+ goto unmount_c ;
2327 }
2428
25- // Retrieve drive bitmaks . Every bit represents one drive letter
29+ // Retrieve drive bitmasks . Every bit represents one drive letter
2630 DWORD driveBits = GetLogicalDrives ();
2731 if (driveBits == 0 && GetLastError () != ERROR_SUCCESS ) {
28- debugPrint ( "Failed to retrieve drive bitmask!\n" );
29- Sleep ( 5000 );
30- return 1 ;
32+ error = GetLastError ( );
33+ debugPrint ( "Failed to retrieve drive bitmask! Error code: %x\n" , error );
34+ goto unmount_e ;
3135 }
32- debugPrint ("Drive bitmask: 0x%x\n\n" , driveBits );
3336
37+ debugPrint ("Drive bitmask: 0x%x\n\n" , driveBits );
3438
3539 // Reserve buffer long enough for all possible drive strings plus null-terminator
3640 char buffer [26 * 4 + 1 ];
3741 // IMPORTANT: The size passed to GetLogicalDriveStringsA is WITHOUT the null-terminator, even though it gets written
3842 DWORD charsWritten = GetLogicalDriveStringsA (sizeof (buffer )- 1 , buffer );
3943 if (charsWritten == 0 ) {
40- // Additional error info can be retrieved with GetLastError()
41- debugPrint ("Failed to retrieve drive strings!\n" );
42- Sleep (5000 );
43- return 1 ;
44+ error = GetLastError ();
45+ debugPrint ("Failed to retrieve drive strings! Error code: %x\n" , error );
46+ goto unmount_e ;
4447 }
4548
4649 if (charsWritten > sizeof (buffer ) - 1 ) {
4750 // Can't happen here as our buffer is large enough to cover all possibilities
4851 debugPrint ("Buffer for GetLogicalDriveStringsA too small!\n" );
49- Sleep (5000 );
50- return 1 ;
52+ goto unmount_e ;
5153 }
5254
5355 debugPrint ("Drives found:\n" );
@@ -56,11 +58,25 @@ int main(void)
5658 debugPrint ("%s\n" , drive );
5759 while (* drive ++ );
5860 }
59- debugPrint ("\ndone" );
6061
61- while (1 ) {
62- Sleep (2000 );
62+ debugPrint ("\nDone!" );
63+
64+ goto unmount_e ;
65+
66+ unmount_e :
67+ ret = nxUnmountDrive ('E' );
68+ // If there was an error while unmounting
69+ if (!ret ) {
70+ error = GetLastError ();
71+ debugPrint ("\nCouldn't unmount E: drive! Error code: %x\n" , error );
6372 }
6473
74+ unmount_c :
75+ ret = nxUnmountDrive ('C' );
76+ if (!ret ) {
77+ error = GetLastError ();
78+ debugPrint ("\nCouldn't unmount C: drive! Error code: %x\n. Trying to unmount E: drive...\n" , error );
79+ }
80+ Sleep (5000 );
6581 return 0 ;
6682}
0 commit comments