How to copy bytes from buffer into the managed struct?

Posted by Chupo_cro on Stack Overflow See other posts from Stack Overflow or by Chupo_cro
Published on 2012-10-25T03:40:39Z Indexed on 2012/10/25 5:01 UTC
Read the original article Hit count: 149

Filed under:
|
|
|

I have a problem with getting the code to work in a managed environment (VS2008 C++/CLI Win Forms App). The problem is I cannot declare the unmanaged struct (is that even possible?) inside the managed code, so I've declared a managed struct but now I have a problem how to copy bytes from buffer into that struct. Here is the pure C++ code that obviously works as expected:

typedef struct GPS_point {
  float point_unknown_1;
  float latitude;
  float longitude;
  float altitude;            // x10000
  float time;
  int point_unknown_2;
  int speed;                 // x100
  int manually_logged_point; // flag (1 --> point logged manually)
  } track_point;

int offset           = 0;
int filesize         = 256;  // simulates filesize
int point_num        = 10;   // simulates number of records

int main () {

char *buffer_dyn = new char[filesize];  // allocate RAM
// here, the file would have been read into the buffer
buffer_dyn[0xa8] = 0x1e;  // simulates the speed data (1e 00 00 00)
buffer_dyn[0xa9] = 0x00;
buffer_dyn[0xaa] = 0x00;
buffer_dyn[0xab] = 0x00;

offset = 0x90;  // if the data with this offset is transfered trom buffer
                // to struct, int speed is alligned with the buffer at the
                // offset of 0xa8

track_point *points = new track_point[point_num];
points[0].speed    = 0xff;  // (debug) it should change into 0x1e
memcpy(&points[0],buffer_dyn+offset,32);

cout << "offset: " << offset << "\r\n";
//cout << "speed: " << points[0].speed << "\r\n";
printf ("speed : 0x%x\r\n",points[0].speed);
printf("byte at offset 0xa8: 0x%x\r\n",(unsigned char)buffer_dyn[0xa8]);  // should be 0x1e

delete[] buffer_dyn;  // release RAM
delete[] points;

/*
What I need is to rewrite the lines 29 and 31 to work in the managed code (VS2008 Win Forms C++/CLI)
What should I have after:

array<track_point^>^ points = gcnew array<track_point^>(point_num);

so I can copy 32 bytes from buffer_dyn to the managed struct declared as

typedef ref struct GPS_point {
  float point_unknown_1;
  float latitude;
  float longitude;
  float altitude;             // x10000
  float time;
  int point_unknown_2;
  int speed;                  // x100
  int manually_logged_point;  // flag (1 --> point logged manually)
  } track_point;
*/

return 0;
}

Here is the paste to codepad.org so it can be seen the code is OK.

What I need is to rewrite these two lines:

track_point *points = new track_point[point_num];
memcpy(&points[0],buffer_dyn+offset,32);

to something that will work in a managed application. I wrote:

array<track_point^>^ points = gcnew array<track_point^>(point_num);

and now trying to reproduce the described copying of the data from buffer over the struct, but haven't any idea how it should be done.

Alternatively, if there is a way to use an unmanaged struct in the same way shown in my code, then I would like to avoid working with managed struct.

© Stack Overflow or respective owner

Related posts about .NET

Related posts about visual-c++