Visual Studio - Run the project outside of Visual Studio - c++

The project runs okay in the debug mode of Visual Studio, but when I tried to double-click the exe generated, it says some dll is missing. When I copied the missing dll beside the exe and double-click again, no error message dialog appeared but also nothing happened(the project has Qt-based GUI and reference some external png files).
How does Visual Studio run the exe ? How can I run the exe on my own ? Should I create a installer for the project to make it run on other computers?

you would need to either build statically or provide the required dll files.
the page at http://www.tapkaa.com/2013/05/what-dll-files-are-required-to-run-an-application-developed-with-visual-c/ tells how you can find the missing dll files.

When a process needs to load a DLL by name (without a full path to it), it will check several different places. One of those places may be the current working directory. (The details of the search path are complicated by history and security issues. You can learn the details by looking up LoadLibrary and SetDllDirectory on MSDN.)
In Visual Studio, if you look at the Properties page for the project, and click the Debugging tab, you'll see what directory is set as the working directory when you launch the program from Visual Studio. When you double click on an icon, I believe the working directory will be the directory of the executable file. If these are different, that could explain why you're able to find the DLL in one case but not in the other.
If you're calling LoadLibrary directly, the best thing to do is to always give the full path to the library. Typically, you GetModuleFileName to find out the full path for the executable, then replace the filename portion with the name of the DLL or a relative path from the executable to the DLL.
If you're failing to load an implicitly-linked DLL, then you probably need to make sure your DLL is in the same directory as the executable file.

Related

Why is the DLL not copied to output directory? C++ Visual Studio

I set up a c++ project to use a DLL in visual studio. The library had .h .lib and .dll files. The program compiles and runs, and successfully uses the library. The DLL was put in the root directory of the source, not the output directory. When I build, I expected the DLL to be copied form the source to the output directory, but it's not. All I see in the output directory is 3 files of type, application, incremental linker file, and program debug database.
Maybe I'm wrong but I thought that the DLL needed to be in the same folder as the executable in the output directory. So what's going on?
This is something I had difficulty finding as well, but VS does not copy the .dll's for you. You have to tell it to by creating a Post-Build event. Check out the link on XCOPY:
https://support.microsoft.com/en-us/kb/240268
I needed to copy some dll's to my target directory (the build directory) so used the following in a Post-Build event in my project settings:
xcopy $(ProjectDir)openal32.dll $(TargetDir) /Y /D
Just replace "openal32.dll" with whatever dll you need to copy and make a new build event for each dll you need copied to the target (build) directory.
/Y suppresses prompting to confirm overwrite
/D instructs the compiler to only copy if it's a newer version of the dll on this build.
When I build, I expected the DLL to be copied form the source to the
output directory, but it's not.
Maybe I'm wrong but I thought that the DLL needed to be in the same
folder as the executable in the output directory. So what's going on?
First, DLL's are not part of the build process, that's one reason why Visual Studio doesn't do anything with your DLL.
Having said that, another reason why Visual Studio shouldn't automatically copy over the DLL to your executable directory is that Visual Studio doesn't and cannot assume this is where you want your DLL placed.
It can't assume this because you may want your DLL to be placed in one of the directories that will be found by the Windows OS when it is time to load the DLL. See this link:
Dynamic Link Library Search Order
What if you are testing your app to see if it will load the DLL correctly if it is placed in a certain directory on your PATH? How will you do that if Visual Studio were to always copy the DLL to your executable directory?
Another scenario: What if your program uses LoadLibrary and wants to test if the program behaves correctly if the DLL is not found?
As the other answer suggested, that's why you have to make the concerted effort of telling Visual Studio what to do in the post build event. Also, even after applying the post build event, Visual Studio still knows absolutely nothing about your DLL -- all it sees is a copy or xcopy command.

In visual studio '13 how do I make it so I can include dll files in a relative sub folder of my exe?

In C# I know there's a way to edit the assemble config file or something of that sort, but for C++ I have no clue how to make it so instead of having all my dll's in the root directory put into something like /libs or /bin/ folder and my exe will look there for the dll files?
If you're loading the DLL at runtime you can simply write hDll = LoadLibrary("subFolder\\test.dll");. At first Windows looks for the DLL in the directory your program is located. Therefore test.dll will be found in app_dir/subFolder/test.dll (app_dir = path of executable). Note that this also works if the current directory is different to app_dir.
Look at MSDN for more information about DLL searching.
Your EXE doesn't look for DLL files. That means it doesn't matter whether you're using C++ or C#. Visual Studio is even less relevant as it's not even present when your EXE runs.
Windows looks for DLLs. In your C# example, Windows uses the assembly manifest to guide the search. In your C++ example, you didn't add one, so Windows uses just the default paths. The solution is thus obvious: use a similar manifest.

visual studio 2013 modify path variable when running .exe

I have compiled an exe file in Visual Studio 2013, and it depends on some external files to function. I want to keep those files in the same folder that the exe is in. When I run the .exe by clicking on it in file explorer, it loads the files fine. However, when I run it from Visual Studio, it is unable to run because the PATH variable does not include the directory with the necessary files. How can I configure my project to run the program with a modified path variable so that it can access the files properly?
As far as I understand you question correctly. I suspect that this is an issue related to the different directories when you execute. In Visual Studio (to my knowledge), you have a folder named Debug and a second folder named Release.
You may choose in Visual Studio to run the program in either debug or release mode. But you might not have the required files neccessary in both directories.
For example:
I've made a program that reads "Hello World!" from hello.txt, and displays this in the dialog window as a string.
If I store the file in the Release directory, the executable will run fine outside the IDE, when just launching the executable file.
However, if you run the application in debug mode through the IDE (Visual Studio), the program will not find the neccessary file. The program is looking for the file in it's current directory (Debug).
A quick fix to this is to copy the required files to the current working directory. Eventually have a duplicate set of files in both directories at all times.
Hope that I did understand you correct, and that my answer helps you. :)

visual studio 2013 deploy a project

I crack my head already, I need to deploy a project. I tried to use InstallShield, it create an msi file, and local installation has no errors, but then when I tried to launch the program it output "debug error". In my settings code generator -> runtime library set for Multi-threaded Debug DLL(/MDd). If I'll set it to Multi-threaded (/MT) it would not compile: "Please use the /MD switch for _AFXDLL builds"
Then I tried to use VS Installer project extension. It creates the msi and setup file, but after I install it, I can't find the .exe file and launch program. What I am doing wrong?
Folder with project files
![enter image description here][3]
Folder with exe file
![enter image description here][4]
At the stage of creating the installer for your program, you have to put all the external files, libraries and resources, that your program needs in order to run correctly, in your program's working directory. When you run the program via Visual Studio, everything works fine since it can find the files, because, as you've mentioned, they are in the project folder, and the project folder is where Visual Studio is looking for them.
When you launch your program outside VS, the program can't find these files since it by default tries to locate them in the folder in which the .exe file being run is located. So, if you wan't to run program outside VS, you need to put all the files needed to your .exe's directory.
For the same reason, while creating your installer, you need to include all the files necessary in the installation target directory together with your .exe. Every installer creator lets you do that.
In general, if you cannot run your program outside any IDE, and you can run it within that IDE, it is a rule of thumb that you should first check if you have included all the necessary files/libraries/dll's/etc. in your program's working directory.
I solved it!!!!!!!!
1)Help to run .exe file, helped updating .uld file in the same directory as a exe file.
2)I used Dependency Walker (http://www.dependencywalker.com/) to find all dll that it's need. And then I create a Setup Project using Wizard at the "Choose file to include" step I add every file and every library that it's depend on! Build->Install-> Then magic, and my application installed and running! Thank you all for your help

debug DLL in a different solution

I have an *.exe project that was written in one solution under vs2005 and i have a DLL file
that the *.exe project is using.
the problem is that the dll was written in adiffrent solution and when i try to make attach
to the *.exe file (after i run it) from the dll solution in order to debug the dll , i get no symbols are loaded error (and i cant debug the dll) altough symbols were loaded (i can see the *.pdb files that created after i compiled the dll solution) .
What can I do?
First check the Output window, it will show whether or not it could find debugging symbols for the DLL when it got loaded. Next, switch to Debug + Windows + Modules, right-click your DLL and choose "Symbol load information". That shows where the debugger looked for .pdb files for the DLL. Ensure the .pdb is located in one of these paths.
If the problem is not getting source code for the DLL instead of missing .pdb files, first delete the hidden .suo file in the solution directory. The next time you debug into the DLL, Visual Studio will again prompt you to provide the path to the source code file. Don't press Escape, enter the path. Another thing you can do is right-click the solution in the Solution Explorer window, Properties, Common Properties, Debug Source Files. Add the path to the DLL source code directory.
If you have the visual studio project that generates the dll, compile it in debug configuration and launch debug. You'll be asked for an executable ; select the one you have (from your other solution) and you'll be able to debug the dll.
Now if you want to debug both at once, i believe your way is correct, as long as the dll that the exe uses is the exact one that you've just compiled in your other solution. That might be the problem you're encountering.