CString to LPCTSTR
-
How to convert CString to LPCTSTR. I use this function: BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); } But pFromFolder don't works with type cast (LPCTSTR)CString but it's work only with "C:\\directory\\subdirectory" !!! THANKS
-
How to convert CString to LPCTSTR. I use this function: BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); } But pFromFolder don't works with type cast (LPCTSTR)CString but it's work only with "C:\\directory\\subdirectory" !!! THANKS
GetBuffer and ReleaseBuffer. Christian Graus - Microsoft MVP - C++
-
GetBuffer and ReleaseBuffer. Christian Graus - Microsoft MVP - C++
Sorry but Getbuffer doesn't works with pFromFolder SHFileOperation return a error. WORK CopyFolder("C:\\directoy\\sub","D:\\directoy\\sub"); DON'T WORK CopyFolder(str.Getbuffer(size),"D:\\directoy\\sub"); CopyFolder((LPCTSTR)str,D:\\directoy\\sub"); BOOL CDialog::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); }
-
How to convert CString to LPCTSTR. I use this function: BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); } But pFromFolder don't works with type cast (LPCTSTR)CString but it's work only with "C:\\directory\\subdirectory" !!! THANKS
http://www.codeproject.com/string/cppstringguide2.asp?df=100&forumid=11477&exp=0&select=971864[^] Tom Archer - Visual C++ MVP Archer Consulting Group.com
-
http://www.codeproject.com/string/cppstringguide2.asp?df=100&forumid=11477&exp=0&select=971864[^] Tom Archer - Visual C++ MVP Archer Consulting Group.com
Yes i'll read this article but the conversion is good in DEBUG mode but the function (SHFileOperation) always returns an error!! Strange Thanks for help
-
How to convert CString to LPCTSTR. I use this function: BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder) { SHFILEOPSTRUCT fo = {0}; fo.wFunc = FO_COPY; fo.pFrom = pFromFolder; fo.pTo = pToFolder; fo.fFlags = FOF_SILENT | FOF_NOERRORUI; int rc = SHFileOperation(&fo); return (rc == 0); } But pFromFolder don't works with type cast (LPCTSTR)CString but it's work only with "C:\\directory\\subdirectory" !!! THANKS
jerome_data wrote: How to convert CString to LPCTSTR.
CString
already converts automatically to LPCTSTR (through a cast operator). You don't need to do anything special. jerome_data wrote: But pFromFolder don't works with type cast (LPCTSTR)CString What exactly do you mean? What's your code and what's the error? The following fragment should compile and run as expected:CString sFromFolder = "C:\\directory\\subdirectory";
CString sToFolder = "C:\\directory\\OtherSubdirectory";
if (!CDialogMaintenance::CopyFolder(sFromFolder, sToFolder))
{
// handle error;
}[UPDATE] If that doesn't work, maybe you have wrong definitions for Unicode/MBCS? You should have one of the following combinations: a) Unicode project: UNICODE defined, _UNICODE defined, _MBCS not defined b) MBCS project: UNICODE not defined, _UNICODE not defined, _MBCS defined c) No setting: UNICODE not defined, _UNICODE not defined, _MBCS not defined Any other combination may cause problems. [END UPDATE] That being said, the documentation for SHFILEOPSTRUCT[^] says the following about both
pFrom
andpTo
: "Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of [pFrom or pTo]" I suggest you modify CopyFolder to something like this:static LPCTSTR GetBufferWithExtraNull(CString s)
{
int len = s.GetLength();
// make sure the buffer has space for an additional char
LPCTSTR p = s.GetBuffer(len+1+1);
// append the required additional NULL
p[len+1] = '\0'
// note there's no ReleaseBuffer()
return p;
}
BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder)
{
&nbs -
jerome_data wrote: How to convert CString to LPCTSTR.
CString
already converts automatically to LPCTSTR (through a cast operator). You don't need to do anything special. jerome_data wrote: But pFromFolder don't works with type cast (LPCTSTR)CString What exactly do you mean? What's your code and what's the error? The following fragment should compile and run as expected:CString sFromFolder = "C:\\directory\\subdirectory";
CString sToFolder = "C:\\directory\\OtherSubdirectory";
if (!CDialogMaintenance::CopyFolder(sFromFolder, sToFolder))
{
// handle error;
}[UPDATE] If that doesn't work, maybe you have wrong definitions for Unicode/MBCS? You should have one of the following combinations: a) Unicode project: UNICODE defined, _UNICODE defined, _MBCS not defined b) MBCS project: UNICODE not defined, _UNICODE not defined, _MBCS defined c) No setting: UNICODE not defined, _UNICODE not defined, _MBCS not defined Any other combination may cause problems. [END UPDATE] That being said, the documentation for SHFILEOPSTRUCT[^] says the following about both
pFrom
andpTo
: "Although this member is declared as a null-terminated string, it is used as a buffer to hold multiple file names. Each file name must be terminated by a single NULL character. An additional NULL character must be appended to the end of the final name to indicate the end of [pFrom or pTo]" I suggest you modify CopyFolder to something like this:static LPCTSTR GetBufferWithExtraNull(CString s)
{
int len = s.GetLength();
// make sure the buffer has space for an additional char
LPCTSTR p = s.GetBuffer(len+1+1);
// append the required additional NULL
p[len+1] = '\0'
// note there's no ReleaseBuffer()
return p;
}
BOOL CDialogMaintenance::CopyFolder(LPCTSTR pFromFolder, LPCTSTR pToFolder)
{
&nbsJose Lamas Rios wrote: An additional NULL character must be appended to the end of the final name I know for a fact that this is the problem. Without double terminating the string unpredictable results will occur... John
-
Jose Lamas Rios wrote: An additional NULL character must be appended to the end of the final name I know for a fact that this is the problem. Without double terminating the string unpredictable results will occur... John
John M. Drescher wrote: I know for a fact that this is the problem. Without double terminating the string unpredictable results will occur... I've been bit by this more than once.
"One must learn from the bite of the fire to leave it alone." - Native American Proverb