Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Managed c++ how to memcpy a handle reference

Managed c++ how to memcpy a handle reference

Scheduled Pinned Locked Moved Managed C++/CLI
c++helptutorialquestion
7 Posts 4 Posters 43 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yehiga1467
    wrote on last edited by
    #1

    how do you copy the bytes of a managed variable to another managed variable? memcpy doesn't work since my arguments are not the correct type for memcpy

    #include "string.h" //Updated Solution
    float^ f = gcnew (float);
    unsigned int^ temp = gcnew(unsigned int);
    *temp = 0;
    *temp = _byte3;
    *temp = (*temp << 8U) + _byte2;
    *temp = (*temp << 8U) + _byte1;
    *temp = (*temp << 8U) + _byte0

    // memcpy(f, temp,4U); //error

    **//Updated solution :

    	pin\_ptr pinPtr\_FloatVal = &(\*f);
    
    
    	pin\_ptr pinPtr\_UintVal = &(\*temp);
    
    
    memcpy\_s(pinPtr\_FloatVal , 4U, pinPtr\_UintVal , 4U); //4U i.e. 4 bytes**
    
    V F 2 Replies Last reply
    0
    • Y yehiga1467

      how do you copy the bytes of a managed variable to another managed variable? memcpy doesn't work since my arguments are not the correct type for memcpy

      #include "string.h" //Updated Solution
      float^ f = gcnew (float);
      unsigned int^ temp = gcnew(unsigned int);
      *temp = 0;
      *temp = _byte3;
      *temp = (*temp << 8U) + _byte2;
      *temp = (*temp << 8U) + _byte1;
      *temp = (*temp << 8U) + _byte0

      // memcpy(f, temp,4U); //error

      **//Updated solution :

      	pin\_ptr pinPtr\_FloatVal = &(\*f);
      
      
      	pin\_ptr pinPtr\_UintVal = &(\*temp);
      
      
      memcpy\_s(pinPtr\_FloatVal , 4U, pinPtr\_UintVal , 4U); //4U i.e. 4 bytes**
      
      V Offline
      V Offline
      Victor Nijegorodov
      wrote on last edited by
      #2

      [c++ cli - Memcpy of native array to managed array in C++ CLI - Stack Overflow](https://stackoverflow.com/questions/6944288/memcpy-of-native-array-to-managed-array-in-c-cli)

      Y 1 Reply Last reply
      0
      • V Victor Nijegorodov

        [c++ cli - Memcpy of native array to managed array in C++ CLI - Stack Overflow](https://stackoverflow.com/questions/6944288/memcpy-of-native-array-to-managed-array-in-c-cli)

        Y Offline
        Y Offline
        yehiga1467
        wrote on last edited by
        #3

        Hello, I am trying to memcpy two managed values. The link provided seem to be for unmanaged to managed? Could you show an example how to achieve this? Thanks!

        V L 2 Replies Last reply
        0
        • Y yehiga1467

          Hello, I am trying to memcpy two managed values. The link provided seem to be for unmanaged to managed? Could you show an example how to achieve this? Thanks!

          V Offline
          V Offline
          Victor Nijegorodov
          wrote on last edited by
          #4

          Are you banned by Google? :confused: [memcpy in managed c /cli - Google Search](https://www.google.com/search?q=memcpy+in+managed+c%2B%2B%2Fcli&newwindow=1&rlz=1C1CHBF\_enDE886DE886&sxsrf=ALeKk033wPoCX2qWMAvCFyUkfnZmOYBonA%3A1622197366259&ei=dsSwYJ6vD6PAlAaA84PgAg&oq=memcpy+in+managed+c%2B%2B%2Fcli&gs\_lcp=Cgdnd3Mtd2l6EAMyCAghEBYQHRAeOgcIABBHELADOgcIABCwAxBDOgQIABBDOgIIADoHCAAQhwIQFDoGCAAQFhAeOggIABAWEAoQHjoFCCEQoAE6BwghEAoQoAFQkhJYn1xggF9oAXACeACAAZYBiAG6C5IBBDE4LjKYAQCgAQGqAQdnd3Mtd2l6yAEKwAEB&sclient=gws-wiz&ved=0ahUKEwiewpWOlOzwAhUjIMUKHYD5ACwQ4dUDCA4&uact=5)

          1 Reply Last reply
          0
          • Y yehiga1467

            Hello, I am trying to memcpy two managed values. The link provided seem to be for unmanaged to managed? Could you show an example how to achieve this? Thanks!

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            The link that Victor gave you shows how to get a pointer to a managed object in order to copy data into it. So all you need is two such pointers in order to copy managed to managed.

            Y 1 Reply Last reply
            0
            • L Lost User

              The link that Victor gave you shows how to get a pointer to a managed object in order to copy data into it. So all you need is two such pointers in order to copy managed to managed.

              Y Offline
              Y Offline
              yehiga1467
              wrote on last edited by
              #6

              Thank you very much Richard for elaborating a bit on this! I have solved my problem !

              1 Reply Last reply
              0
              • Y yehiga1467

                how do you copy the bytes of a managed variable to another managed variable? memcpy doesn't work since my arguments are not the correct type for memcpy

                #include "string.h" //Updated Solution
                float^ f = gcnew (float);
                unsigned int^ temp = gcnew(unsigned int);
                *temp = 0;
                *temp = _byte3;
                *temp = (*temp << 8U) + _byte2;
                *temp = (*temp << 8U) + _byte1;
                *temp = (*temp << 8U) + _byte0

                // memcpy(f, temp,4U); //error

                **//Updated solution :

                	pin\_ptr pinPtr\_FloatVal = &(\*f);
                
                
                	pin\_ptr pinPtr\_UintVal = &(\*temp);
                
                
                memcpy\_s(pinPtr\_FloatVal , 4U, pinPtr\_UintVal , 4U); //4U i.e. 4 bytes**
                
                F Offline
                F Offline
                Fly Gheorghe
                wrote on last edited by
                #7

                //Assuming int is on 4 bytes typedef union u { int i; byte b[4]; } U; U x; x.b[0] = 0 // Put first byte here x.b[1] = 0 // second byte here, etc. // Then use x.i as an integer // Keep in mind that on Intel CPUs, integer values are represented in memory in reverse order. // This means that hex value 0x11223344 will be represented in memory as 0x44, 0x33, 0x22, 0x11 // This is called little-endian vs. big-endian representation. // There can be CPUs that uses the other convention. // Make a small test project and play around with some values etc.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups