Debug a C++ DLL used in ASP MVC application - c++

I have a C++ DLL in bin folder of my ASP MVC5 web application project. (I have copied and pasted it from the original C++ project as I was unable to add a reference) I am able to call some of the functions of the DLL(used DLLImport). I have a problem in one of the functions. This can only be solved when I will be able to step into the code of the C++ DLL. I also have the code of the C++ project (built using Cmake).
How do I reach the C++ code while debugging from my web application code. If I use F11 I should be able to reach the C++ code.

Debug a C++ DLL used in ASP MVC application
1) First, make sure that make sure that the DLL you are referring to has a PDB file or other output file in its folder.
You should open the c++ project in VS IDE and then make sure that you built it with Debug and x64 mode. If so, build it again.
Then in c# web project, use DLLImport to reference the xxx.dll in the output Debug folder which exists pdb and other whole files.
2) Second, go to Tools --> Options --> Projects and Solutions --> Web Projects --> Use the 64 bit version of IIS Express.
3) Third, if you debug a net core web project, please right-click on the project--> Properties-->Debug-->check Enable native Code Debugging.
If you debug a net framework web project, please right-click on the project--> Properties-->Web-->check Native Code.
4) Fourth, then close VS Instance, delete the bin and obj folder of the asp.net core project, then restart your web project and test again.
In addition, you can check this document Debug C# and C++ in the same debugging session.
Update 1
Due to the error, I think you should change to set IIS to x64 since you use x64 native dll. Right-click on your project-->Properties--> Web-->change IIS bitness to x64.

Related

Attaching DLL to managed process doesn't work

I have a C++ DLL project (x86) that I need to debug.
This DLL is consumed by an exe.
I can easily attach the DLL project in VS2017 to a native exe (x86).
When I set breakpoints in the C++ DLL project in VS2017, these break points are hit.
This is the normal, desired behaviour.
Now I have attached the C++ DLL project to a .NET exe (compiled as x86).
Break points are not hit, and I don't have any idea why that doesn't work like with a native exe.
I have unchecked the option "Use Application Framework", but that didn't change anything.
I have also tried the option "Enable native code debugging" without any success.
Also, I have tried to attach it to a Debug version of the NET exe and to a Release version of the NET exe.
I can see that VS2017 attaches to the correct process as when I close the NET exe, VS2017 goes out of debugging mode.
However, breakpoints are not hit.
Is there anything special that I have to take care of?
Most likely, the managed .exe has not loaded the native DLL. Or it has loaded an incorrect build of the DLL, not the one that you’re debugging.
To troubleshoot, add __debugbreak(); call in your native code. That kind of breakpoint is very unlikely to be ignored, unless you mess with structural exception handling. Windows will show you a message offering to attach a debugger, you can choose existing instance of visual studio. Once attached, “Modules” window will show you exactly which build of the native DLL it loaded.
The best way to solve that permanently is add two projects in a single visual studio solution, setup dependency, and ensure the DLL output location is where the EXE looks for the library. I do it all the time in VS2017, with native code debugging enabled I can set my breakpoints in either managed or native code, which helps a lot debugging the interop.

Debugging a .NET COM DLL loaded by unmanaged C++ binary in Visual Studio

I'm using VS2010. I have an unmanaged EXE written in C++ that's using a .NET COM component which is also part of the same solution. I know that the COM object was created successfully because CoCreateInstance returned without an error. Yet, the component symbols aren't loaded (I can also notice that by not being able to create breakpoints in the .NET project source files), so I can't step into the code of the object's methods.
I tried to copy the .NET DLL's PDB into the same output directory of the EXE and it also didn't help. All projects in the solution are x64 and Debugging mode is set to Mixed.
If that matters, the DLL was registered using the command regasm /codebase
Any ideas? Thanks.
Yes, you have to enable managed debugging. One problem with Visual Studio (at least 2008 and 2010 -- don't know about later versions) is that you can only debug Native and Managed code at the same time with 32-bit processes. With 64-bit processes, you have to debug one type or the other, but not both at the same time. I suppose you might be able to spin up another instance of Visual Studio and debug the Native with one instance and Managed with the other.
Under your project settings, go to the "Configuration Properties" - "Debugging" page. On the right go over to Debugger Type and select Mixed.

Problems with running EXE file built with Visual Studio on another computer

I created a client server application in C++ using Visual Studio.
Now I want to run the client EXE file on another computer (which doesn't have Visual Studio installed),
but when I try run the EXE file, it gives the following error message:
This application has failed to start because the application
configuration is incorrect. Reinstalling the application may fix this
problem.
How can I run the EXE file without installing anything on the computer?
Applications built with Visual Studio depend on Visual C++ Redistibutable (VCRedist). When the program is being linked dynamically, then your binaries will need
MSVCR**.dll (Microsoft C Runtime Library).
On MSDN, there is a nice article called Redistributing Visual C++ Files (for Visual Studio 2008), that states that there are Potential run-time errors in case that required Visual C++ library is not installed:
you may get one of the following error messages depending on the version of Windows on which you try to run your application:
The application failed to initialize properly (0xc0000135).
This application has failed to start because the application configuration is incorrect. Reinstalling application may fix this problem.
The system cannot execute the specified program.
Basically you have two options:
The simplest possible solution is to change the dynamic linking of runtime libraries to static linking. Go to project properties and under C/C++ → Code Generation you will find Runtime Library option. You need to change it from Multi-threaded DLL (/MD) to Multi-threaded (/MT).
Another possible solution is to make sure that the right version of Microsoft VC++ Redistributable Package is installed on the target machine.
But your application might depend on other DLL files as well. In case you want to find out what are the dependencies of your program, there is a great utility called Dependency Walker that will help you in this and many other situations :)
Background:
C++ applications need run-time assemblies (DLL files) to run in any Windows computer.
Normally these run-time assemblies are located at C:\Windows\Winsxs directory.
All the Windows operating systems by default comes with several run time assemblies.
But if your application is developed in a newer version of the run-time assembly environment, the target computer also needs the same version of the run time to exist there.
When you're installing Visual Studio, most newer versions of the run-time assemblies comes to your computer.
Solution:
Finally by anyway the target computer should have the exact run time assemblies. There are a few ways to do this (for more details search each in Google).
Statically link run-time assemblies with your application (troublesome for large application).
Install the C++ redistribution environment on the target computer (the easiest way).
Creating a setup project to deploy the run-time on the target computer when installing the application (not bad).
For deploying run-time assemblies as private assemblies (professional), see here for more details
Conditions:
You must not use .NET framework in your application.
You must not use the common language run-time support for your application
I deployed my program in release instead of debug, and the EXE file now works on the other computer.
I haven't seen that specific error before. Usually it's an error around a missing DLL (Windows redistributable). Assuming there isn't actually a problem with the configuration, you have two choices:
Change the compile mode from Multithreaded DLL to Multithreaded. This can be done from the C++ section of project properties under code generation. In multithreaded mode your binary will be statically linked against the Windows redistributable. This is probably what you want.
Install the Windows redistributable on the target machine. This probably isn't OK, because you state that you don't want to install anything on the target machine.
A warning about option 1: Different versions of Windows have different versions of the redistributable. It's possible to encounter a highly specialized environment in which a statically linked program will not behave as expected.
It look like you're missing some DLL files. Be sure to copy appropriate DLL files along with EXE file.
I am running Visual Studio 2019 and I found a very helpful configuration property to address the problem of moving a simple application to another computer without an installation package.
Open the project Property Pages.
Choose which configurations this change should apply to, I used “All Configurations”.
In the left-hand window click to expand the top node called “Configuration Properties”.
Click on "Advanced". In the right-hand window look for the property called “Copy C++ Runtime to OutDir” and set that to “yes”.
Click OK to close the Properties window.
Rebuild your project. All the necessary dlls will be copied to the project’s output directory. Copy your exe and all dlls to another computer. The exe should find everything it needs to run.

VS 2008 C++ how to make a project without .net dependency

I am writing a plain vanilla c++ console app using VS 2008. When I create the project, the IDE gives me a choice of .net versions. There is no option for 'none'. When I look at the project properties page, the Targeted Framework has whatever value I chose and is greyed out.
When I try and run the app on a windows machine without the clr, it gives me a setup error and quits.
There is nothing in my code that has anything to do with .net. How can I escape the clutches of .net and the clr?
How are you creating the project? If I start Visual Studio 2008 and go File / New / Project... / Other languages / Visual C++ / Win32 / Win32 Console Application, I get a plain old C++ project with no .net dependency.
Make sure that you chose the "Win32 Console Application" project type. This will give you a C++ only project. Most of the other console options will bind the project to .Net.
Just choose Win32 Console Application. The drop down at the top of the window is irrelevant.
alt text http://img15.imageshack.us/img15/2029/screenmzi.jpg
The problem (based on your comment under the question) has nothing to do with .NET.
The problem is most likely how you link to the C runtime library.
Visual Studio defaults to using the dynamically linked (dll) version, which means that dll has to be present on the target machine.
The simple fix is to change your project to use the statically linked version.
Under project properties -> C/C++ -> Code Generation, set Runtime Library to Multi-Threaded or Multi-Threaded Debug (but not Multi-Threaded (debug) DLL).
Alternatively, you have to deploy the runtime dll along with your program.
Also if you right click the project in the solution explorer and go to Properties->Configuration Properties->General
You should be satisfied to see that the "Common language support" field should be set to "No common language support" if you followed the above advice i.e. compiling with no clr !
Use Dependency Walker to find out which DLLs are needed to yours program. May be problem is not with .NET, and with Runtime or ATL libraries. Did you used static linkage?

How should I deploy an MFC application?

I created an application in VS 2008 Express as an MFC app just to take advantage of the easy GUI creation. I might re-do this app in pure win32 since no other MFC classes are used (just a button and a text box, the button fires off the main program, all win32). My only question that determines whether I stay in MFC or port it over to pure win32 is this:
How difficult is it to deploy an MFC app? What do I need to do (in VS 2008) to make sure it works on another machine?
Statically link MFC and it's just another .exe.
You can just give that to the user or create an installer with either the microsoft .msi tool or a regular setup.exe with something like innosetup.
Edit - the error message in your comment is about another dll that is part of a 3rd party library. You can't (easily) take a DLL and incorporate it into your app. the licensing may also require you to ship their DLL as a separate lib.
You can use Visual Studio Merge modules. These can be added while building the installer.
Merge modules provide all the dlls, files required to run your application.