Help! CString convert to LPBYTE ?
-
: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; }
-
: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; }
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
-
: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; }
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
-
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
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 theCString
argument'sLPCTSTR
conversion operator is guaranteed to create one.
Software Zen:
delete this;
-
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
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!