I am collecting data between a C # and C ++ application. In a C # application, I force the string size to be some size (say 256 bytes). I would like to read the same amount in C ++ (I will recreate the structures with reinterpret_cast) so that the data remains in the format as it was in the C # application. Unfortunately, I'm pretty rusty with C ++, and I'm not sure how to force the size of a string in a C ++ structure.
As requested, example. I have a C # structure that looks like this:
[StructLayout(LayoutKind.Sequential)] public struct DataLocater { [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] public string filename; [MarshalAs(UnmanagedType.I4)] public Int32 sizeOfData; public Int32 startLocation; public Int32 encrypted; }
I collect several files (along with other data) into a data file. Then the C ++ file reads this file, and I will parse it back into a C ++ struct with the same structure. My first attempt at this structure looked like this:
struct DataLocater { std::string filename; int sizeOfData; int startLocation; int encrypted; };
But the compiler does not know that I want std :: string to be 256 bytes.
edit: add a complete header file, e.g.
#pragma once #include "CoreArea/Singleton.h" class FileReader : public Singleton<FileReader> { friend class Singleton<FileReader>; public: void* GetFileData(std::wstring fileName, int &size); private: FileReader(); ~FileReader(); struct Header { std::string somestring; int numOfFiles; }; struct DataLocater { char[256] filename; int sizeOfData; int startLocation; int encrypted; }; };
Chris source share