CStringList to LPTSTR[]
-
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
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] -
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
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] -
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]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(); } */
-
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] -
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] -
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] -
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(); } */
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] -
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(); } */
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] -
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?
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] -
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]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]