0

I have an application in Visual Studio 2015 that receives a certain struct, as a memory pointer, with some fields from an application in LabWindows CVI that writes in a socket. Then I recast the memory into the same struct in my application. Code looks like:

DT_ULLong data;
wait_msg(...,...,&data);
XC_GEN_MSG* gen_msg = (XC_GEN_MSG*)data;
float var = gen_msg->varFloat;
//var has trash values
...

Structures:

typedef struct _XC_GEN_MSG
{
XC_SERVER_MSG_HEADER   Header;   
DT_ULong               data; //unsigned long long int
} XC_GEN_MSG;


typedef struct
{
unsigned char word[8];
unsigned int num_image;
unsigned int num_pixels_az;
unsigned int num_pixels_dist;
unsigned int rawtime_ms;
unsigned int rawtime_time_t;
unsigned int PW;
unsigned int PRI;
unsigned int Range;
unsigned int PW_Digit;
unsigned int Integrated_pulses_AZ;
unsigned int Rotation_time;
unsigned int bytes_per_pixel;
unsigned int bytes_digit;
DT_UEncoder  UD_AZ_RESOLUTION; // typedef unsigned short DT_UEncoder;
float                       f_az_resolution;
unsigned int Status_bitfield;
float                      Lat;
float                      Long;
float                      wind_speed;
float                      wind_direction;
float                      speed;
float                      course;
unsigned int comand;
unsigned int valor_comand;
} XC_SERVER_MSG_HEADER;

All the fields are correctly casted and I can see the proper values, but the float fields (varFloat in this case) are always with trash (usually close to 0, but sometimes for example 2*e24). I guess this can be due to compilation problems or memory copy ones, but the interesting part is that it works in the 32 version of the application but not in the 64 version of it.

Where could be the problem and how can I solve it?

Thanks!

  • 3
    Probably because in 64 bits the layout of the structure in memory is different than the layout in 32 bits due to different alignement requirements and/or padding. Please show the `XC_GEN_MSG` structure, without that information it's hard to tell more. – Jabberwocky Mar 15 '18 at 09:52
  • Maybe it's just that a 32bit float and 64bit float doesn't have the same size. Are you sure just wildly casting the received data is the correct way ? – Tom's Mar 15 '18 at 09:54
  • ... and show also the definition of `DT_ULLong`. – Jabberwocky Mar 15 '18 at 10:01
  • You never read float, you read `DT_ULLong` instead. – user7860670 Mar 15 '18 at 10:11
  • @VTT not sure what you mean in your comment. – Jabberwocky Mar 15 '18 at 10:13
  • 2
    `XC_GEN_MSG gen_msg = (XC_GEN_MSG*)data;`: this is very strange, and this should not even compile. Is this your _actual_ code? Or is it rather `XC_GEN_MSG *gen_msg = (XC_GEN_MSG*)data;`? – Jabberwocky Mar 15 '18 at 10:16
  • 2
    You can use `#pragma pack` when defining the struct to tell the compiler to use a certain packing of the members so you can ensure compatibiliy with the 32 bit version. – Paul Ogilvie Mar 15 '18 at 10:24
  • do whatever you feel like @MichaelWalz . I forgot to copy the declaration, of course it is XC_GEN_MSG * gen_msg = (XC_GEN_MSG * )data; Also DT_ULLong is a typedef as: typedef unsigned long long int . Everyone is telling about the lack of code, but the problem is not "inside" the code as it is working, the problem comes in the packing or serialization of the float type when coming from a different compiler than visual studio's. – Adrián Arroyo Perez Mar 15 '18 at 11:23
  • @AdriánArroyoPerez please [edit] your question and put all relevant information _there_. And yes: _problem comes in the packing or serialization of the float type when coming from a different compiler than visual studio's_, this is probably the cause. BTW not necessarily the packing of the float type but the packing of the whole structure. If you provide the definition of `XC_GEN_MSG` we might be able to help more. – Jabberwocky Mar 15 '18 at 11:29
  • Edited, thanks for the help. I am trying to read wind_speed and wind_direction – Adrián Arroyo Perez Mar 15 '18 at 11:36
  • I could not note any alignement difference between 32 and 64 bit version for your structures. So the problem must be somewhere else in your code. I'm afraid I can't help more. – Jabberwocky Mar 15 '18 at 12:51
  • This doesn't address the question, but names that begin with an underscore followed by a capital letter (`_XC_GEN_MSG`) and names that contain two consecutive underscores are reserved for use by the implementation. Don't use them in your code. – Pete Becker Mar 15 '18 at 15:03
  • May I suggest that if you want to serialise data between two applications you use a real serialisation library (e.g. Protocol Buffers, Messagepack, etc) rather than just trying to read a struct directly from memory and hoping it works? – Sean Burton Mar 15 '18 at 16:32

0 Answers0