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. type cast a CString to a LPTSTR

type cast a CString to a LPTSTR

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
14 Posts 3 Posters 2 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.
  • J Offline
    J Offline
    Jose Cruz
    wrote on last edited by
    #1

    How can I type cast a CString to a LPTSTR to avoid this error: 'type cast' : cannot convert from 'char *(__thiscall CString::*)(int)' to 'char *':confused:

    O 1 Reply Last reply
    0
    • J Jose Cruz

      How can I type cast a CString to a LPTSTR to avoid this error: 'type cast' : cannot convert from 'char *(__thiscall CString::*)(int)' to 'char *':confused:

      O Offline
      O Offline
      Oscar Londono
      wrote on last edited by
      #2

      Try CString::GetBuffer.;) Oscar L.

      J 2 Replies Last reply
      0
      • O Oscar Londono

        Try CString::GetBuffer.;) Oscar L.

        J Offline
        J Offline
        Jose Cruz
        wrote on last edited by
        #3

        I did but I get a type cast error too.

        1 Reply Last reply
        0
        • O Oscar Londono

          Try CString::GetBuffer.;) Oscar L.

          J Offline
          J Offline
          Jose Cruz
          wrote on last edited by
          #4

          Code problem: This is the structure of rvi: typedef struct _RVITEM { UINT nMask; INT iItem; INT iSubItem; LPTSTR lpszText; INT iTextMax; INT iTextColor; INT iImage; INT iCheck; INT iBkColor; UINT nPreview; INT iIndent; UINT nState; LPARAM lParam; INT iOverlay; _RVITEM() : nMask(0), iItem(RVI_INVALID), iSubItem(-1), lpszText(NULL), iTextMax(0), iTextColor(-1), iImage(-1), iCheck(-1), iBkColor(-1), nPreview(0), iIndent(-1), nState(0), lParam(0), iOverlay(0) {}; } RVITEM, FAR* LPRVITEM; CString strDato; //The followin returns a CString: pBdeDb->GetFieldAsString(1,&bIsBlank); strDato = pBdeDb->GetFieldAsString(1,&bIsBlank); //strncpy(strDato, Dato, strDato.GetLength()); rvi.iSubItem = 0; rvi.lpszText = strDato.GetBuffer;//<--------------Problem--------- rc.SetItem(&rvi);

          O 1 Reply Last reply
          0
          • J Jose Cruz

            Code problem: This is the structure of rvi: typedef struct _RVITEM { UINT nMask; INT iItem; INT iSubItem; LPTSTR lpszText; INT iTextMax; INT iTextColor; INT iImage; INT iCheck; INT iBkColor; UINT nPreview; INT iIndent; UINT nState; LPARAM lParam; INT iOverlay; _RVITEM() : nMask(0), iItem(RVI_INVALID), iSubItem(-1), lpszText(NULL), iTextMax(0), iTextColor(-1), iImage(-1), iCheck(-1), iBkColor(-1), nPreview(0), iIndent(-1), nState(0), lParam(0), iOverlay(0) {}; } RVITEM, FAR* LPRVITEM; CString strDato; //The followin returns a CString: pBdeDb->GetFieldAsString(1,&bIsBlank); strDato = pBdeDb->GetFieldAsString(1,&bIsBlank); //strncpy(strDato, Dato, strDato.GetLength()); rvi.iSubItem = 0; rvi.lpszText = strDato.GetBuffer;//<--------------Problem--------- rc.SetItem(&rvi);

            O Offline
            O Offline
            Oscar Londono
            wrote on last edited by
            #5

            Sorry. Use -> strDato.GetBuffer(0); But, remember that this buffer is temporary if strDato is local variable.!! To avoid this, create a buffer : rvi.lpszText = new char[strDato.GetLength()]; strcpy(rvi.lpszText, strDato); ;) Oscar L.

            J 1 Reply Last reply
            0
            • O Oscar Londono

              Sorry. Use -> strDato.GetBuffer(0); But, remember that this buffer is temporary if strDato is local variable.!! To avoid this, create a buffer : rvi.lpszText = new char[strDato.GetLength()]; strcpy(rvi.lpszText, strDato); ;) Oscar L.

              J Offline
              J Offline
              Jose Cruz
              wrote on last edited by
              #6

              Thank you very much.:-D

              C 1 Reply Last reply
              0
              • J Jose Cruz

                Thank you very much.:-D

                C Offline
                C Offline
                Carlos Antollini
                wrote on last edited by
                #7

                Te aconsejo usar strcpy(rvi.lpszText, strDato.GetBuffer(0)); Cheers!!! :) Carlos Antollini.

                O J 2 Replies Last reply
                0
                • C Carlos Antollini

                  Te aconsejo usar strcpy(rvi.lpszText, strDato.GetBuffer(0)); Cheers!!! :) Carlos Antollini.

                  O Offline
                  O Offline
                  Oscar Londono
                  wrote on last edited by
                  #8

                  Ese tambien funciona. La diferencia esta en que la primera devuelve un LPCTSTR (que es constante y es menos peligrosa) y la segunda devuelve un LPTSTR que se puede modificar directamente. Si deseas poder usar cast: strcpy(rvi.lpszText, (LPCTSTR) strDato); Oscar L.

                  J 1 Reply Last reply
                  0
                  • O Oscar Londono

                    Ese tambien funciona. La diferencia esta en que la primera devuelve un LPCTSTR (que es constante y es menos peligrosa) y la segunda devuelve un LPTSTR que se puede modificar directamente. Si deseas poder usar cast: strcpy(rvi.lpszText, (LPCTSTR) strDato); Oscar L.

                    J Offline
                    J Offline
                    Jose Cruz
                    wrote on last edited by
                    #9

                    Abusando de tu amabilidad: Podrias enseñarme como convierto de: double a LPTSTR. BOOL PTSTR. COleDateTime a LPTSTR. Long a LPTSTR. int a LPTSTR. :-O

                    O 1 Reply Last reply
                    0
                    • C Carlos Antollini

                      Te aconsejo usar strcpy(rvi.lpszText, strDato.GetBuffer(0)); Cheers!!! :) Carlos Antollini.

                      J Offline
                      J Offline
                      Jose Cruz
                      wrote on last edited by
                      #10

                      Gracias. Podrias echarme la mno con mi otra pregunta. de como convertir de los otros diferentes tipos de datos a LPTSTR:-O

                      C 2 Replies Last reply
                      0
                      • J Jose Cruz

                        Abusando de tu amabilidad: Podrias enseñarme como convierto de: double a LPTSTR. BOOL PTSTR. COleDateTime a LPTSTR. Long a LPTSTR. int a LPTSTR. :-O

                        O Offline
                        O Offline
                        Oscar Londono
                        wrote on last edited by
                        #11

                        Jose! Dale un vistazo a la documentación de CString::Format con la cual podes realizar las conversiones tipo sprintf. De igual forma COleDateTime tambien tiene el método COleDataTime::Format. Oscar L.

                        J 1 Reply Last reply
                        0
                        • O Oscar Londono

                          Jose! Dale un vistazo a la documentación de CString::Format con la cual podes realizar las conversiones tipo sprintf. De igual forma COleDateTime tambien tiene el método COleDataTime::Format. Oscar L.

                          J Offline
                          J Offline
                          Jose Cruz
                          wrote on last edited by
                          #12

                          y que tal de int, long y BOOL a LPTSTR

                          1 Reply Last reply
                          0
                          • J Jose Cruz

                            Gracias. Podrias echarme la mno con mi otra pregunta. de como convertir de los otros diferentes tipos de datos a LPTSTR:-O

                            C Offline
                            C Offline
                            Carlos Antollini
                            wrote on last edited by
                            #13

                            Usa wsprintf. LPTSTR str = NULL; int n = 1000; malloc((LPTSTR)str, 256); //or the size that you want wsprintf(str, "%d", n); //int -> LPTSTR Check the documentatión about wsprintf for other types. Carlos Antollini.

                            1 Reply Last reply
                            0
                            • J Jose Cruz

                              Gracias. Podrias echarme la mno con mi otra pregunta. de como convertir de los otros diferentes tipos de datos a LPTSTR:-O

                              C Offline
                              C Offline
                              Carlos Antollini
                              wrote on last edited by
                              #14

                              También tenes las funciones itoa() y ltoa() para convertir the int y long a Ascii luego lo convertis a LPCTSTR. El booleano es sencillo. BOOL bVAl = TRUE; LPTSTR str = bVal ? "TRUE": "FALSE"; Saludos :) Carlos Antollini.

                              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