Search Unity

How to step into a native C++ dll in Visual Studio?

Discussion in 'Windows' started by kaminate, Jun 25, 2016.

  1. kaminate

    kaminate

    Joined:
    Sep 15, 2015
    Posts:
    5
    I'm debugging my C# unity code through visual studio, and I would like to step into my native C++ code. From C#, I am loading my native c++ plugin using the LoadLibrary function.
    Code (CSharp):
    1. [DllImport("kernel32.dll")]public static extern IntPtr LoadLibrary(string dllToLoad );
    I can:
    • Debug my C# code
    • Load my C++ dll
    • Call my C++ dll functions
    I can't ( and need help with ):
    • Stepping into my C++ dll functions
    • Seeing my C++ dll in Visual Studio's modules window
    Is Visual Studio not loading my pdb? I've already set the dll's pdb location in Visual Studio Tools-->Options-->Debugging->Symbols

    Screenshot of my modules window after calling LoadLibrary() on my dll: http://puu.sh/pFRvc/fa9d73a3db.png
     

    Attached Files:

  2. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    I think you could if the solution included both the c# and c++ projects, which the dependency c# -> c++ library, you may be able to do this.. but I have not, and not really finding any 'how-to' do that.
     
  3. kaminate

    kaminate

    Joined:
    Sep 15, 2015
    Posts:
    5
    Good idea, but unfortunately it didn't work.
     
  4. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    From your screenshot, all loaded modules are managed ones (e.g: .NET based).
    There's a debugger setting in Visual Studio for whether you want managed debugging, native, or both (mixed).

    Check out this link, it might help you in setting it up: https://msdn.microsoft.com/en-us/library/kbaht4dh.aspx
     
  5. kaminate

    kaminate

    Joined:
    Sep 15, 2015
    Posts:
    5
    I don't have a Debugging option in my Configuration Properties o_O



    If I right click on my C++ project and left click on Properties, I can set the Debugger Type of my C++ project.
    However, if I right click on my C# project and left click on Properties, Visual Studio flashes and then then nothing happens...

     

    Attached Files:

  6. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,920
    It seems like you're debugging Windows Standalone, and you're using Visual Studio Tools for Unity, those tools doesn't support mixed debugging.

    You can debug native code, by attaching debugger to executable - In Visual Studio, Debug->Attach To Process.
     
    liortal likes this.
  7. kaminate

    kaminate

    Joined:
    Sep 15, 2015
    Posts:
    5
    If I don't use Visual Studio Tools for Unity, can I enable mixed debugging?
     
  8. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,920
    Could you clarify what platform are you debugging? Mixed debugging only works for Windows Store Apps when using x64 or x86 configurations.
     
  9. kaminate

    kaminate

    Joined:
    Sep 15, 2015
    Posts:
    5
    Clarification!
    My laptop runs 64-bit Windows 10. I am creating my dll with these commands:

    C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\vcvars64.bat
    "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\cl.exe" tacgame.cpp tacclient.cpp tacserver.cpp taccommon.cpp tacutil.cpp /W4 /WX /LD /Fetacwindowsx64.dll /EHs /Zi /nologo /MDd /DLL

    In Unity, I'm currently building the Windows x86_64 standalone,
    ( But I'll do whatever it takes to have convenient debugging )


    I have: 1. My source code, 2. My dll, and 3. My pdb, which are the three ingredients for happiness.
    Happiness being able to step through my C# code in Visual Studio, press F11, and now be stepping through my C++ code.

    ( C# code on the left, C++ code on the right )
     

    Attached Files:

  10. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    What are the debugger settings in visual studio? did you try to see if you have the option to enable native debugging?

    Are you debugging using Unity tools for VS ? this is what it looks like from the image above. i guess you cannot debug native code using that.
     
  11. tswalk

    tswalk

    Joined:
    Jul 27, 2013
    Posts:
    1,109
    do you have any breakpoints in the c++ code?
     
  12. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Mixed mode debugging doesn't work on Windows Standalone (or any platform that runs Mono scripting backend). You can either debug managed code in VS, or debug native code, but not both at the same time.
     
  13. Kolja_Lubitz

    Kolja_Lubitz

    Joined:
    Aug 2, 2021
    Posts:
    4
    Hope this helps someone.
    I used this tutorial to build the C++ Lib: https://levelup.gitconnected.com/in...-as-plugins-a-step-by-step-guide-17ad70c2e3b4

    Than set: In the Properties pane, under Linker > Debugging, select Yes (/ASSEMBLYDEBUG) for Debuggable Assembly.
    discribed here: https://docs.microsoft.com/en-us/visualstudio/debugger/debugging-dll-projects?view=vs-2019

    Than you can debug your native C++ Dlls.
    1. Set a Breakpoint
    2. In the C++ VS Project Debug -> Attach to Process...
    3. In Attach to Select only Native code
    4. Search for Unity and select the Unity with the Project name in Title
    5. Important: There are no code changes allowed in the C++ File. Otherwise the debugger will not work!
    6. But you can now change the C++ Code and hot Apply Code Changes in C++ in runtime.
     
    mbalmaceda, blackdeng, dua2 and 2 others like this.
  14. TinyMuffin

    TinyMuffin

    Joined:
    Dec 31, 2018
    Posts:
    6
    Can confirm that this is working with Unity 2019.4.12, Visual Studio 2019 (16.11.5) and Window 11.
     
    Kolja_Lubitz likes this.
  15. Delian_Pirinski

    Delian_Pirinski

    Joined:
    Mar 11, 2022
    Posts:
    1
    I see the only way to debug the native dll line by line is to attach its project to Unity.exe after running the simulation. I am trying to do this but always VS 2019 says that "The breakpoint will not be hit. No symbols have been loaded for this document".
    Can someone please provide any guidance how to attach the native VS 2019 dll project to Unity.
    Just forgot to mention that the pdb file is there as well.
     
    mc_fragezeichen likes this.
  16. Tautvydas-Zilys

    Tautvydas-Zilys

    Unity Technologies

    Joined:
    Jul 25, 2013
    Posts:
    10,680
    Debug -> Attach to Process. Make sure that the debugger type in set to "Native": upload_2022-3-17_11-25-55.png
     
    Kolja_Lubitz likes this.
  17. cubrman

    cubrman

    Joined:
    Jun 18, 2016
    Posts:
    412
    Works for Win 10, Unity 2021.2.17f1 and VS 2022.

    Few notes:

    - the "Properties pane" is when u right click on the PROJECT (not a solution) in Visual Studio and select "Properties".
    - if u can't open the Properties pane - in Visual Studio go to Tools>Options>Tools for Unity>General and enable the "Access to project properties" option.
    - VS 2022 does not have the Linker tab in Properties pane. I skipped this step and it still worked fine.
    - I was confused about the 2nd point from the original post and in general I did not understand how you debug a cpp file in a project where only the dll of said file exists. That was because I was thinking that you will be debugging inside the C# UNITY PROJECT, which is WRONG. You are performing a debug inside a C++ PROJECT, that produced the dll in the first place. That is why you need to Attach to Process.

    If nothing works, try this just in case:

    - Go to Debug tab in Properties pane and turn on "Enable native code debugging". Do it for TWO projects: "Assembly-CSharp" and "Assembly-CSharp.Player"
    - Turn OFF "Enable just my code" in Visual Studio Tools->Options->Debugger.
     
    Last edited: Apr 18, 2022
  18. mc_fragezeichen

    mc_fragezeichen

    Joined:
    Aug 17, 2020
    Posts:
    14
    Here is my approach towards debugging native c++ code used within a Unity project