Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Changing system drive


Dear Experts,

Sorry for bothering all of u. But I am a humble novice to C++ and WIN32
API. I set out to write a simple console program which would find out all the drive letters used and then change the drive letter to one of the valid one in a DOS Program and I have not been
able to do that. I have included all the code to see if one of the experts can give me a solution.

I appreciate any help.

#include <iostream.h>
#include <stdio.h>
#include <io.h>
#include <windows.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
#include <direct.h>

void GetDrive()
{
 char path[MAX_PATH];
 int ch, drive, curdrive;
  /* Save current drive. */
 curdrive = _getdrive();
 printf( "Current Drive is  printf("\n");
 printf( "Available drives are: \n" );
 /* If we can switch to the drive, it exists. */
 for( drive = 1; drive <= 26; drive++ )
  if( !_chdrive( drive ) )
   printf( " printf("\n");
 /* Restore original drive.*/
 _chdrive( curdrive );

 bool driveok = true;
 drive = curdrive; // Set the current drive check
 while (driveok)
 {
  drive++;
  if (drive > 26)
   driveok = false;
  if (!_chdrive(drive))
  {
   if ( ( FileExist("\\SDD.DAT")) )
   {
    ch = drive;
    if( ch == 27 )
     break;
    if( isalpha( ch ) )
    _putch( ch );
    if( _getdcwd( toupper(ch) - 'A' + 1, path, _MAX_PATH ) != NULL )
      printf( "\nCurrent directory on that drive is (null)\n", path );
    path[0] = drive + 'A' - 1;
    path[1] = '\0';
    strcat(path, ":");

    SetCurrentDirectory((LPCTSTR)path);
    \\ _chdrive(path);
    printf( "Drive has been changed to     driveok = false;
   }
 
  }
 
 }

}


void main()
 {
 GetDrive();
 }

If u run this program from the DOS session, the correct messages are displayed but the drive letter is still the original.

Is there an API which will change the current drive and not the working drive. I have no idea how a working drive differs from a current drive
0
rian
Asked:
rian
  • 7
  • 4
  • 3
1 Solution
 
os012897Commented:
Hi,

I think you are mixing up "current drive" and "working directory". The "current drive" is the drive you are currently refering to, while the "working directory" is, the directory that is currently set to be default.

I am including a short program that can be found in the MS-VC++ 4.2 help: It checks for available drives and the current working directories on those drives!


#include <stdio.h>
#include <conio.h>
#include <direct.h>
#include <stdlib.h>
#include <ctype.h>

void main( void )
{
   int ch, drive, curdrive;
   static char path[_MAX_PATH];

   /* Save current drive. */
   curdrive = _getdrive();

   printf( "Available drives are: \n" );

   /* If we can switch to the drive, it exists. */
   for( drive = 1; drive <= 26; drive++ )
      if( !_chdrive( drive ) )
         printf( "%c: ", drive + 'A' - 1 );

   while( 1 )
   {
      printf( "\nType drive letter to check or ESC to quit: " );
      ch = _getch();
      if( ch == 27 )
         break;
      if( isalpha( ch ) )
         _putch( ch );
      if( _getdcwd( toupper( ch ) - 'A' + 1, path, _MAX_PATH ) != NULL )
         printf( "\nCurrent directory on that drive is %s\n", path );
   }

   /* Restore original drive.*/
   _chdrive( curdrive );
   printf( "\n" );
}

Hope that helps,

         os

0
 
nietodCommented:
The thing is ....

You CAN write console programs with WIN32.
You CANNOT write 16bit progams anymore.

So no DOS pograms under WIN32, but console programs are still possible.
0
 
os012897Commented:
I have tried the above code with _chdir and it does not change the drive to the new drive. Hence I tried, the GetCurrentDirectory.

When I run my or your program in a DOS window, the drive letter is not changed.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rianAuthor Commented:
Hmmm ... cannot believe that the Microsoft-supplied example does not work!

If you want the drive you set as your current drive to stay to be the current drive even after your program ended, you have to delete the line at the end of the sample where it says:

/* Restore original drive.*/
_chdrive( curdrive );  // DELETE THIS LINE

BTW, use "_chdrive" to change the drive, not "_chdir".

When u run the program and change the drive, does it show the current directory of the drive u changed to?

Greetings,

                   os

0
 
os012897Commented:
The program is a console program and sorry, i am using the _chdrive API. Surprising the Microsoft provided solution does not work.
0
 
rianAuthor Commented:
It looks to me like rian is trying to change the current directory of a DOS process by using a console application that is run from the DOS process.   If I'm wrong, ignore this.

Windows maintains a current directory for every process, including console apps.  If you change the directory in the console application it does not have any effect on te DOS process that called it.  If you want to change the directory in the DOS process, you have to use a DOS app.
0
 
nietodCommented:
Is'nt a console application a DOS program. I guess not. I do have a DOS program which changes the drive letters. Unfortunately, it used DOS registry information to determine what drives are present and which one is a logical drive. WHen I run this program under 95 or NT, it fails to recognize the substitute drives. Can I find out which memory location under NT is this drive information stored.
0
 
rianAuthor Commented:
Hmmm .... ok, lets see

1) So you do want to change the drive of a "DOS-box" from inside your program?!

2) Does the drive-change work for the application while it is running (does it show the current dirs of the drives you changed to?)

A console program under win32 looks like a dos-program  but it is not the same. dos-program are 16-bit, win32 console programs are 32 bit. They use different libraries and thus can behave differently.

This is for rian:
I am not sure whether you are right with your assumtion, that you cannot change the current drive of the dos-process from within a win32 consol application (though I am also not 100% sure that you are NOT right). The sample program which was written for VC++ 4.2 (so for Win32) resets the current drive at the end, which would imply, that the "DOS"-process' current drive would indeed be changed by the program if you do not do so.


Greetings,

               os

PS: Trying to directly access memory that resides not withing your application-process mem, is a bad idea under WinNT! NT has security mechanisms that fight any effort to do so.
0
 
os012897Commented:
Argh ;) ....

The paragraph "for rian" should of course read  "for nietod", sorry

os

0
 
os012897Commented:
yes to the first question. The sole purpose of the console program is to determine all the drives, check which drive has the information needed and change the drive letter. Once the drive letter is changed to the correct logical or physical drive, other programs running in batch can run properly since they are all designed to run from a default drive which has the information.
0
 
rianAuthor Commented:
Hmmm .....

Well if the proposed way does not work all I could do is suggest a kind of workaround .....

Write your program to return an error level that is specific for the drive you want to change to. Use your program in a batchfile which changes to a certain drive depending on the returned error-level of your program.

By the way, you mentioned that you have a DOS-program that DOES succeed in changing the drive of the current dos-session. Do you have the code for it? Which function does it use to do that?

Well, I am afraid that is all I can supply as info to your problem, reject my answer if you cannot make it work ....

Greetings,

          os

0
 
os012897Commented:
I am 99% sure that changing the current path/drive of a windows app, including a console applications, does not affect any DOS process's current path/drive.  Windows maintains a current path for every process.  A DOS process maintains is own current path for command.com and each process running in the DOS process.  It would be a bad idea for a console app running under a DOS process to change the current path of command.com also running under the same process.  Don't you agree?

At least it would be a bad idea to do it though the commands that change the current process's (console app's) path.  There might be a seperate mechanism for explicitly changing command.com's path.
0
 
nietodCommented:
Hmmm ..... I do not really see a problem in changing the current drive. Especially as rian mentiones that his 16bit DOS-program seems to work under 95/NT too (with the exception of the problem with logical drives).

_chdrive worked according to the needed task under DOS and I do not see why they should have changed that behaviour now in 95/NT.

Just my late night thoughts on that ......

Greetings,

                os
0
 
os012897Commented:
I appreciate all the help.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 7
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now