From: David F. on
Any solution to this problem?

Here's the sample output - shows what's wrong (when drive changed the
current directory set on a drive is reverted):

Set drive to C:
Change working directory of C: and D: to Test Folder
Current Directory of C: is:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
Current Directory of D: is:
D:\Test
Set drive to D:
Current Directory of C: is:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
Current Directory of D: is:
D:\Test
Set drive to C:
Current Directory of C: is:
C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE
Current Directory of D: is:
D:\


Here's the console program (project using multi-byte (not unicode) mode):

// chdrive.cpp - test current directory being changed by _chdrive
//

#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <direct.h>
#include <conio.h>

TCHAR WorkBuf[_MAX_PATH];

int _tmain(int argc, TCHAR *argv[])
{
// change to C: drive
_tprintf(_T("Set drive to C:\n"));
_chdrive(3);

// change directories
_tprintf(_T("Change working directory of C: and D: to Test Folder\n"));
SetCurrentDirectory(_T("C:\\Test"));
SetCurrentDirectory(_T("D:\\Test"));

if (_tgetdcwd(3, WorkBuf, sizeof(WorkBuf)/sizeof(WorkBuf[0]))!=NULL) {
_tprintf(_T("Current Directory of C: is:\n%s\n"), WorkBuf);
}
if (_tgetdcwd(4, WorkBuf, sizeof(WorkBuf)/sizeof(WorkBuf[0]))!=NULL) {
_tprintf(_T("Current Directory of D: is:\n%s\n"), WorkBuf);
}

// change to D: drive
_tprintf(_T("Set drive to D:\n"));
_chdrive(4);

if (_tgetdcwd(3, WorkBuf, sizeof(WorkBuf)/sizeof(WorkBuf[0]))!=NULL) {
_tprintf(_T("Current Directory of C: is:\n%s\n"), WorkBuf);
}
if (_tgetdcwd(4, WorkBuf, sizeof(WorkBuf)/sizeof(WorkBuf[0]))!=NULL) {
_tprintf(_T("Current Directory of D: is:\n%s\n"), WorkBuf);
}

// change to C: drive
_tprintf(_T("Set drive to C:\n"));
_chdrive(3);


if (_tgetdcwd(3, WorkBuf, sizeof(WorkBuf)/sizeof(WorkBuf[0]))!=NULL) {
_tprintf(_T("Current Directory of C: is:\n%s\n"), WorkBuf);
}
if (_tgetdcwd(4, WorkBuf, sizeof(WorkBuf)/sizeof(WorkBuf[0]))!=NULL) {
_tprintf(_T("Current Directory of D: is:\n%s\n"), WorkBuf);
}


while (_gettch()!=13);

return 0;
}

From: Leo Davidson on
On Apr 30, 7:02 am, "David F." <df2...(a)community.nospam> wrote:

>   _chdrive(3);
>   SetCurrentDirectory(_T("C:\\Test"));
>   SetCurrentDirectory(_T("D:\\Test"));

You're mixing two different APIs there.

Change SetCurrentDirectory to _chdir (or _tchdir in this case) and it
seems to work as you want.

I expect that the CRT functions (_chdrive and _chdir) are doing some
extra bookkeeping to remember the current dir for each drive, while
the Win32 SetCurrentDirectory does not do that (or does it in a
different way, but AFAIK remembering the current dir for different
drives is an MS-DOSism and not inherent to Win32).
From: David F. on


"Leo Davidson" <leonudeldavidson(a)googlemail.com> wrote in message
news:a29b7ba9-a379-4879-b32a-27941bd799d1(a)y36g2000yqm.googlegroups.com...
On Apr 30, 7:02 am, "David F." <df2...(a)community.nospam> wrote:

>> _chdrive(3);
>> SetCurrentDirectory(_T("C:\\Test"));
>> SetCurrentDirectory(_T("D:\\Test"));

>You're mixing two different APIs there.

That was just for testing, the reason being I don't want the drive to change
if I say to change the current directory of d: to d:\test. I want to stay
on c: but docs say using the _chdir also changes the drive if given
(_chdir("d:\test") and now the current drive is D: instead of c:).

From Docs: "If a new drive letter is specified in dirname, the default drive
letter is changed as well" ... I don't want it to do that.

?



From: Leo Davidson on
On Apr 30, 7:57 pm, "David F." <df2...(a)community.nospam> wrote:


> From Docs: "If a new drive letter is specified in dirname, the default drive
> letter is changed as well"  ... I don't want it to do that.

I think you'll have to _chdir to the new path, then _chdrive back to
the old drive/path if you want to do that.

I'm curious why you want to do an MS-DOS-style _chdir for a drive you
are not using, and without affecting the actual current dir?

Is this for your own code or is some other component forcing you to do
these contortions?
From: David F. on
For an environment where that makes since .. I didn't want to have to
getdrive / setdrive but I guess I'll have to do something like that.

"Leo Davidson" <leonudeldavidson(a)googlemail.com> wrote in message
news:610b6b83-125c-4698-b2f0-d34a9feab4d6(a)s29g2000yqd.googlegroups.com...
On Apr 30, 7:57 pm, "David F." <df2...(a)community.nospam> wrote:


> From Docs: "If a new drive letter is specified in dirname, the default
> drive
> letter is changed as well" ... I don't want it to do that.

I think you'll have to _chdir to the new path, then _chdrive back to
the old drive/path if you want to do that.

I'm curious why you want to do an MS-DOS-style _chdir for a drive you
are not using, and without affecting the actual current dir?

Is this for your own code or is some other component forcing you to do
these contortions?