I have a function in C++ that writes text into an std::ostream
void foo_cpp(std::ostream* log)
{
(*log) << "foo" << std::endl;
}
The C++ code is called from C# code using a managed layer with CLI. Currently the managed C layer gets a path to a file, creates an ofstream
object and passes this to the C++ code:
void foo_cli(String^ filePath)
{
msclr::interop::marshal_context context;
const std::string logPathCpp = msclr::interop::marshal_as<std::string>(filePath)
std::ostream* log = new ofstream(logPathCpp);
foo_cpp(log);
}
How can I move the logic of creating the output stream to the C# code. Something like:
void foo_csharp()
{
FileStream log = new FileStream(@"D:\log.txt", FileMode.Create, FileAccess.Write);
foo_cli2(log);
}
void foo_cli2(FileStream^ logCsharp)
{
std::ostream* logCpp = someMagic<std::ostream*>(logCsharp);
foo_cpp(logCpp);
}