Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing an fstream (or equivalent) from C# to C++ through CLI

How can I pass an fstream or equivalent from C# through CLI to an unmanaged C++ DLL?

Rough application outline:

  • C# application reads a binary file from a database
  • Unmanaged C++ dll is used to "decode" this file and return the information contained therein
  • I can modify any of the C# code. The CLI wrapper is the only portion of the C++ side that I can modify.

I'm currently saving the binary file to disk and passing the path of it to the CLI wrapper where it is opened as an fstream. This is fine for test purposes, but won't work for production for obvious reasons.

I've also looked into passing a byte array to the DLL but I was not able to find a way to convert that to an fstream other than with GlobalAlloc, which I would prefer not to use.

Any help or ideas would be appreciated.

Thanks.

like image 571
AaronN Avatar asked Jul 06 '11 18:07

AaronN


People also ask

What is fstream in C?

#include <fstream.h> In C ++ you declare variables of the ofstream and ifstream classes to get output and input file streams, respectively. Output streams are used to write to files just as you would cout. Input streams are used to read from files just as you would cin.

What is the meaning of fstream in C++?

fstream Library: Fstream is a library that consists of both, ofstream and ifstream which means it can create files, write information to files, and read information from files. This header file is generally used as a data type that represents the file stream.

Should I use fstream or ifstream?

It is basically possible to never use ifstream and ofstream and always use fstream with the required flags. But it is prone to accidental errors while setting the flags. Hence, using ifstream you can be sure that writes will never occur and with ofstream only writes will take place.

Is fstream a class in C++?

In C++, files are mainly dealt by using three classes fstream, ifstream, ofstream available in fstream headerfile. fstream: Stream class to both read and write from/to files.


1 Answers

You can pass a managed binary array to the C++/CLI DLL. Pin the array. This can then be converted to an STL string object. You can then pass the STL string into an STL stringstream object, which inherits from iostream. Think of stringstream as a .NET MemoryBuffer object. Pass stringstream to your unmanaged C++. That can probably be done in < 10 lines of code. The downside is the data will get copied in memory which is inefficient. For many applications I doubt it would be an issue.

Alternatively, you could write your own class inheriting from stream_buffer that wraps a .NET stream object. (Better to inherit from this instead of iostream, as others propose). That would be the most efficient way because no memory would be needlessly copied but I wouldn't bother doing it if the first method is fast enough.

like image 162
James Johnston Avatar answered Sep 20 '22 08:09

James Johnston