Visual Studio mixed projects debugging - c#

I have a solution primarily based on C# projects, but I will occasionally need to use libraries written in native c++. The way I have solved this is by having a native project that simply acts as a wrapper for a native library, in this case OpenCV (I know I could use EMGUCV directly in C#, but in my experience it has some bugs that I would like to avoid and this is more a proof of concept anyways). I then have a C++ CLR project that references the native wrapper, and finally a C# project referencing the C++ CLR project. Strictly speaking I could just reference OpenCV directly from the CLR project instead of having the native wrapper project, but I would like to organize it this way both to conform to how the rest of the solution is organized and because I might at some point want to have this structure for another reason.
The first problem with this approach is that I can't find a way for Visual Studio to automatically include the dll of the native wrapper in the final target directory. Referencing the native project from the CLR project works flawlessly and referencing the CLR project from the C# project is no problem, but when I build it won't run because the native dlls are not brought to the final target. I solved this by adding some post build actions to copy the native dll and pdb files. This works, and I'm able to run the code with no errors. However, I'm unable to debug the native project. I hit breakpoints in the C# project and CLR projects, but not the native project.
What I have tried:
In the C# project properties I have enabled the native code debugging option.
In the CLR project I have set debugging mode to mixed.
In the native project I have tried both native only and mixed debugging mode
In the C# project I have checked allow unsafe code
In visual studio under tools->options->debugging->general I have tried checking and unchecking use native compatibility mode and suppress jit optimization on module load.
I tried opening the native project in a separate visual studio instance to attach the debugger to the project running from the whole solution, but I was not allowed to do this as the solution debugger was already attached to that project. Someone suggested to do this, but didn't explain how this could be done.

The problem was that the C# project I referred to before was in itself a dll being called by another C# exe project. When I enabled native debugging for this project, everything worked as expected.

Related

How can I debug .a libs(c++ and I have source code) in a Xamarin.forms project?

I have a Xamarin.Forms project and there are also some .a libs(C++) referenced in Xamarin.iOS project.
Xamarin.Forms project
------codes
Xamarin.iOS project
------codes
------.a libs(c++)
I do have the source code of those .a C++ libs.
The question is that is there a way that I can debug the C++ source code when running the Xamarin.forms project?
Update:
I just followed this document and the Xcode does not attach to the Xamarin app when it launches. Any idea?
You cannot debug C++ in a xamarin.ios project, it is not supported and was confirmed by Microsoft's dev team.
Possible solution will be to mix objectiv-C and C++ code together in your xcode application.
Otherwise you will not be able to debug native code.
reference for help:
Using native libraries in xamarin
Xamarin.iOS applications can be debugged with the built-in debugger in Visual Studio for Mac or Visual Studio.
Xamarin.iOS will generate slower and much larger applications as every line of code must be instrumented when you compile applications in Debug mode. Make sure that you do a Release build before releasing.
Use Visual Studio for Mac's native debugging support for debugging C# and other managed languages code and use LLDB when you need to debug C, C++ or Objective C codethat you might be linking with your Xamarin.iOS project.
The Xamarin.iOS debugger uses the Mono Soft Debugger, which means that the generated code and the Mono runtime cooperate with the IDE to provide a debugging experience. This is different than hard debuggers like LLDB or MDB which control a program without the knowledge or cooperation from the debugged program.
Xamarin.iOS ships with the source code for Mono's class libraries, and you can use this to single step from the debugger to see how things are working under the hood.
To debug the class libraries in Visual Studio, you must disable Just My Code under the Debug > Options menu. In the Debugging > General node, clear the Enable Just My Code checkbox:
Please check this link for full debug tutorial: Link

DLL loading (C#/IronPython/C#) in VS2012

I want to debug separate pieces of my application in Visual Studio 2012.
I have C# executable which works with Oracle.DataAccess dll. It works fine.
Within it IronPython runtime is invoked and it works fine too.
Within these IronPython modules object from main C# application is invoked and it works fine with Oracle dll.
If IronPython script is invoked standalone then it works fine and uses C# object fine as well.
However in this case C# object doesn't see Oracle dll.
To debug IronPython scripts I have to create separate Python solution so I cannot configure my C# solution. So I do not have control of C# references. GAC has right Oracle dll but how to tell C# dll to use it?
Vise versa if I'm in C# solution where I can manage the references then I cannot add py files and debug them.
In what way can I configure VS to be able to run/debug my application with dual entry C# or IronPython separately?
You can try replicating what "works for me". Create a solution containing:
python project (ironpython)
C# project
Add a reference to desired oracle library (Oracle.DataAccess.dll) to C# project using the standard VS mechanism. C# project should also contains a post build step to copy the resulting dll and pdb into the place where python script can find it. In my case root of the python project.
Your python project is selected as Startup Project.
I use Ctrl-F5 and F5 to start it. In both cases things work as expected.
In debug mode I am able to set and hit breakpoint in python and in referenced C# module. I can see the oracle library being loaded (Output window of debugger).
However:
The stack traces are C# only.
Visual Studio 2013 Update 4 together with PTVS 2.1 crashes on occasions when debugging.

Build and deliver a C/C++ DLL into a C# app using Visual Studio 2013

I am a Visual Studio 2013 Newbie who is trying to figure out how to build a C DLL in Visual Studio and then invoke the functions exported by the C lib from within a C# app. I've got everything building, but I cannot figure out the proper way of getting the DLL included with the app build so that it will actually load properly when using [DLLImport()]. I currently get a System.DllNotFoundException when attempting to invoke a function from the library from within my C# class.
I have setup a VS 2013 solution which contains the following projects:
HelloWorldLib.Shared :
Shared project that contains the .cpp and .h files for the DLL
HelloWorldLib.Windows :
Win 8.1 C++ project that references the shared project and builds the DLL for Win 8.1
MyApp-Win.Windows :
C# project for Win 8.1 that wants to make use of the HelloWorldLib.Windows.dll produced by the HelloWorldLib.windows build
I have been looking over many SO questions and answers. For instance, I think my question is similar to this one, which doesn't appear to have been answered in any useful way:
Interop, Native DLL and Visual Studio build process
Some of the other answers suggested adding a file reference to the DLL, but how would I do that and keep the proper Debug/Release versions? Given that all of these projects are being built within the same solution, and there are build dependencies between them, it feels like there should be a straightforward way to set this up.
BTW, I am using the shared project setups, as my ultimate goal is to figure out how to get this all working with Xamarin to target iOS, Win, Win Phone, Mac and Android. So that we can use a common C/C++ layer for our high performance requirement code, and the reference it from the UI layers on the various platforms.
This is a pretty straight-forward problem, easy to identify if you look where the built files end up. The C++ build system has a different strategy than the managed build system. It by default writes the build output to the solution directory's Debug directory. Your C# project however builds to its bin\Debug directory and requires all dependent DLLs to be present there.
So, by default, your C# program can never find the DLL, it is in the wrong directory and Windows doesn't look there. Nor is MSBuild smart enough to discover this by itself, like it can for managed DLLs, it cannot see that you program depends on the C++ DLL.
Two basic ways to work around this:
Change the C++ project's Output Directory setting
Write a post-build event for your C# project that uses xcopy.exe to copy the DLL.
Unfortunately things aren't that easy, at least not yet. I was going to have a meeting with Microsoft C++ team but that got postponed, similar functionality you are after was one of the things on my wishlist (building managed C++/CLI wrappers for Xamarin.iOS and Android).
Currently, if you can, the easiest option is to wrap C++ code inside a DLL that exposes C interface. Then build it with XCode and Android NDK. If you can't then the next option is to create Objective-C binding project that wraps the C++ code inside Objective C classes for iOS. For Android either create Java wrapper or use SWIG. Both of these options are cumbersome which is why C API should be the first choice you investigate.
I know it's not the answer you were hoping for but reusing C++ code is way more complicated than reusing C# or even C.

Debugging VB6 project that calls a .Net(C#) dll

I have been stump in this problem for a few hours now. I hope someone has had a similar problem to this.
We have developed a prototype .Net(C#) dll using VS2010, and would like to be able to call this dll in a both C# applications and VB6 application.
My question is:
Is it possible to debug a VB6 application that is calling a .Net dll? I get an error message "Automation Error The system cannot find the file specified"
The error message suggests that there is something missing for my VB6 app to find the .Net dll.
I am aware that if the VB6 application has been compiled, and the .exe has been created, the VB6 will successfully call the .Net dll functionality when using the .exe
However it is important that we can debug through our VB6 application. Unfortunately debugging does not allow you to step over the line of code instantiating the .Net DLL's class object. I can't seem to do this.
NOTE: I have looked around forums and MSDN documentation and I mostly find solution for calling a VB6 dll in .NET; which is unfortunately the opposite of what we need to do.
NOTE: I have already registered the compiled .Net(C#) assembly, and referenced it in the VB6 project.
I have however found these two pages, which seemed to be what we need, but its a solution for calling a .NET(c#) dll generated using VS2005. This doesnt seem to work when the .NET(C#) dll was generated using VS2010.
site1
site2
If someone could give any suggestions or direct me somewhere I can get one, that would be great.
Thanks
SOLUTION
Thanks to #HansPassant, I have found the solution.
To debug a VB6 project that contains a C# .NET assembly, you need to register the .NET dll through both "regasm" and "gacutil", then make sure to close and reopen the VB6 application before you start debugging.
This is not a problem, VB6 uses its own debugger that doesn't get in the way of any other debugger, including the managed one for C# code.
You start from your C# class library project, ensure it is selected as the start project. Project + Properties, Debug tab. Select the "Start external program" option and enter the path to the VB6 IDE. Typically c:\program files\microsoft visual studio\vb98\vb6.exe. Set a breakpoint on the method you want to debug.
Press F5 and the VB6 ide will start running. Load or create your vb6 project. Note how you can add the path to the .vbp project in the previous step so it will automatically load your project.
Start debugging your vb6 project as usual. As soon as it starts using your [ComVisible] C# class then your C# assembly gets loaded. And the breakpoint will hit when your vb6 code calls the method. Switch back and forth as needed. Note that you cannot single-step from vb6 to C# code, you have to set breakpoints to get the debugger to stop.
Ah, the wonders of using .NET from VB6 in a debuggable manner.
in the VB6 project compile options (reached using the Options button on the Make Project dialog window), choose the Compile to Native Code, No Optimization, and Create Symbolic Debug Info options. Then compile your VB6 project. These options allow proper VB6 binary-to-source mapping.
Go to the Configuration Properties...Debugging property page of your solution and change the Start Action to launch your VB6 executable.
In VS Solution Explorer, go to File...Add Existing Item and navigate to the folder containing the VB6 source code you want to debug. Double-click on the VB6 source code file you want to debug, and a source window should open in VS that allows you to set breakpoints in the VB6 source code.
Make sure that your .NET library has a public default constructor. This is essential.
Now also set any C# breakpoints that you need. Do not step into the .NET code - this doesn't work.
When you start debugging with VS, your VB6 and C# breakpoints should be hit normally.
One approach is to debug each individually:
Debugging the VB6 code can be done in the IDE after compiling the C# DLL and adding it as a reference to the VB6 project.
Debugging the DLL with the VB6 host is possible in Visual Studio by compiling the VB6 project and using it in the project properties as the executable to run.
In some cases this is simpler/quicker than setting up the environment to debug both simultaneously.
This approach will require having at least the framework of each working beforehand.

Debug COM dll in VS2010

I have an ASP.NET WebApplication (.NET 4.0/C#) that works with an external COM dll (written in C++).
On a function call, which worked perfectly with an older ASP project, I get a AccessViolationException in the DLL code.
To debug it, I requested a debug version of the DLL from my colleague who wrote it. I have no access to the dll project or source code directly though.
How can I use this debug dll to find out where the error is occuring? I could not find a way to step into the method I am calling. Is it possible?
EDIT: I have the .pdb file as well, if that's any good. For some reason I cannot see the COM dll in the Debug->Windows->Modules overview. I'm really stuck, any help appreciated.
You need to have Unmanaged Code Debugging enabled.
From MSDN:
The Unmanaged code debugging property, available on the Debug page of the Project Designer, determines whether debugging of native code is supported. Select this option if you are making calls to COM objects, or if you start a custom program written in native code that calls your project and you need to debug the native code.
For ASP, you need to check 'Native Code' in the Debuggers section of the Web tab.
Also you should have Just My Code debugging disabled in the debugging options.
Perhaps you could use Ildasm.exe to rip through the dll to see what you are missing. As for violations it sounds as if it is similar to this issue. Try a static import from this SO. MsCorlib.dll may be having an issue with the third party dll.
Unmanaged DLL causing AccessViolationException
Be aware of the dependencies and that you may have some marshaling issues. I advise using DllImport in a console app to see if it can resolve. Best of lucK!

Categories

Resources