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. Help! CString convert to LPBYTE ?

Help! CString convert to LPBYTE ?

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
6 Posts 5 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.
  • A Offline
    A Offline
    alias0018
    wrote on last edited by
    #1

    :confused: //Hi all,Thank you for helping me! LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb=new BYTE[str.GetLength()+1]; for(int i=0; ibr>lpb[str.GetLength()]=0; //???? //How can I code this line? return lpb; }

    T D M 3 Replies Last reply
    0
    • A alias0018

      :confused: //Hi all,Thank you for helping me! LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb=new BYTE[str.GetLength()+1]; for(int i=0; ibr>lpb[str.GetLength()]=0; //???? //How can I code this line? return lpb; }

      T Offline
      T Offline
      try88
      wrote on last edited by
      #2

      try this function :str.GetBuffer(0) 路漫漫其修远兮,吾将上下而求索。

      1 Reply Last reply
      0
      • A alias0018

        :confused: //Hi all,Thank you for helping me! LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb=new BYTE[str.GetLength()+1]; for(int i=0; ibr>lpb[str.GetLength()]=0; //???? //How can I code this line? return lpb; }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #3

        Try:

        for (int i = 0; i < str.GetLength(); i++
        lpb[i] = str[i];

        lpb[i] = '\0';

        or:

        _tcscpy(lpb, str);


        "One must learn from the bite of the fire to leave it alone." - Native American Proverb

        1 Reply Last reply
        0
        • A alias0018

          :confused: //Hi all,Thank you for helping me! LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb=new BYTE[str.GetLength()+1]; for(int i=0; ibr>lpb[str.GetLength()]=0; //???? //How can I code this line? return lpb; }

          M Offline
          M Offline
          MailtoGops
          wrote on last edited by
          #4

          Hi, Do not use _tcscpy() function, because CString is a class, it doesn't mean it is allocating one extra byte for '\0' NULL character to say end of string. And also avoid lengthly for loop while causes poor performance.. Use this method.. LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb= NULL; int nLength = str.GetLength(); if (nLength > 0) { // If you need space for NULL allocate one more. otherwise don't lpb = new BYTE[(nLength + 1) * sizeof(TCHAR)]; memcpy(lpb,str.GetBuffer(0),(nLength) * sizeof(TCHAR)); lpb[nLength * sizeof(TCHAR)] = 0; // only if you need NULL at end } return lpb; } " Action without vision is only passing time, Vision without action is merely day dreaming, But vision with action can change the world " - Words from Nelson Mandela Thanks & Regards, Gopalakrishnan

          G A 2 Replies Last reply
          0
          • M MailtoGops

            Hi, Do not use _tcscpy() function, because CString is a class, it doesn't mean it is allocating one extra byte for '\0' NULL character to say end of string. And also avoid lengthly for loop while causes poor performance.. Use this method.. LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb= NULL; int nLength = str.GetLength(); if (nLength > 0) { // If you need space for NULL allocate one more. otherwise don't lpb = new BYTE[(nLength + 1) * sizeof(TCHAR)]; memcpy(lpb,str.GetBuffer(0),(nLength) * sizeof(TCHAR)); lpb[nLength * sizeof(TCHAR)] = 0; // only if you need NULL at end } return lpb; } " Action without vision is only passing time, Vision without action is merely day dreaming, But vision with action can change the world " - Words from Nelson Mandela Thanks & Regards, Gopalakrishnan

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            S.Gopalakrishnan wrote: _tcscpy() function, because CString is a class, it doesn't mean it is allocating one extra byte for '\0' NULL _tcscpy does copy the terminator, since the CString argument's LPCTSTR conversion operator is guaranteed to create one.


            Software Zen: delete this;

            1 Reply Last reply
            0
            • M MailtoGops

              Hi, Do not use _tcscpy() function, because CString is a class, it doesn't mean it is allocating one extra byte for '\0' NULL character to say end of string. And also avoid lengthly for loop while causes poor performance.. Use this method.. LPBYTE CString_To_LPBYTE(CString str) { LPBYTE lpb= NULL; int nLength = str.GetLength(); if (nLength > 0) { // If you need space for NULL allocate one more. otherwise don't lpb = new BYTE[(nLength + 1) * sizeof(TCHAR)]; memcpy(lpb,str.GetBuffer(0),(nLength) * sizeof(TCHAR)); lpb[nLength * sizeof(TCHAR)] = 0; // only if you need NULL at end } return lpb; } " Action without vision is only passing time, Vision without action is merely day dreaming, But vision with action can change the world " - Words from Nelson Mandela Thanks & Regards, Gopalakrishnan

              A Offline
              A Offline
              alias0018
              wrote on last edited by
              #6

              Thank you so much everyone! but there is still problems! I used Mr S.Gopalakrishnan's method but there is Linking error: System.obj : error LNK2001: unresolved external symbol "public: unsigned char * __thiscall System::CString_To_LPBYTE(class CString)" (?CString_To_LPBYTE@System@@QAEPAEVCString@@@Z) Debug/ex_ShutdownWindows.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. How can I resove it? Thank you!

              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