I have a test application write in c#, which calls a function in c++/cli.
c++/cli function then calls a fnction in a c++ code.
I have three project:
c# project which calls c++/cli code.
c++/cli project which wraps c++ code.
All then build ad debug.
In c# I have a code such as this:
var myFile=MyFileWrapper.MyFileWrapper.ReadMyFile(@"C:\tmp\MyFile.bin");
in c++/cli I have code like this:
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:
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.
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.
when checking value of X, it is 100, which shows the correct value is passed.
Why can not I step into C++ code from c++/cli code?
Why I can not watch objects which defined in c++ code?
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.
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.