从c#到c ++ / cli到c ++的debuging函数调用不起作用

时间:2022-06-14 05:19:11

I have a test application write in c#, which calls a function in c++/cli.

我有一个用c#编写的测试应用程序,它调用c ++ / cli中的函数。

c++/cli function then calls a fnction in a c++ code.

然后c ++ / cli函数在c ++代码中调用一个函数。

I have three project:

我有三个项目:

c# project which calls c++/cli code.

c#project调用c ++ / cli代码。

c++/cli project which wraps c++ code.

包装c ++代码的c ++ / cli项目。

All then build ad debug.

然后构建广告调试。

In c# I have a code such as this:

在c#中我有一个这样的代码:

var myFile=MyFileWrapper.MyFileWrapper.ReadMyFile(@"C:\tmp\MyFile.bin");

in c++/cli I have code like this:

在c ++ / cli我有这样的代码:

MyFileWrapper^ MyFileWrapper::ReadMyFile(System::String ^fileName)
{
      auto nativeMyFile=MyFile::ReadMyFile((char *) System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(fileName).ToPointer());
     MyFileWrapper^ MyFileWrapper=gcnew MyFileWrapper();
     int x=nativeMyFile->Width;
      // do some work
     return MyFileWrapper;
}

and in c++ I have this code:

在c ++中我有这个代码:

MyFile* MyFile::ReadMyFile(char * FileName)
{
      auto myFile=new MyFile();
      myFile->Width=100;
      myFile->Height=200;

      return myFile;
}

When I try to debug the system, I can step into c++/cli from C# code, but I can not step into c++ code from C++/CLI code.

当我尝试调试系统时,我可以从C#代码进入c ++ / cli,但是我无法从C ++ / CLI代码进入c ++代码。

When I try to watch nativeMyFile in c++/cli, I can see its address, but not its type or data (for example nativeMyFile->Width in watch window generate error.

当我尝试在c ++ / cli中观察nativeMyFile时,我可以看到它的地址,但不能看到它的类型或数据(例如,watchMyFile-> Width in watch window会产生错误。

when checking value of X, it is 100, which shows the correct value is passed.

检查X的值时,它为100,表示传递了正确的值。

Why can not I step into C++ code from c++/cli code?

为什么我不能从c ++ / cli代码进入C ++代码?

Why I can not watch objects which defined in c++ code?

为什么我看不到c ++代码中定义的对象?

1 个解决方案

#1


4  

Single-stepping from managed code into native code is not supported. Not an issue going from C# to C++/CLI code since they are both managed code. But C++/CLI to native C++ cannot work. You must set a breakpoint on the native C++ code that you want to debug. When the breakpoint hits, the debugger will perform the necessary mode switch to match the type of code.

不支持从托管代码单步执行到本机代码。从C#到C ++ / CLI代码不是问题,因为它们都是托管代码。但C ++ / CLI到本机C ++是行不通的。您必须在要调试的本机C ++代码上设置断点。当断点命中时,调试器将执行必要的模式切换以匹配代码类型。

You'll also need to enable unmanaged debugging in this scenario, it won't be turned on by default since you started a C# project. Right-click your EXE project, Properties, Debug tab, tick the option. The breakpoint in your C++ code will be yellow when you start debugging, it turns into solid red as soon as the native code gets loaded and the breakpoint is armed.

您还需要在此方案中启用非托管调试,默认情况下它不会在您启动C#项目时打开。右键单击EXE项目,“属性”,“调试”选项卡,勾选该选项。当您开始调试时,C ++代码中的断点将为黄色,一旦本机代码加载并且断点处于布防状态,它就会变为稳定的红色。

#1


4  

Single-stepping from managed code into native code is not supported. Not an issue going from C# to C++/CLI code since they are both managed code. But C++/CLI to native C++ cannot work. You must set a breakpoint on the native C++ code that you want to debug. When the breakpoint hits, the debugger will perform the necessary mode switch to match the type of code.

不支持从托管代码单步执行到本机代码。从C#到C ++ / CLI代码不是问题,因为它们都是托管代码。但C ++ / CLI到本机C ++是行不通的。您必须在要调试的本机C ++代码上设置断点。当断点命中时,调试器将执行必要的模式切换以匹配代码类型。

You'll also need to enable unmanaged debugging in this scenario, it won't be turned on by default since you started a C# project. Right-click your EXE project, Properties, Debug tab, tick the option. The breakpoint in your C++ code will be yellow when you start debugging, it turns into solid red as soon as the native code gets loaded and the breakpoint is armed.

您还需要在此方案中启用非托管调试,默认情况下它不会在您启动C#项目时打开。右键单击EXE项目,“属性”,“调试”选项卡,勾选该选项。当您开始调试时,C ++代码中的断点将为黄色,一旦本机代码加载并且断点处于布防状态,它就会变为稳定的红色。