从C#

时间:2019-10-09 00:17:16

标签: c# c++ c++-cli

此问题类似于以下未解答的问题:Debugger does not step into native code when debugging a static lib wrapped in a C++/CLI DLL

设置相同。我有一个纯C ++静态库,该库链接到C ++ / CLI DLL,然后由C#可执行文件使用。根据设置,我可以调试C#层,也可以调试C#和C ++ / CLI。无论我尝试什么,都无法调试C ++层。我正在使用Visual Studio 2019。

这是我尝试过的结果。对于下面描述的所有情况,我都在C ++ / CLI和C ++函数(不是C#)上设置了断点。

  1. 具有本地调试功能的C#,具有自动调试功能的C ++和C ++ / CLI:调试器在C#中调用C ++ / CLI函数的调用处停止,但我无法对其进行设置。 Visual Studio在C ++ / CLI端没有关于断点的消息(它们是活动的,但不能被击中)。在C ++方面,它表示:
This breakpoint will not currently be hit. Breakpoints in module clr.dll are not alowed. This module contains the implementation of the underlying runtime you are trying to debug.
  1. 没有本地调试的C#,带有混合调试的C ++和C ++ / CLI:C ++ / CLI中的断点有效。 C ++断点或者消失,或者显示以下消息:
This breakpoint will not currently be hit. No executable code of the debugger's target code is associated with this line. Possible causes include: conditional compilation, compiler optimizations, or the target architecture of this line is not supported by the current debugger code type.
  1. 具有本地调试功能的C#,具有混合调试功能的C ++和C ++ / CLI:请注意第一点,其行为是相同的。
  2. 没有本地调试的C#,带有本地调试的C ++和带有混合调试的C ++ / CLI:与第2点相同。

我的代码如下:

C ++ Native.hpp:

#pragma once
namespace native
{
   class Native
   {
   public:
      Native();

      bool here() const;
   };
}

C ++ Native.cpp:

#include "Native.hpp"

#include <iostream>

namespace native
{
   Native::Native()
   {
      std::cout << "Created native entity!" << std::endl; // breakpoint here
   }

   bool Native::here() const
   {
      std::cout << "Native is here!" << std::endl; // breakpoint here
      return true;
   }
}

C ++ / CLI Wrapper.h:

#pragma once

#include "../cpp/Native.hpp"

using namespace System;

namespace clr
{
    public ref class Wrapper
    {
   public:
      Wrapper() 
      { 
         Console::WriteLine("Building wrapper for native"); // breakpoint here
         mNative = new native::Native(); 
      }
      ~Wrapper() { delete mNative; }
      bool go() 
      { 
         Console::WriteLine("In wrapper to find native..."); // breakpoint here
         return mNative->here(); 
      }

   private:
      native::Native* mNative;

    };
}

和C#:

using System;
using clr;

namespace csharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Wrapper w = new Wrapper();
            Console.WriteLine("Finding native through wrapper...");
            w.go();
            Console.WriteLine("Waiting...");
            Console.Read();
        }
    }
}

我已经尝试了几乎所有可以想到的东西,并且无法调试C ++方面。我真的很感激能解决这个问题。

1 个答案:

答案 0 :(得分:0)

可能很简单,例如在Visual Studio中未启用Enable native code debugging

docs.microsoft.com的Tutorial: Debug C# and C++ in the same debugging session包含成功进行c#+ c ++调试会话的步骤。

相关问题