I have used CreateFileMapping and MapViewOfFile to map a file under Window using C++ VS2010.
The only way to get / read data from this mapped file that I know is to use MemCpy. However, I was hoping that there might be a faster / more direct way.
Is there one? If yes, could somebody please post a sample?
Thank you!
you may cast the memory block to a data struct, as a pointer,
struct someStruct* data = (struct someStruct*)memAddress;
then you can access the data as a pointer
somefuction (data->var1, data->var2);
or
sum = data->var1 + data->var2;
you will have to be carful, and sure that the mapping struct matches the memory block, or you gonna get some junk
something like this
struct msgStructureCommon { unsigned int messageID; unsigned int messageSize; char bufferLimit[1024-(2*sizeof(unsigned int))]; };
struct msgStructureID911
{
unsigned int messageID;
unsigned int messageSize;
//someData....
};
struct msgStructureID2013
{
unsigned int messageID;
unsigned int messageSize;
//someData....
};
// in main
char* buffer = receiveData(socket);
struct msgStructureCommon* msgRCV = (struct msgStructureCommon*)buffer;
std::cout<< msgRCV->messageID << std::endl;
if(msgRCV->messageID == 911)
{
struct msgStructureID911* msg911 = (struct msgStructureID911*)buffer;
if(msg911->messageSize >= MSG_LIMIT_SIZE_911)
{
std::cout<< msg911->someData<< std::endl;
}
else
{
std::cout<< "ops, message is not correct!" << std::endl;
}
}
else if (msgRCV->messageID == 2013)
{
//...
}