Solved

Changing system drive

Posted on 1997-12-15
14
245 Views
Last Modified: 2012-08-13

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
Comment
Question by:rian
  • 7
  • 4
  • 3
14 Comments
 
LVL 3

Accepted Solution

by:
os012897 earned 50 total points
ID: 1176291
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
 
LVL 22

Expert Comment

by:nietod
ID: 1176292
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
 
LVL 3

Expert Comment

by:os012897
ID: 1176293
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
 

Author Comment

by:rian
ID: 1176294
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
 
LVL 3

Expert Comment

by:os012897
ID: 1176295
The program is a console program and sorry, i am using the _chdrive API. Surprising the Microsoft provided solution does not work.
0
 

Author Comment

by:rian
ID: 1176296
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
 
LVL 22

Expert Comment

by:nietod
ID: 1176297
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:rian
ID: 1176298
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
 
LVL 3

Expert Comment

by:os012897
ID: 1176299
Argh ;) ....

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

os

0
 
LVL 3

Expert Comment

by:os012897
ID: 1176300
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
 

Author Comment

by:rian
ID: 1176301
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
 
LVL 3

Expert Comment

by:os012897
ID: 1176302
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
 
LVL 22

Expert Comment

by:nietod
ID: 1176303
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
 
LVL 3

Expert Comment

by:os012897
ID: 1176304
I appreciate all the help.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now