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

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.

Related

Visual Studio - Run the project outside of Visual Studio

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.

Starting an executable program that uses additional libs

I made a program with Microsoft Visual Studio 2010. It uses additional libraries (Allegro), and it runs perfectly from MSVC, but I can't run it from it's directory. (...\"project name"\Debug\"project name".exe) It writes that it can't find some kind of .dll files.
That's not good, because I want to make it work for everyone! What to do to make it work?
I know that I have to put the necesarry .dll files, but I don't know where?
When an executable is started, Windows searches the current directory, the PATH and then some other places. The exact description can be found here
You need to make sure either the PATH includes the library you need, or place it in the same directory (or in some other automatically searced directory, but that's typically not a good solution).

opencv_highgui230.dll was not found

I am creating an application by using opencv2.3 IN VC++2010 express addition. The build is successful but while compiling it says that 'opencv_highgui230.dll was not found.Reinstalling the application may fix the problem.' Though I have added all the necessary include and lib files.
It's likely that this DLL can be found in bin or similarly named directory under where you installed the OpenCV library. For Windows binary distributions of various libraries, the DLL is usually included.
For your program to load it, it either has to be in the same directory as the executable, in your system directory, usually C:\Windows\system32\, or I think that it is possible to specify the location programmatically, in your code. This MSDN article can tell you more.
Quick and, more likely then not, correct solution would be to copy the DLL into your executable's directory.
Because your application relies upon the library, you have to build the library first before you build your application. The error message is telling you that it can't find the binary file corresponding to your library, opencv_highgui230.dll, not one of the code files.
You can either configure Visual Studio to automatically build the projects in the correct order by setting up the appropriate project dependencies, or you can do it manually.

How can I run an .exe with DLLs in separate directory?

I know that this was already discussed somewhere in here but I have not find question that I wanted, namely: I have a C++ application that uses a lot (more than 20 -30 ) DLLs. I have defined in my Visual Studio 2010 project that the .exe will be copied to the ProjectDir (so that everything is neat and clear) but when the .exe is standing in the ProjectDir alone it cannot access the DLLs stored in the bin..// whatever folder along with many other files.
Can I somehow point the DLL folder so that the application will know where they are located ? (and the <myapp>.exe.local folder thing does not work in my Windows 7)
first of all there is no need to copy your exe file to your project dir, whereever your .exe file is created when you are debuging your project the running dir would be your project dir. and after that when you are trying to import the dll if you look for it relatively windows first search for that dll in your running dir then then it checks if it can find the dll in the whatever directory defined system PATH variable, but if you check for a absolute address there will be no looking.
so the first trick is to set all your dll pathes abslote so that there will be no searching and dll are imported easily but there will be a lot of problems if you want to move your application to another computer (eg HINSTANCE hDLL = LoadLibrary(L"C:\\mydll.DLL");)
. second you can give your dll pathes relative to the running dir (not the application path, these 2 may differ) and you can also specify directory for that (eg. HINSTANCE hDLL LoadLibrary("..\\dlls\\mydll.dll")
You may set the PATH variable.
Here you can find where windows looks for dll: http://msdn.microsoft.com/en-us/library/ms682586%28v=vs.85%29.aspx
and here how to set the path in windows 7: http://geekswithblogs.net/renso/archive/2009/10/21/how-to-set-the-windows-path-in-windows-7.aspx

How can I automatically load DLLs from a subdirectory?

In Visual Studio, you create a .dll project and it creates a .dll and .lib files. You link statically to the .lib, and if the .dll is in the same folder as the .exe, everything works.
I suspect that everything would also work if the .dll was in System32 or any other PATH folder (confirm or correct, please).
But here's the question: I want my exe to find the .dll in ./DLLS/ folder, that is, if my exe is in ....../MyApp/MyApp.exe then it should look for the .dll in ...../MyApp/DLLS/MyDll.dll. I DO NOT want to include the DLLS folder in path. Is there any way I can do this?
Please note that I do not wish to use LoadLibrary explicitly, so I can't specify the path there.
Thanks in advance for any help.
Here is the default sequence which Win32 applications go through when looking for a DLL:
http://msdn.microsoft.com/en-us/library/7d83bc18(VS.80).aspx
So according to this, another approach might be to call SetCurrentDirectory or SetDllDirectory. But in order for this to work you have to use the Delay Loaded Library functionality (you specify that in Project Settings in Visual Studio). Delay loaded library means that the DLL is loaded only at the moment when the program needs it, not automatically at the programs' startup.
You could use SetDllDirectory for this. The loader will use the additional directory you specify when loading libraries. There can only be one additional directory, however, so you need to make sure that there aren't other calls to this at a later point in your application, otherwise the directory you specify will be ignored.
If that API does not allow relative directories (I'm not sure), you could always call GetModuleFileName with a NULL first parameter to get the file name of the currently executing program. With a little string manipulation, you can get the absolute path to your DLLs folder.