-
October 6th, 2010, 12:41 AM
#1
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?
-
October 6th, 2010, 01:23 AM
#2
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';
}
-
October 6th, 2010, 02:14 AM
#3
Re: SetCurrentDirecoryA ignored inside VS
...the directory change seems to be completely ignored.
Seems, or are you sure ?
-
October 6th, 2010, 02:18 AM
#4
Re: SetCurrentDirecoryA ignored inside VS
Beside, what is "Windows"?
-
October 6th, 2010, 03:22 AM
#5
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.
-
October 6th, 2010, 04:03 AM
#6
Re: SetCurrentDirecoryA ignored inside VS
Code:
static pcstr apszLocations[] = { "..\\..\\media\\definitions" };
SetCurrentDirectoryA( apszLocations[ 0 ] );
char buffer[260];
GetCurrentDirectoryA(260, buffer);
printf("%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.
-
October 6th, 2010, 05:46 AM
#7
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
-
October 6th, 2010, 05:37 PM
#8
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.
-
October 7th, 2010, 05:21 AM
#9
Re: SetCurrentDirecoryA ignored inside VS
Originally Posted by superkemo
I would still like to hear about why I shouldn't use relative paths.
Because
Originally Posted by ovidiucucu
the working directory is not always the application directory
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|