lundi 28 janvier 2019

Howto read out SharedMemory Mapped File as different datatypes on c++ side

@First of all I´m sorry if the code formatting is not working, it´s my first attempt...

I´m setting up a interprocess communication between a C# WPF application and a C++ DLL (which is used by a 3rd party application). The communication shall be used to communicate several different basic datatypes (bool, (u)integer, float, double, string). Therefor I planned to use the MemoryMappedFile as Field of unspecified Data with a size that fits my amount of data. I read in the configuration on both side using the same ConfigFile and then store my byte offsets from on to the other variable. e.g

//bool1 => memory[0]
//int1 => memory[1]
//float1 => memory[5]
//double1 => memory[9]
//int2 => memory[17] and so on...

But I´m not able to re-interpret the values on the c++ side without getting exceptions.

The SharedMemory communication itself works properly. I sent data from each side to the other. And If I fill the byte[] on the c# sid with 1 in each field, I can print out the whole buffer on the c++ side as characters and I see a SOH (ascii 0x01) for each field.

// read the MapViewOfFile ... works
auto buffer = (char*) MapViewOfFile(this->shm_handle, 
FILE_MAP_READ, 0, 0, this->shm_field_size);


//interpret as std::string and print out 
//makes a bunch of SOH SOH... in my logfile
std::string incomingPayload(buffer);
_HEILOG(incomingPayload);


//this line throws an exception (I´m not able to catch it)
int tmp_int = *reinterpret_cast<int*>(buffer, 0);
_HEILOG("field as int" << tmp_int);

// this block throws also an unknown exception...
void* v = (void*)buffer[0];int* ip = (int*)v;
int testi = *ip; 
_HEILOG("field as int2" << testi );

I expected both tries to work, as I expected to be able to navigate through the memory field by buffer[offset] (e.g buffer[0], buffer[1], buffer[5]... for above example).

And then do a reinterpretation of the upcoming bytes to read the data as bool, int, float or whatever. But my 3rd Party application throws unknown exceptions and my Logfile has no entries.

I´m thankfull for each hint.

Best Regards SU52

Aucun commentaire:

Enregistrer un commentaire