如何中断从 C# Interop 调用的 C++ 代码
How to break in c++ code called from c# interop
使用 Visual Studio 2019。从 c# 调用 c 函数很常见,但是当出现运行时错误时,我会得到一个弹出窗口 window:
不过,我想真正进入 C++ 代码,以便调试它,特别是查看调用堆栈、观察局部变量、添加断点等。
如何实现?
如果需要演示项目:
https://github.com/mprevot/InteropDemo
我正在调用一个旨在失败的 c 函数:
#ifndef Pinvoke
#define Pinvoke extern "C" __declspec(dllexport)
#endif
std::wstring ToString(int number)
{
std::wstringstream s;
s << "got number " << number;
return s.str();
}
Pinvoke auto GetNumbers() -> void
{
std::vector<int> array0{11,12,13,14};
std::vector<int> array1{21,22,23};
for (size_t i = 0; i < 4; i++)
ToString(array0[i]);
for (size_t i = 0; i < 4; i++)
ToString(array1[i]);
}
我从 c# 调用这样的函数:
internal static class NativeLibCall
{
public const string _dll = "NativeLib.dll";
[DllImport(_dll, CallingConvention = CallingConvention.StdCall)]
internal static extern void GetNumbers();
}
public class NativeLibInterop
{
public void GetNumbers() => NativeLibCall.GetNumbers();
}
按照 Kevin Gosse 的建议并在 MSFT documentation 中进行了详细说明,只需启用 混合模式调试 :
Enable mixed-mode debugging for a managed calling app
Select the C# or Visual Basic project in Solution Explorer and select the Properties icon, press Alt+Enter, or right-click and choose
Properties.
Select the Debug tab, and then select Enable native code debugging.
Close the properties page to save the changes.
使用 Visual Studio 2019。从 c# 调用 c 函数很常见,但是当出现运行时错误时,我会得到一个弹出窗口 window:
不过,我想真正进入 C++ 代码,以便调试它,特别是查看调用堆栈、观察局部变量、添加断点等。
如何实现?
如果需要演示项目: https://github.com/mprevot/InteropDemo
我正在调用一个旨在失败的 c 函数:
#ifndef Pinvoke
#define Pinvoke extern "C" __declspec(dllexport)
#endif
std::wstring ToString(int number)
{
std::wstringstream s;
s << "got number " << number;
return s.str();
}
Pinvoke auto GetNumbers() -> void
{
std::vector<int> array0{11,12,13,14};
std::vector<int> array1{21,22,23};
for (size_t i = 0; i < 4; i++)
ToString(array0[i]);
for (size_t i = 0; i < 4; i++)
ToString(array1[i]);
}
我从 c# 调用这样的函数:
internal static class NativeLibCall
{
public const string _dll = "NativeLib.dll";
[DllImport(_dll, CallingConvention = CallingConvention.StdCall)]
internal static extern void GetNumbers();
}
public class NativeLibInterop
{
public void GetNumbers() => NativeLibCall.GetNumbers();
}
按照 Kevin Gosse 的建议并在 MSFT documentation 中进行了详细说明,只需启用 混合模式调试 :
Enable mixed-mode debugging for a managed calling app
Select the C# or Visual Basic project in Solution Explorer and select the Properties icon, press Alt+Enter, or right-click and choose Properties.
Select the Debug tab, and then select Enable native code debugging.
Close the properties page to save the changes.