PRB: _getdcwd() Returns the Root Directory Under Win32s

Last reviewed: December 17, 1996
Article ID: Q98286
The information in this article applies to:
  • Microsoft Win32s versions 1.1, 1.2, 1.30, 1.30a, and 1.30c

SYMPTOMS

In the following code segment, _getdcwd() always returns the root:

   _getdcwd( 3, cBuf, MAX_PATH );
   MessageBox( hWnd, cBuf, "Drive 3 <C drive>", MB_OK );

Also, in the following code segment, _chdrive() and _getcwd() always return the root:

   _chdrive( 3 );
   _getcwd( cBuf, MAX_PATH );
   MessageBox( hWnd, cbuf, "Drive 3 <C drive>", MB_OK );

CAUSE

When a Win32-based application starts on Win32s, the root is set as the current directory for any drive except the default drive.

RESOLUTION

The following code fragments work as expected under Win32s:

   _getdcwd( 0, cBuf, MAX_PATH );
   MessageBox( hWnd, cBuf, "Drive 0 <default drive>", MB_OK );

   -or-

   GetCurrentDirectory( sizeof (cBuf), cBuf );
   MessageBox( hWnd, cBuf, "SCD", MB_OK );

MORE INFORMATION

Windows NT uses the current directory of a process as the initial current directory for the current drive of a child process. So for example, if the current directory in the command prompt (CMD.EXE) is C:\WINNT then the current directory of the child process will be C:\WINNT.

However, on Win32s, the current directory for any drive except the default drive is set to the root and not the current directory of the parent process. A Win32-based application running on Win32s calling _chdrive() or SetCurrentDirectory() to change the drive GetCurrentDirectory or _getcwd() will then return the root. The _getdcwd() function is a composite of changing drives, getting the current directory of that drive, and change back to the original drive. Therefore, _getdcwd() will always return the root on Win32s.

Running the following sample to display the current directory of drives C and D under Windows NT properly displays the full path of the drive. Running the sample under Win32s always displays the root ("C:\", "D:\").

Sample Code

#include <direct.h>

...

   status =  _getdcwd(3, szPath, MAX_PATH);    // drive 3 == C:
   if( status != NULL )
   {
      MessageBox( hWnd, szPath, "Current working directory on C:", MB_OK );
   }

   status =  _getdcwd( 4, szPath, MAX_PATH );  // drive 4 == D:
   if( status != NULL )
   {
      MessageBox( hWnd, szPath, "Current working directory on D:", MB_OK );
   }

...


KBCategory: kbprg kbprb
KBSubcategory: W32s
Additional reference words: 1.10 1.20 1.30 1.30a 1.30c


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: December 17, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.