none
Can't Step into Native Code RRS feed

  • Question

  • Calling C++ from C#. Method runs OK, but I can't step into it to debug. It shows the disassembly screen and says source code not available. Any ideas?

    Code below, in case it helps...

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
    	class Program
    	{
    		[return: MarshalAs(UnmanagedType.I4)]
    		[DllImport("ClassLibrary1.dll", SetLastError = true)]
    		static extern int DoSomething();
    		static void Main(string[] args)
    		{
    			int x = DoSomething();
    			Console.WriteLine(x.ToString());
    			Console.ReadKey();
    		}
    	}
    }
    
    #pragma once
    
    using namespace System;
    
    namespace ClassLibrary1
    {
    	__declspec(dllexport) int DoSomething();
    }
    // This is the main DLL file.
    
    #include "stdafx.h"
    
    #include "ClassLibrary1.h"
    
    int DoSomething()
    {
    	return 3;
    }


    Wednesday, July 9, 2014 8:51 PM

Answers

  • "As for using PInvoke, I didn't realize I was. How did you gather that?"

    You're using DllImport, that means PInvoke.

    "The program still runs, but I still can't step into the C++ code."

    As I mentioned previous, that seems to be VS problem, it doesn't seem to step correctly when the native code is inside a CLR dll. You should recreate that dll, use Win32 Project/DLL instead of CLR Class Library.

    Thursday, July 10, 2014 12:31 PM
    Moderator

All replies

  • You have two project linked together.  When you add one project to another use the following steps

    1) From Solution explorer (or menu) add existing item

    2) Browse to second project in the bin : debug folder select the dll (or exe file).

    You either copied the executable from one project to another or you linked to the release folder instead of the debug folder.  To fix the issue open the Solution Explorer and locate the reference item to the 2nd project.  Right click on the object and delete.  Then do steps 1 & 2 above.


    jdweng

    Wednesday, July 9, 2014 9:21 PM
  • That code won't run correctly, you'll get an error at runtime because the DoSomething function cannot be found in ClassLibrary1.dll. You dll exported a function named DoSomething in the ClassLibrary1 namespace but then you defined a function named DoSomething in the global namespace. Also you need to use 'extern "C"' for that function to actually end up exported with the name "DoSomething".

    In short, delete the namespace/DoSomething stuff in the header and change the cpp file to this:

    extern "C" __declspec(dllexport) int DoSomething() {
        return 3;
    }
    

    This should allow you to run the program correctly.

    As for stepping into native code there are 2 problems:

    • To do this you first need to enable "native code debugging" in the C# project properties, see the Debug property tab
    • Even if you do that you may find that it still doesn't work properly. It appears that your dll is actually a CLR Class Library and the debugger seems to be having problems with debugging native code in a CLR mixed dll

    I don't know why you used a CLR class library dll, it doesn't make sense to do that and then use PInvoke.

    Thursday, July 10, 2014 5:56 AM
    Moderator
  • Hi Mike,

    Thanks for the reply; I appreciate your interest.

    The program does run, actually - I just can't enter into the native code to debug.

    As for using PInvoke, I didn't realize I was. How did you gather that? (I don't work with native code much - sorry if it's a dumb question)

    I made the changes you suggested - in fact, removed the header altogether. "Native code debugging" was already enabled. The program still runs, but I still can't step into the C++ code.

    Anyway, I want to try a few other things during my free time, and will post anything interesting that I find. Thanks again for your help,

    Steven


    Thursday, July 10, 2014 11:48 AM
  • "As for using PInvoke, I didn't realize I was. How did you gather that?"

    You're using DllImport, that means PInvoke.

    "The program still runs, but I still can't step into the C++ code."

    As I mentioned previous, that seems to be VS problem, it doesn't seem to step correctly when the native code is inside a CLR dll. You should recreate that dll, use Win32 Project/DLL instead of CLR Class Library.

    Thursday, July 10, 2014 12:31 PM
    Moderator