std::vector<char> in C++/CLI?

Started by
3 comments, last by beebs1 11 years, 8 months ago
Hiya,

I have a C++ class with a public method like so:


typedef std::vector<char> FileBuffer;

void ArchiveWriter::write(const std::string& name, const FileBuffer& fileBuffer);



I need to use this from C#, so I've written a C++/CLI wrapper DLL around the class. I'm a little stuck on the method above, as I'm not sure how I can pass a byte buffer from C# and convert it to a vector<char>.

My understanding of C# isn't really up to scratch, but I believe a [font=courier new,courier,monospace]byte[][/font] type is similar to [font=courier new,courier,monospace]std::vector<char>[/font]?

Any help is appreciated!

Cheers.
Advertisement
Why? Are you allowed to change the interface? If so, I'd recommend using an iterator version of the code. Otherwise you'll have to copy the byte array into a std::vector.
Yep, I can change the interface.

I'm not very familiar with .NET, so I've just tried to match the interface as closely as possible when writing the CLI wrapper. I've been hunting around for a [font=courier new,courier,monospace]vector<char>[/font] equivalent, but if there's a more sensible alternative I'm all ears!

Thanks.
A few months ago I ported all the Kinect C# code to C++/CLI and it took months. I can't remember exactly, but know that a byte[] is equivalent to unsigned char(Unsigned 8-bit integer). I used regular buffer arrays for my work, I think they were pointers, so I can't help you with that vector signature.

You should be able to get away with a regular [ ] array. Perhaps a ^ handle pointer. Thing is I was porting from C# to C++ and you are doing the opposite, can't help you more than that =/.


I'm not 100%, but from memory, you might have to use a uchar * and point to the first block of memory and pin it in managed code to convert it.

[size=1]I "surf" the web, literally.

Aha, thanks. I'm note sure that there's actually any way to construct a std::vector<> using pre-allocated memory - so I'm going down the wrong path here. I'll change the interface to accept a pointer and size, and try your suggestion.

Thanks!

This topic is closed to new replies.

Advertisement