VC++ Interop DLL: Binary was not built with debug information - c#

I have a VC++ (.exe) application built in Visual Studio 2019. In project properties I have set to generate debug information for it.
Configuration Properties --> C/C++ --> Debug information format --> Program Database for Edit and Continue (/ZI)
Configuration Properties --> Linker --> Debugging --> Generate Debug Info --> Generate Debug Information (/DEBUG) (Also I have tried here: Generate Debug Information optimized for sharing and publishing (/DEBUG:FULL)
In the output it generates three files:
myApp.exe
myApp.pdb
myApp.Interop.dll
Now from another C# app I launched this VC++ exe app by using Process.Start("myapp.exe") in order to communicate between them later.
Now I am trying to debug C# and VC++ apps so I open two instances of VS, one for each app.
From VS I start debugging the C# app and from the another VS I attach to the process VC++ app. In the VS where I have launched my C# app, in modules window I can see below message:
C:\Users\myUser\AppData\Local\assembly\dl3\C23J1LN6.XOZ\GJPNWB74.YC1\bdcce8ba\10fb0e7d_e918d801\myApp.Interop.dll --> Binary was not built with debug information
Why isn't it capable to find my myApp.pdb file that was generated? all myApp.* are in the same directory.

It looks like an issue with the debugger type that you are using in the solution with the C# project. Visual Studio will default to "Managed only" when debugging a C# project. However, you want to debug both the C# project and the associated C++ dll. When trying to load the symbols for a native application / shared library it will fail as if there were no valid symbols.
Depending on how you attach to the C# executable:
if you are starting the executable directly from Visual Studio, you need to check "Enable native code debugging" in the Debug tab of your project properties [1];
if you are starting the executable externally and only later attaching using Visual Studio, you need to select "Native" and "Managed" clicking on the Select button in the "Attach to" row.
References
[1] https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-debug-managed-and-native-code#configure-mixed-mode-debugging
[2] https://learn.microsoft.com/en-us/visualstudio/debugger/how-to-debug-in-mixed-mode

Related

Prevent attaching debugger to external DLL

From my C# application I'm integrating with a 3rd-party DLL using interop (DllImport). I suspect that DLL is written in C++ or maybe C++/CLI.
That 3rd-party DLL refuses to run in a debugger (when running my application from within Visual Studio, the DLL produces a message box saying it won't run in a debugger).
Is there a Visual Studio setting oder some interop hack which allows me to run my application in debug mode, but without having the debugger attached to the external 3rd-party DLL?
Because the information you provided is limited, I have given several solutions, please try them one by one.
Dll project, properties - configuration properties - debugging - set the command to bin\Debug*.exe of the exe project that calls the dll
Dll project, properties - configuration properties - debugging - set the debugger type to local only
Dll project, properties - configuration properties - general - set the output directory to the bin\Debug directory of the exe project that calls the dll
Set a breakpoint in the dll project, and then regenerate the dll project, in order to output the dll to the set directory
Exe project, properties - debugging - enable unmanaged code debugging
Exe project, set as startup project
Tools - Options - Debugging - Remove the check mark in front of (Enable "Only My Code") and use Managed Compatibility Mode at the same time. In the c++ project settings, the debugger type should be set to automatic (not set to native only).

VC 2019 c# how include sources of DLL compiled in debug:full from other project to enable full source code navigation

i have a simple c# app that is using DLL from another project which is compiled with debug: full mode
i can step into the code of the DLL but when I'm in the source code i can't click + jump to other source code that is related to the DLL.
how do i configure Visual Studio 2019 to be able to fully navigate the DLL I'm referencing
this is by the way is enable somehow by default in VC++
This is more of a comment/suggestion, but I needed multiple lines, check out the following
The other DLL needs to be built using the same framework version as the code you are searching from
as Paval said in Tools -> Options... -> Debugging -> General make sure the "Enable Just My Code" is not selected
When you are in 'source code' make sure the PDB for the dll you are referencing is in the build directory for the selected configuration

How can I remove debugging information from my C# application?

I am using Visual Studio 2019 to build my C# .NET application, and I want to remove the names of functions/classes from the output file.
I have changed "Debugging information" in the project settings from Pdb-only to None, but I can still see the symbols by attaching a debugger (such as dnSpy) to the program.
I expect the built .exe file to not reveal the name of my functions and classes.

How to debug the c++ dll from c# project?

I have created a visual c++ 6.0 dll project and using it from my c# code. Now i want to debug the dll but i am not able to do it.
I choose the following option to do it:
put the breaking point in my visual c++ project code.
build the dll and copy it into the directory of my c# project.
Now i build my c# project and dll works fine (method are calling perfectly).
Now i want to debug the dll.
I follow a blog and open the c++ project and choose the Attach to process from vc++.
but i am not able to find the running process of visual c# project, whereas it available at task manager process.
In my c# project solution i have two project i.e.
web service (i called the dll method at the time of accessing a url)
Another one is webform application which starts the web services.
Now please help me how should i debug my dll. I have followed so many blogs but all of them are focusing on Attaching process method which is not working in my condition. What should i do?
You'll need to enable unmanaged debugging, it is not turned on by default in either scenario because your main program is managed.
You do it in your C# project with Project > Properties > Debug tab > tick the "Enable unmanaged code debugging" checkbox.
You do it with Tools > Attach to Process by clicking the Select button. Choose the "Debug these code types" radio button and tick both Managed and Native. You may have to resort to only native if your process is 64-bit.
Set a breakpoint in the DLL's source code and be sure to write C# code that exercises the DLL function. If you still have trouble getting a breakpoint then use Debug > Windows > Modules and verify that you see the DLL in the module list. Get additional troubleshooting info by right-clicking it, select Symbol Load Information. Which shows a trace of where the debugger looked for the PDB file.
You can add C++ project to the your C# solution and add reference directly to the project (not dll), then you will not be needing to copy DLL. After that just start normal debugging (F5) of your C# project, and breakpoints will be working for C++ project too. This way will be very comfortable for debugging. I have tried such debugging and did not change any other settings.

Debug c++ dll in C#

I have a .dll from c++ and I want to debug it in C#, but I don't know how to.
When I compiled the c++ project, Visual studio asked me to execute an ".exe".
I supposed that I had to create a project to execute the dll.
But I am lost, how could I debug it?
If I understand you correctly, you want to debug a C++ coded DLL that you created, in a C# project that calls the DLL, which you also created?
I've done this before by going into your C# project properties, and under the Debug section, checking the "Enable unmanaged code debugging" check box. This should allow you to step into your C++ DLL.
To debug a C++ from C# there a couple of things you have to do.
Add a C# project to you solution for your debug application.
Edit the properties of the C# project to "Allow unmanaged code debugging" on the "Debug" tab of the project properties.
Set the C++ project as a dependency of the C# project.
Write code in your C# project to use the DLL either using P/Invoke or COM.
Set some breakpoints in your C++ code and run the C# project.
I overcame this in Visual Studio 2019 by selecting "Enable native code debugging" as shown under my C# project's properies.
Visual Studio cannot execute a dll on its own.
You need to set the startup .exe that will be using your C++ dll in the properties of your dll project. You can do so from properties --> debugging --> command specifying the path of the executable that's gonna call your dll and any command line argument needed.
For VS 2017 Pro, go to the property page of the main project (your c# project in the solution) by right mouse clicking it. At the Debug menu item, find the option in Debugger engines, choose Enable native code debugging.
To complement the useful answers by heavyd and CCicotta and Noobie3001, in Visual Studio 2017 I also had to change the following when attaching to a running C# process calling a C++ DLL: in Debug > Attach to process ..., ensure Native code is listed by Attach to:
SOS (Son of Strike) Debugging Extension (SOS.dll) helps you debug
managed code in Visual Studio by providing information about the
internal CLR environment
refer the below article:-
MSDN ARTICLE
The option "Allow unmanaged code debugging" in the "Debug" tab works for most people, but it doesn't work for me. I have to manually select "Native" as the following to debug c++ dll in my c# code.
FYI: I'm using vs2019, and the dll is written with c++ and loaded into C# app with "dllimport".

Categories

Resources