1 Replies - 9975 Views - Last Post: 24 December 2012 - 04:13 PM

#1 Sublight  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 41
  • Joined: 11-July 08

memcpy equivalent in Managed C++

Posted 13 December 2012 - 04:40 PM

I'm attempting to bring some old TCP/IP related code into a new Visual C++ .Net application, but am having trouble finding a substitute for good old memcpy.

Here's a silly example of what I've been doing in unamanged c++:

struct birthday
{
    unsigned int year : 13;
    unsigned short month : 5;
    unsigned short day : 6;
};

int main (void) 
{
    birthday bDay, bDay2;
    bDay.year = 2012;
    bDay.month = 12;
    bDay.day = 12;

    char* packetA[4];
    int packetB;
    memcpy(packetA, &bDay, sizeof(bDay));
    memcpy(&packetB, &bDay, sizeof(bDay));
    memcpy(&bDay2, packetA, sizeof(bDay2));

    ...
}


So far the only practical solution my google skills have found is to declare blocks of code 'unsafe' as necessarily to continue using memcpy. Does anyone know of a better way?

This post has been edited by Sublight: 13 December 2012 - 04:41 PM


Is This A Good Question/Topic? 0
  • +

Replies To: memcpy equivalent in Managed C++

#2 miltonneal  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 7
  • Joined: 09-November 12

Re: memcpy equivalent in Managed C++

Posted 24 December 2012 - 04:13 PM

You could try using the Marshal Class.
Its in the System::Runtime::InteropServices.
You might find something here, depends on what you want to do. Failing that I would tend to re-write the code using managed methods.
Milton
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1