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. CStringList to LPTSTR[]

CStringList to LPTSTR[]

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structures
11 Posts 3 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
    azusakt
    wrote on last edited by
    #1

    Hi , I have a CStringList that contains some filepaths, these paths need to be passed into a function: eg: GetNewXML(LPTSTR PreviewFileArray[],int count) How can I construct a LPTSTR array from CStringList? thanks

    C C 2 Replies Last reply
    0
    • A azusakt

      Hi , I have a CStringList that contains some filepaths, these paths need to be passed into a function: eg: GetNewXML(LPTSTR PreviewFileArray[],int count) How can I construct a LPTSTR array from CStringList? thanks

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Are you sure that you really need LPTSTR (don't you need LPCTSTR)? As stated, you probably have to copy all the strings.

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      [my articles]

      A 1 Reply Last reply
      0
      • A azusakt

        Hi , I have a CStringList that contains some filepaths, these paths need to be passed into a function: eg: GetNewXML(LPTSTR PreviewFileArray[],int count) How can I construct a LPTSTR array from CStringList? thanks

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Or another suggestion (if you can change the function signature): just pass the string array as a constant reference instead. void GetNewXML(const CStringList& myList); You wrote the GetNewXML function ?

        Cédric Moonen Software developer
        Charting control [v1.2]

        A 1 Reply Last reply
        0
        • C Cedric Moonen

          Or another suggestion (if you can change the function signature): just pass the string array as a constant reference instead. void GetNewXML(const CStringList& myList); You wrote the GetNewXML function ?

          Cédric Moonen Software developer
          Charting control [v1.2]

          A Offline
          A Offline
          azusakt
          wrote on last edited by
          #4

          sorry that I can't change the interface for function "GetNewXML", coz it has been written by pervious staff. so I trying to do, but not work /* LPTSTR[] files = new LPTSTR[count]; for(int i=0; i< count ; i++) { files[i] = bmpList.GetAt(i).GetBuffer(); } */

          C C 2 Replies Last reply
          0
          • C CPallini

            Are you sure that you really need LPTSTR (don't you need LPCTSTR)? As stated, you probably have to copy all the strings.

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            [my articles]

            A Offline
            A Offline
            azusakt
            wrote on last edited by
            #5

            sorry, what do you mean "copy all the strings. "? can you provide me a code? thanks

            C 1 Reply Last reply
            0
            • A azusakt

              sorry, what do you mean "copy all the strings. "? can you provide me a code? thanks

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Yes the function prototype implies such copy. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              [my articles]

              A 1 Reply Last reply
              0
              • C CPallini

                Yes the function prototype implies such copy. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                [my articles]

                A Offline
                A Offline
                azusakt
                wrote on last edited by
                #7

                however, I can't initialize the array like this: LPTSTR[] files = new LPTSTR[count]; do you mean copy character by: LPTSTR a = list.GetAt(0).CopyChars?

                C 1 Reply Last reply
                0
                • A azusakt

                  sorry that I can't change the interface for function "GetNewXML", coz it has been written by pervious staff. so I trying to do, but not work /* LPTSTR[] files = new LPTSTR[count]; for(int i=0; i< count ; i++) { files[i] = bmpList.GetAt(i).GetBuffer(); } */

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Well, in principle, you can do the really weird thing you suggested:

                  LPTSTR* files = new LPTSTR[count];

                  for(int i=0; i< count ; i++)
                  {
                  files[i] = bmpList.GetAt(i).GetBuffer();
                  }
                  GetNewXML(files, count);
                  for(int i=0; i< count ; i++)
                  {
                  files[i] = bmpList.GetAt(i).ReleaseBuffer();
                  }
                  delete [] files;

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  [my articles]

                  1 Reply Last reply
                  0
                  • A azusakt

                    sorry that I can't change the interface for function "GetNewXML", coz it has been written by pervious staff. so I trying to do, but not work /* LPTSTR[] files = new LPTSTR[count]; for(int i=0; i< count ; i++) { files[i] = bmpList.GetAt(i).GetBuffer(); } */

                    C Offline
                    C Offline
                    Cedric Moonen
                    wrote on last edited by
                    #9

                    I think you didn't really understand what the function accept: the function doesn't accept an array of strings, but one single string (and the count argument is the size of this string). So, you need to pass each string separately. Basically, what you'll need to do is loop through your string array, create a LPTSTR array of the size of the string and pass it to your function, then delete it. That's completely stupid because if your previous team would have been smart enough so that they passed the string as a constant (LPCTSTR instead of a LPTSTR), then you wouldn't have to create a new string buffer, copy the string in it, pass it to the function and delete it. You would just have to pass the CString object (and it would have been automatically converted). Oops, I forgot the [] after the first argument :~. So, forget what I said...

                    Cédric Moonen Software developer
                    Charting control [v1.2]

                    C 1 Reply Last reply
                    0
                    • A azusakt

                      however, I can't initialize the array like this: LPTSTR[] files = new LPTSTR[count]; do you mean copy character by: LPTSTR a = list.GetAt(0).CopyChars?

                      C Offline
                      C Offline
                      CPallini
                      wrote on last edited by
                      #10

                      azusakt wrote:

                      LPTSTR[] files = new LPTSTR[count];

                      change to

                      LPTSTR* files = new LPTSTR[count];

                      :)

                      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                      [my articles]

                      1 Reply Last reply
                      0
                      • C Cedric Moonen

                        I think you didn't really understand what the function accept: the function doesn't accept an array of strings, but one single string (and the count argument is the size of this string). So, you need to pass each string separately. Basically, what you'll need to do is loop through your string array, create a LPTSTR array of the size of the string and pass it to your function, then delete it. That's completely stupid because if your previous team would have been smart enough so that they passed the string as a constant (LPCTSTR instead of a LPTSTR), then you wouldn't have to create a new string buffer, copy the string in it, pass it to the function and delete it. You would just have to pass the CString object (and it would have been automatically converted). Oops, I forgot the [] after the first argument :~. So, forget what I said...

                        Cédric Moonen Software developer
                        Charting control [v1.2]

                        C Offline
                        C Offline
                        CPallini
                        wrote on last edited by
                        #11

                        What a mess a pair of brackets can do... :-D

                        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                        [my articles]

                        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