none
How to debug native C++ dll from C# application? RRS feed

  • Question

  • Hi All,

    I am using Visual Studio 2005 and .Net CF 2.0 for building an application in WinCE 5.0 based device.

    I have a DLL that was written in C++ and called from a C# application. The DLL is unmanaged code.I have copied the DLL along with the C# app's debug executable in the system directory of the embedded device.But I still can't hit any break points I put into the DLL code. The break point has a message attached to it saying that "no symbols have been loaded for this document", when I tried to attach the dll from the dll solution into the C# executable.

    Moreover, "enable managed code debugging" option is not available in the project settings for embedded device which is available for .Net.

    What else do I have to do to get the debugging in the dll source?

    Regards,

    Arunava

    Wednesday, September 1, 2010 6:18 AM

Answers

  • Launch your managed code as usual using VS debugger. Now you would need to launch second copy of VS and attach it to the process you're debugging. Select native code for this second debugger instance. Now assuming DLL is already loaded (anything from that DLL already executed) you should be able to set a break point.

    Sometimes it helps to show message box in the native DLL so execution would stop at the point you're interested in and you can attach native debugger and continue.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, September 1, 2010 5:21 PM
    Moderator

All replies

  • Hi,

    Go to your Dlls project properties page, click "Debugging". Browse to your "exe" file in the "command" field. Then click "OK".

    Press  F5. It will load your dll and exe and stop at the breakpoint. 

     

    Gurkan 

     


    ASPENDOS
    Wednesday, September 1, 2010 7:33 AM
  • Launch your managed code as usual using VS debugger. Now you would need to launch second copy of VS and attach it to the process you're debugging. Select native code for this second debugger instance. Now assuming DLL is already loaded (anything from that DLL already executed) you should be able to set a break point.

    Sometimes it helps to show message box in the native DLL so execution would stop at the point you're interested in and you can attach native debugger and continue.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, September 1, 2010 5:21 PM
    Moderator
  • Hi Arunava,

    In addition to Ilya's reply, we can also set the c++ dll project's remote executable property to managed exe file path, we can just press F5 to start running the c++ dll project, which will load the managed exe file.

    For more information:
    http://social.msdn.microsoft.com/forums/en-US/vssmartdevicesnative/thread/03b20581-1479-44aa-ba9d-caf1c313c4eb

     

    Best regards,
    Guang-Ming Bian - MSFT


    Please remember to mark the replies as answers if they help and unmark them if they provide no help
    Friday, September 3, 2010 8:55 AM
    Moderator
  • Hi Ilya,

    The procedure that you have mentioned for debugging unmanaged C++ dll from C# application, works in the DESKTOP(Windows, .NET framework) but if we are doing it in WinCE 5.0 (.NET compact framework 2.0), the breakpoint in the unmanaged dll is not getting enabled

    Moreover, if you are developing C# application for desktop(.NET), then an option is available in the following location:-

    Project Property-> "Debug" tab->Enable debuggers->enable unmanaged code debugging.

    However, this option is not atall available for C# embedded applications.

    Any other suggestions.

    Regards,

    Arunava

    Tuesday, September 7, 2010 10:21 AM
  • Procedure which I described above is specific to the device development. AFAIK it will not work on desktop as attempt to attach second debugger instance will fail with the message what debugger is already attached - not what you would need to ever do it on desktop.

    Yes, option you've mentioned is not available for embedded applications which is why you would need to use procedure I described with two debuggers one of which is managed and another one is native. You should set breakpoints in native DLL in the second instance.

    This posting is provided "AS IS" with no warranties, and confers no rights.
    Tuesday, September 7, 2010 10:51 AM
    Moderator
  • Hi Ilya,

    I am still facing problem.So I am mentioning the steps serially that I have followed. Please point out the mistake that I have made or any more steps I have missed.

    For Managed(C#) code:

    1)Compile and deploy the debug version in to the device system folder.

    2)Debug the managed code.

    6)Call the dll function

    7) MessageBox is displayed in the device.

    10)Press Ok button in the messagebox

    For the Unmanaged/native C++ code:

    3) Add a message box in the source code in the function I want to debug

    4)Compile and put the "dll", "lib" and the ".pdb" file into the device system folder.

    5)Put the break point in the function after the message box.

    8)Now attach the process from the native code solution to the managed process.

    9) The break point is not getting enabled.

    11)The break point is not getting hit.

    Regards,

    Arunava

    Wednesday, September 8, 2010 9:54 AM
  • Here's the sequence:

    1)Compile and deploy the debug version by hitting F5 in the "managed" VS. Native DLL can be included with the project as content file. There's no need to deploy anything but DLL. 

    2)Debug the managed code as needed.

    3)Call the dll function.

    4)MessageBox is displayed in the device.

    5)Now launch another instance of VS and attach debugger to the process. Choose native code mode.

    6)Break execution on native debugger. You should have your native DLL on the call stack. Also make sure symbols are loaded for your DLL - load them if not.

    7)Load source code for native DLL it is is not loaded automatically and put the break point in the function you're debugging after the message box. Break point should bind.

    8)Resume execution on native debugger.

    9)Click OK on the device to continue. Break point should hit.


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Wednesday, September 8, 2010 10:48 AM
    Moderator