Sign in to follow this  
Leadorn

get set currentdirectory win32 base

Recommended Posts

Hi I have a problem using get set current directory in a multithreaded environment. My program is a plugin (dll) that is loaded from another application. The problem is that my set and get current directory calls messes up the opening of other files by the parent process. I use a directory guard on the stack to save and restore the current directory. I still have problems. Is the working directory global to the process maybe? That’s bad...... suggestions?

class DirGuard
{
public:
	char tmp[MAX_PATH];
	DirGuard(std::string dir)
	{
		GetCurrentDirectory(sizeof(tmp),tmp);
		SetCurrentDirectory(dir.c_str());
	}

	~DirGuard()
	{
		SetCurrentDirectory(tmp);
	}
};


Share this post


Link to post
Share on other sites

I had similar problems lately, so I concluded that all threads of 1 process are operating in the same directory. Can somebody confirm this ?

My solution was a single interface for opening files. So every file opened in any module goes through the same code and that code is "guarded" so that only one thread can request a file at one time. Also that is the only code which is changing directories.

Although, most of the time, the main program is feeding the modules with the files it has opened. Still it has to be thread safe.

Share this post


Link to post
Share on other sites
Quote:
Original post by Demus79

I had similar problems lately, so I concluded that all threads of 1 process are operating in the same directory. Can somebody confirm this ?

My solution was a single interface for opening files. So every file opened in any module goes through the same code and that code is "guarded" so that only one thread can request a file at one time. Also that is the only code which is changing directories.

Although, most of the time, the main program is feeding the modules with the files it has opened. Still it has to be thread safe.


Confirmation. The current directory is a process information. Moreover, I don't see any need to change it. Is open("foo.bar") better than open(basedir + "foo.bar")? I don't think so.

If you know where your files are lying, just use the absolute path. If you are tired to play with thiese damn path, use boost.path :)

Regards,

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this