Linking to DLL via registry key ignored during debugging in Visual Studio - c++

I'm working in VS 2019 on a C++ program in which I want to link into a bunch of 3rd party dlls, which are located in a folder separate from my executable. I don't want to copy all the dlls into the executable folder and have found the following solution.
Just like in the solution, I create a registry entry to define the keys with the information about the executable and the required path information to link to the DLLs:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MyExe.exe
%Path2MyExe%\MyExe.exe (default key)
Path = %Path2DLLs%
The compilation from within VS works fine and when I start the generated executables from outside VS by (a) double clicking or (b) by using the command line, the 3rd party DLLs are found and the program is executed as expected.
However, when I invoke the debugger from within Visual Studio (c), execution is halted right away complaining that DLLs cannot be found.
I have checked the present working directory from within the code using _getcwd and for all options (a-c) it indicates the same directory.
So the question is: why does option (c) - debugging from within VS - not work?!
Cheers, Sebastian

It's probably because the executable that's launched when debugging is a vshost executable, not the generated executable for the project, and so it needs a separate entry in the registry.
If you want to stick with this kind of a build (highly not recommended), the easiest way to debug is to disable the hosting process:

Following the suggestion by drescherjm I tried to prepend the DLL directory to the PATH variable in "Configuration Properties -> Debugging -> Environment" following this solution.
However, this did not work for me. I also added the path directly in the system environment dialogue without success.
What did work in the end is setting the working directory under "Configuration Properties -> Debugging -> Working Directory" to point to the DLL folder. Now all three calling routes (a-c) work as expected.
Thanks to everyone for taking the time to help me out on this.
Sebastian

Related

Visual Studio 2019 Debugger "Unable to start program ... Access is denied" caused by build file [duplicate]

I have a solution in C:\full path here\VS2010\blender.sln
This solution contains many projects(around 100). When I compile them, they all work fine. I can run them without any problem, and (quite) everything works (there are some bugs).
One of the projects is ALL_BUILD, but it gives the same error if I try to debug INSTALL(another project). I'm compiling with RELWithDebInfo as configuration, and if I execute the program manually it works. It is outputted in C:\full path here\VS2010\bin\RelWithDebInfo
But if I try to run the compiler, it says
"Unable to start program
C:\full path here\VS2010\RelWithDebInfo\ALL_BUILD
Specified file cannot be found"
I tried to copy the compiled program into the path required by VS, but it raised the same error.
What should I do to solve this? Right now I set up cmake to generate also a mingw project and I compile it and debug it with gdb, but this is a really a slow and impractical workflow, and I would like to use the VS debugger.
I must say that if I compile with Debug as configuration, the program doesn't even start.
I'm using VS2010 Express on Win7 64bit
(This is a big open source program, so I don't know exactly whatever it does)
Guessing from the information I have, you're not actually compiling the program, but trying to run it. That is, ALL_BUILD is set as your startup project. (It should be in a bold font, unlike the other projects in your solution) If you then try to run/debug, you will get the error you describe, because there is simply nothing to run.
The project is most likely generated via CMAKE and included in your Visual Studio solution. Set any of the projects that do generate a .exe as the startup project (by right-clicking on the project and selecting "set as startup project") and you will most likely will be able to start those from within Visual Studio.
I had the same problem :)
Verify the "Source code" folder on the "Solution Explorer", if it doesn't contain any "source code" file then :
Right click on "Source code" > Add > Existing Item > Choose the file You want to build and run.
Good luck ;)
I think that what you have to check is:
if the target EXE is correctly configured in the project settings ("command", in the debugging tab). Since all individual projects run when you start debugging it's well possible that only the debugging target for the "ALL" solution is missing, check which project is currently active (you can also select the debugger target by changing the active project).
dependencies (DLLs) are also located at the target debugee directory or can be loaded (you can use the "depends.exe" tool for checking dependencies of an executable or DLL).

Visual Studio doesn't recognize opencv: unable to locate DLL [duplicate]

I am trying just a basic program with OpenCV with the following code:
#include "cv.h"
#include "highgui.h"
int main()
{
IplImage* newImg;
newImg = cvLoadImage("~/apple.bmp", 1);
cvNamedWindow("Window", 1);
cvShowImage("Window", newImg);
cvWaitKey(0);
cvDestroyWindow("Window");
cvReleaseImage(&newImg);
return 0;
}
When I run this, I get
The program can't start because
libcxcore200.dll is missing from your
computer. Try reinstalling the
program to fix this problem.
However, I can see this DLL. It exists. I have added the following to the input dependencies for my linker
C:\OpenCV2.0\lib\libcv200.dll.a
C:\OpenCV2.0\lib\libcvaux200.dll.a
C:\OpenCV2.0\lib\libcxcore200.dll.a
C:\OpenCV2.0\lib\libhighgui200.dll.a
What gives? I'm using visual studio 2008.
When I link the .dll files instead of .dll.a files, I get
fatal error LNK1107:invalid or corrupt
file: cannot read at 0x3F8
libcv200.dll
I followed instructions on http://opencv.willowgarage.com/wiki/VisualC%2B%2B_VS2010 but was still stuck on exactly the same problem, so here's how I resolved it.
Fetched MSVC 2010 express edition.
Fetched Win 32 OpenCV 2.2 binaries and installed in default location.
Created new project.
Project setup
Project -> OpenCV_Helloworld Properties...Configuration Properties -> VC++ Directories
Include Directories... add: C:\OpenCV2.2\include\;
Library Directories... add: C:\OpenCV2.2\lib;C:\OpenCV2.2\bin;
Source Directories... add:
C:\OpenCV2.2\modules\calib3d\src;C:\OpenCV2.2\modules\contrib\src;C:\OpenCV2.2\modules\core\src;C:\OpenCV2.2\modules\features2d\src;C:\OpenCV2.2\modules\flann\src;C:\OpenCV2.2\modules\gpu\src;C:\OpenCV2.2\modules\gpu\src;C:\OpenCV2.2\modules\highgui\src;C:\OpenCV2.2\modules\imgproc\src;C:\OpenCV2.2\modules\legacy\src;C:\OpenCV2.2\modules\ml\src;C:\OpenCV2.2\modules\objdetect\src;C:\OpenCV2.2\modules\video\src;
Linker -> Input -> Additional Dependencies...
For Debug Builds... add:
opencv_calib3d220d.lib;opencv_contrib220d.lib;opencv_core220d.lib;opencv_features2d220d.lib;opencv_ffmpeg220d.lib;opencv_flann220d.lib;opencv_gpu220d.lib;opencv_highgui220d.lib;opencv_imgproc220d.lib;opencv_legacy220d.lib;opencv_ml220d.lib;opencv_objdetect220d.lib;opencv_video220d.lib;
At this point I thought I was done, but ran into the problem you described when running the exe in debug mode. The final step is obvious once you see it, select:
Linker -> General ... Set 'Use Library Dependency Inputs' to 'Yes'
Hope this helps.
Just add C:\OpenCV2.0\bin into your PATH environment variable
or
When you install OpenCV,
Choose the option, Add OpenCV to the system PATH for current user which is not default one
Just for your information,after add the "PATH",for my win7 i need to reboot it to get it work.
In Visual Studio 2013 you need to add this to the Environment Variables and then Restart your pc. This is the path where .dll file located in.
I have had numerous problems with opencv and only succeded after a gruesome 4-6 months. This is the last problem I have had, but all of the above didn't work. What worked for me was just copying and pasting the opencv_core2*.dll (and opencv_highgui2*.dll which it will ask for since you included this as well) into the release (or debug folder - I'm assuming. Haven't tested this) folder of your project, where your application file is.
Hope this helps!
You just need to add the folder of the needed dll file (or files) to your system "Environment Variables" in "Path".
Your problem will 100% be resolved. I had this problem too.
just open the bin folder and copy and paste the .dll files to the folder you are working in..it should fix the problem
As to #Marc's answer, I don't think VC uses the path from the OS. Did you add the path to VC's library paths. I usually add the DLLs to the project and copy if newer on the build and that works very well for me.
you can find the opencv_core248 and other dlls in opencv\build\x86\vc12\bin folder. Just copy the dlls you require into system32 folder. And your app should start working in a flash ! Hope it helps.
This might be like resurrecting a dead horse. But just so it's out there, the reason why the answer to these types of questions to simply put dll's into the system32 folder is because that folder is in the os's system path.
It's actually best practice to provide the os with a path link.
With windows 10
open up file explorer
right click on "this pc" and select "properties"
Now in the "Control Panel\System and Security\System" window that comes up, click on "Advanced System Settings" from the left hand panel.
At the bottom of the next window, click on the "Environment Variables" button.
On the next window, there are two panels, the top one is for modifying variables to the current user, and the bottom panel is for modifying variables to the system. On the bottom panel, find the variable "Path" and click it to select it, then click on the "edit" button.
Here you can then create, edit, delete, or update the different paths for the system. For example, to add mingw32-make to the system so you can access that command via command prompt, click new, then paste in the path to the bin. Example path, "D:\Qt\Tools\mingw730_64\bin", no quotation marks nor additional whitespaces.
Click ok on all the windows so that the changes are saved, then reboot your computer for the changes to be loaded.
The ".a" at the end of your DLL files is a problem, and those are there because you didn't use CMAKE to build OpenCV 2.0. Additionally you do not link to the DLL files, you link to the library files, and again, the reason you do not see the correct library files is because you didn't use CMAKE to build OpenCV 2.0. If you want to use OpenCV 2.0 you must build it for it to work correctly in Visual Studio. If you do not want to build it then I would suggest downgrading to OpenCV 1.1pre, it comes pre-built and is much more forgiving in Visual Studio.
Another option (and the one I would recommend) is to abandon OpenCV and go with EmguCV. I have been playing with OpenCV for about a year and things got much easier when I switched to EmguCV because EmguCV works with .NET, so you can use a language like C# that does not come with all the C++ baggage of pointers, header files, and memory allocation problem.
And as for the question of 64bit vs. 32bit, OpenCV does not officially support 64bit. To be on the safe side open your project properties and change the "Platform Target" under the "Build" tab from "Any CPU" to "X86". This should be done any time you do anything with OpenCV, even if you are using a wrapper like EmguCV.
Copy all .dll from /bin in System32
Just copy the .dll files to C:\windows\system32\
No need to do any of that. It is a visual studio error.
just go here:
http://connect.microsoft.com/VisualStudio/Downloads/DownloadDetails.aspx?DownloadID=31354
and download the appropriate fix for your computer's OS
close visual studio, run the fix and then restart VS
The code should run without any error.

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 2008 isn't creating an .exe file when i build my project. any ideas why?

i'm new to visual studio and couldn't find anything on google about this. i know this is an extremely noobish question, but i can't seem to find any info for it.
the debug shows me whatever i write, and the build has no errors, so i know the code i'm writing is fine.
the release folder doesn't contain the .exe, even after i build it, rebuild, clean, etc.
it's a win 32 console project. the release folder contains the .obj files, the manifest, the build log, idb, pch and pdb files (one of each)
Some possible reasons:
Did you accidentally create a class library project? In that case the output would be a DLL and not an EXE.
Does the output window or the error list display any build errors? In that case you should first fix these, then build again.
Did you change the configuration of the project, so that the output (EXE) is created in a different folder than the default one?
There's not a whole lot of reason for people to be guessing... You said you can find the build log - the exact location of any output file will be in there. To make sure you're seeing the right build log file, the output window in VS will have a link to the file that particular build run created:
1>Build log was saved at "file://c:\DevTrees\cppTest\Debug\BuildLog.htm"
1>cppTest - 0 error(s), 2 warning(s)
If you're having trouble interpreting it, post the contents.
I had the same problem; the advice above to look closely at the output window was just what I needed - thanks. My confusion was because I was looking in solution>project>Debug when VS put it in solution>Debug.
The Release and Debug folders contain outputs from different build configurations.
If you look in Project properties, you will see an Output Folder setting in the Build section, and it will be different for each configuration. (You can see the setting for each configuration using the dropdown list at the top of the Project Properties window)
The Release folder will only get populated when you build the project in the Release configuration.
To switch to the Release configuration, use the dropdown list in the toolbar.
EDIT: I am describing the UI for C# projects. It may be different for native code.
Are you sure that your project type is correct? A class library project won't build an executable. It would need to be some sort of application project to create an executable.
I had the same problem. The compilation went fine, but no .exe was generated in the target folder (.\Debug).
The problem was actually that the file containing the main() function was called "FooProject.cpp". I renamed it to "main.cpp" and then the .exe was generated properly.
In other IDEs such as Eclipse CDT, you don't need to have your main file called "main.cpp" as long as you have a proper main() function. This is apparently not the case for Visual C++.