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. C / C++ / MFC
  4. How to vopy data pointed by a "Void* " pointer to "CString" object

How to vopy data pointed by a "Void* " pointer to "CString" object

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
4 Posts 4 Posters 0 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.
  • V Offline
    V Offline
    VCSharp007
    wrote on last edited by
    #1

    Hi, I have to copy the data pointed by "void*" to a CString object. Which is the best way? code: void* pVoid = 0; CString csData; pVoid = new BYTE(100); // Do some thing // ??? (Here I have to copy data from void* to CString.) delete[] pVoid; AfxMessageBox( csData ); Thnx in advance.

    W A J 3 Replies Last reply
    0
    • V VCSharp007

      Hi, I have to copy the data pointed by "void*" to a CString object. Which is the best way? code: void* pVoid = 0; CString csData; pVoid = new BYTE(100); // Do some thing // ??? (Here I have to copy data from void* to CString.) delete[] pVoid; AfxMessageBox( csData ); Thnx in advance.

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      Use a typecast i.e. csData = (char*)pVoid; You should probably validate the data in pVoid first, not sure how CString would respond if the data wasn't NULL terminated.

      1 Reply Last reply
      0
      • V VCSharp007

        Hi, I have to copy the data pointed by "void*" to a CString object. Which is the best way? code: void* pVoid = 0; CString csData; pVoid = new BYTE(100); // Do some thing // ??? (Here I have to copy data from void* to CString.) delete[] pVoid; AfxMessageBox( csData ); Thnx in advance.

        A Offline
        A Offline
        ajisthekingofpop
        wrote on last edited by
        #3

        void* pVoid = 0; CString csData; pVoid = new BYTE(100); I would do the following: memcpy(&csData, pVoid, 100); //I would use a #define for the 100 tho because it make sit easier to understand Now this has copied the data from one object to another WITHOUT using a reference.

        1 Reply Last reply
        0
        • V VCSharp007

          Hi, I have to copy the data pointed by "void*" to a CString object. Which is the best way? code: void* pVoid = 0; CString csData; pVoid = new BYTE(100); // Do some thing // ??? (Here I have to copy data from void* to CString.) delete[] pVoid; AfxMessageBox( csData ); Thnx in advance.

          J Offline
          J Offline
          Joe Woodbury
          wrote on last edited by
          #4

          DO NOT USE THE IMMEDIATE ABOVE SUGGESTION; it will result in a buffer overrun and is VERY poor coding practice. Two general methods: 1) Cast the void pointer to a char, tchar or wchar_t pointer. 2) Call CString::GetBuffer() with the length of buffer you require. Copy the data in and then call CString::ReleaseBuffer(). You will want to to the latter if your buffer contains multiple zero terminated strings since method one will terminate the copy at the first null.

          Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

          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