How to specify the directory of a .config file in C++ code? - c++

I have a C++ program that I built using Visual Stuidos. It has to read some variables from a .config file. I notice that when I run the program from VS (i.e. I open the VS solution and press the "Start Without Debugging" button), it can't find the .config file unless it's in the "release" or "debug" directory. However, I'd like to be able to run the program from the VS solution, while keeping the .config file in the same directory as the .sln file. Is there a way I can set the path of the .config file to this directory, perhaps somewhere in the source code?
Thanks.

I would take advantage of Visual Studios directory macros and set an additional include path in your solution directory, I answered a question previously on this site relating to include directories, so here is a diagram I created:
bigger link here
Don't pay attention to the text on the left side, but the black text on the right side is what you're looking for. $(SolutionDir) is a macro that finds the directory of your solution, and adding this will allow your program to have access to this directory during the program.

You can call GetModuleFileName to get the full path to your exe file. From there you can remove the lowest level (/debug or /release) to get the project path.
That should get you what you asked for, but what you asked for is not a good approach. Putting your config file in your exe directory will encounter problems later on with non-admin user rights. The recommended place for such files can be found by calling SHGetFolderPath, where you can create an accessible folder for your app.

However, I'd like to be able to run the program from the VS solution,
while keeping the .config file in the same directory as the .sln
file.
Just set Project Properties|Debugging|Working Directory to $(SolutionDir). Or any directory you want.

Related

Referencing external dll in Visual Studio 2015 [duplicate]

I developed an application that depends on a DLL file. When I debug my application, the applicationwould complain that:
"This application has failed to start because xxx.dll was not found."
So I have to copy the DLL file into the same directory as my .vcproj file.
Is there a way to set the project to look for the DLL file in (preferably) some relative path or (not preferred) some absolute path?
Similar concept to how we set include and library path in the project settings.
I mean when I debug my application (hitting F5) the above error would pop up.
Go to project properties (Alt+F7)
Under Debugging, look to the right
There's an Environment field.
Add your relative path there (relative to vcproj folder) i.e. ..\some-framework\lib by appending PATH=%PATH%;$(ProjectDir)\some-framework\lib or prepending to the path PATH=C:\some-framework\lib;%PATH%
Hit F5 (debug) again and it should work.
Go through project properties -> Reference Paths
Then add folder with DLL's
The search path that the loader uses when you call LoadLibrary() can be altered by using the SetDllDirectory() function. So you could just call this and add the path to your dependency before you load it.
See also DLL Search Order.
Another possibility would be to set the Working Directory under the debugging options to be the directory that has that DLL.
Edit: I was going to mention using a batch file to start Visual Studio (and set the PATH variable in the batch file). So then did a bit of searching and see that this exact same question was asked not long ago in this post. The answer suggests the batch file option as well as project settings that apparently may do the job (I did not test it).
In your Project properties(Right click on project, click on property button) ▶ Configuration Properties ▶ Build Events ▶ Post Build Events ▶ Command Line.
Edit and add one instruction to command line.
for example copy botan.dll from source path to location where is being executed the program.
copy /Y "$(SolutionDir)ProjectDirs\x64\Botan\lib\botan.dll" "$(TargetDir)"
I had the same problem and my problem had nothing to do with paths. One of my dll-s was written in c++ and it turnes out that if your visual studio doesn't know how to open a dll file it will say that it did not find it. What i did was locate which dll it did not find, than searched for that dll in my directories and opened it in a separate visual studio window. When trying to navigate through Solution explorer of that project, visual studio said that it cannot show what is inside and that i need some extra extensions, so that it can open those files. Surely enough, after installing the recomended extension (in my case something to do with c++) the
"This application has failed to start because xxx.dll was not found."
error miraculously dissapeared.
I know this question had been answered years ago, but for those like me who needed to change where the debugger starts the application, change the command property under Project Properties -> Debugging.

visual studio 2013 c++ add path to dll [duplicate]

I developed an application that depends on a DLL file. When I debug my application, the applicationwould complain that:
"This application has failed to start because xxx.dll was not found."
So I have to copy the DLL file into the same directory as my .vcproj file.
Is there a way to set the project to look for the DLL file in (preferably) some relative path or (not preferred) some absolute path?
Similar concept to how we set include and library path in the project settings.
I mean when I debug my application (hitting F5) the above error would pop up.
Go to project properties (Alt+F7)
Under Debugging, look to the right
There's an Environment field.
Add your relative path there (relative to vcproj folder) i.e. ..\some-framework\lib by appending PATH=%PATH%;$(ProjectDir)\some-framework\lib or prepending to the path PATH=C:\some-framework\lib;%PATH%
Hit F5 (debug) again and it should work.
Go through project properties -> Reference Paths
Then add folder with DLL's
The search path that the loader uses when you call LoadLibrary() can be altered by using the SetDllDirectory() function. So you could just call this and add the path to your dependency before you load it.
See also DLL Search Order.
Another possibility would be to set the Working Directory under the debugging options to be the directory that has that DLL.
Edit: I was going to mention using a batch file to start Visual Studio (and set the PATH variable in the batch file). So then did a bit of searching and see that this exact same question was asked not long ago in this post. The answer suggests the batch file option as well as project settings that apparently may do the job (I did not test it).
In your Project properties(Right click on project, click on property button) ▶ Configuration Properties ▶ Build Events ▶ Post Build Events ▶ Command Line.
Edit and add one instruction to command line.
for example copy botan.dll from source path to location where is being executed the program.
copy /Y "$(SolutionDir)ProjectDirs\x64\Botan\lib\botan.dll" "$(TargetDir)"
I had the same problem and my problem had nothing to do with paths. One of my dll-s was written in c++ and it turnes out that if your visual studio doesn't know how to open a dll file it will say that it did not find it. What i did was locate which dll it did not find, than searched for that dll in my directories and opened it in a separate visual studio window. When trying to navigate through Solution explorer of that project, visual studio said that it cannot show what is inside and that i need some extra extensions, so that it can open those files. Surely enough, after installing the recomended extension (in my case something to do with c++) the
"This application has failed to start because xxx.dll was not found."
error miraculously dissapeared.
I know this question had been answered years ago, but for those like me who needed to change where the debugger starts the application, change the command property under Project Properties -> Debugging.

Visual Studio looking for a file in the Desktop directory instead of the Project Directory [duplicate]

I've got an a project (native C++ compiled to .exe) that runs fine outside of VS 2010, but inside of VS (with or without the debugger) it gets stuck trying to find a text file located in the same bin folder as it.
Any ideas as to why this would happen? My hunch is that VS messes with where the code looks when trying to open a file, but I don't know enough details to correct this.
Some details:
My .exe calls a function from a .dll I wrote earlier, which in turn tries to find a text file specifying that function's parameters. For convenience I've placed all of these files in the same folder, so finding the text file wouldn't be a problem.
Before opening the file, the function checks that it exists using:
PersistentAssert(Utility::FileExists(Filename), "Parameter file not found");
In VS 2010 this line causes "Parameter file not found" to display, but outside of VS the program manages to find the file.
When launching an executable from visual studio by default it uses the project file directory as the current working directory. You can change it in Configuration Properties -> Debugging -> Working Directory.
However I don't think it's correct behavior to search your programs datafiles etc from the current working directory. Instead your program should find out the directory of the executable and find the files in that directory.
Processes have the concept of a "current directory", which may or may not be the same directory as where the .exe file is located. It sounds like when you run your program inside VS, the current directory is something other than where your .exe is.
Somewhere in the project settings, there should be a place where you can choose what directory will be the current directory when your program starts inside the debugger. Set that to the same location as your .exe and you should be good.

Change current working directory VS13?

As stated in this post the working directory when I debug my SDL program is relative to the .vcproj instead of the .exe (which it should be IMO)
So I'm wondering if there's anyway I can change this, so when i press F5 the path will be relative to the .exe and not .vcproj?
The current (relatively easy) workaround I'm using, is simply opening up the folder and starting program from there, but I would much rather prefer simply pressing F5.
The naive answer to your question is that you can set the Working Directory option in the Debugging configuration properties to $(TargetDir). The default setting is $(ProjectDir) and by default the project directory is not where the executable file is output. However, I do not recommend you take this option, as I explain below.
You are attempting to solve this the wrong way. Your fundamental problem is that you are assuming that the working directory is the same as directory which contains the executable file. There's no reason for that to be so, and you should not rely one it. You know that the files are in the same directory as the executable and so you should look there, rather than the working directory which is only sometimes, coincidentally, the same as the executable directory.
So, instead of relying on the invoker of the process setting up the working directory to your liking, make your program independent of the working directory. You state that you wish to work with files whose location you know relative to the executable directory. So there is the answer. Construct full paths to your files, using the directory which contains the executable as the base.
If you need to find out the location of the executable, call GetModuleFileName(), and strip off the file name. What you have left is the directory which contains your executable. Combine that with the relative path of your files and your code is now independent from the working directory.

Where do data files go so the Microsoft Visual C++ 2008 debugger can find them?

I am writing code that opens an istream object on a file specified by the user. I want to be able to run the program in the debugger and just type the filename (eg data.txt) at the prompt, not the whole path. I haven't worked out how to do this inside the IDE so I have been saving my .txt file to the debug folder and running the .exe file, but that means I can't step through the program. How do I make it work inside the IDE instead? Thanks.
you can set the working path of the executable (project properties->Debugging->Working Directory), which leads the debugger to start the executable with that path as working directory. This has the advantage that if you set the same path for all your configurations (Debug/Release/...), you only need 1 data.txt on your entire system, which is especially nice if you want to change data.txt or it's name.
I am not sure I understand exactly the problem - is it that your data file is part of the project, but is not in the executable folder when you access it, or is it that the datafile is at another location? If the former, and the data file is part of the project, right-click on the file, and set the Build Action property to "Content". That way, it will get copied to the bin/debug folder where the executable runs when you debug.