CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: SetCurrentDirecoryA ignored inside VS

  1. #1
    Join Date
    Mar 2010
    Posts
    47

    SetCurrentDirecoryA ignored inside VS

    I'm trying to use:

    Code:
    Windows::SetCurrentDirectoryA(string);
    To change my working directory but I'm having an odd problem. The directory change works fine executing the program normally but when I try to debug from inside visual studio 2010 the directory change seems to be completely ignored.

    Any ideas?

  2. #2
    Join Date
    Jun 2002
    Posts
    1,417

    Re: SetCurrentDirecoryA ignored inside VS

    You must be doing something else wrong because this works ok with vc++ 2010 Express both with and without the debugger
    Code:
    #include <iostream>
    #include <Windows.h>
    using namespace std;
    
    int main()
    {
        char buf[255] = {0};
        GetCurrentDirectory( sizeof(buf), buf);
        cout << buf << '\n';
        SetCurrentDirectory("c:\\dvlp");
        GetCurrentDirectory( sizeof(buf), buf);
        cout << buf << '\n';
            
    }

  3. #3
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: SetCurrentDirecoryA ignored inside VS

    ...the directory change seems to be completely ignored.
    Seems, or are you sure ?

  4. #4
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: SetCurrentDirecoryA ignored inside VS

    Beside, what is "Windows"?
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  5. #5
    Join Date
    Mar 2010
    Posts
    47

    Re: SetCurrentDirecoryA ignored inside VS

    Windows is just a namespace.

    Code:
    #if defined ( WIN32 ) || defined ( WIN64 )
    namespace Windows
    {
        #define _WIN32_WINNT	0x0400 
        #include <windows.h>
    }
    Yes, I am sure. Printing path before and after the directory change shows that inside VS the path remains the same, while running the exe from explorer gives the correct result.

  6. #6
    Join Date
    Mar 2010
    Posts
    47

    Re: SetCurrentDirecoryA ignored inside VS

    Code:
        static pcstr apszLocations[] = { "..\\..\\media\\definitions" };
        SetCurrentDirectoryA( apszLocations[ 0 ] );
    
    char buffer[260];
    GetCurrentDirectoryA(260, buffer);
    printf("&#37;s\n", buffer);
    Directory remains the same when run from inside vs.

    Edit: After some messing around I've figured it must be the relative path not working out. However it is strange that it works outside of VS. How should I be doing this the right way?
    Last edited by superkemo; October 6th, 2010 at 04:32 AM.

  7. #7
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: SetCurrentDirecoryA ignored inside VS

    When launching from Visual Studio, the initial current directory is not the application one but the project directory unless you have changed it in project settings/properties.
    Anyhow is not a good practice to use relative paths because the working directory is not always the application directory. Just think about you try to change it twice.

    See this FAQ: http://www.codeguru.com/forum/showthread.php?t=312471

    Also, anytime a function fails take a look at: http://www.codeguru.com/forum/showthread.php?t=318721
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

  8. #8
    Join Date
    Mar 2010
    Posts
    47

    Re: SetCurrentDirecoryA ignored inside VS

    When launching from Visual Studio, the initial current directory is not the application one but the project directory unless you have changed it in project settings/properties.
    Yes, I have set this.

    Anyhow is not a good practice to use relative paths because the working directory is not always the application directory. Just think about you try to change it twice.
    Are you saying to keep a record of the program's installation directory and use absolute paths instead?

    Anyway I checked out the link you posted and after retrieving the function return, there is definitely an error with my relative path. Haven't figured out why yet.

    Edit: OK, it was a very stupid mistake. I had set the working directory to src/release accidentally instead of bin/release. Fixed it now.

    I would still like to hear about why I shouldn't use relative paths.
    Last edited by superkemo; October 6th, 2010 at 05:49 PM.

  9. #9
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,207

    Re: SetCurrentDirecoryA ignored inside VS

    Quote Originally Posted by superkemo View Post
    I would still like to hear about why I shouldn't use relative paths.
    Because
    Quote Originally Posted by ovidiucucu View Post
    the working directory is not always the application directory
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: http://codexpert.ro/blog/author/ovidiu-cucu/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)