I am creating a VC++2008 Windows Form Application which needs to use some of classes from our VC6 project.
When I added one file which contains the following method:
bool Property::createPaths(string &sPaths)
{
char *tok = NULL;
char seps[] = "\\";
string str;
if (sPaths.size() > 0)
{
tok = strtok((char*)sPaths.c_str(),seps);
str = tok;
while (tok != NULL)
{
int res = CreateDirectory(str.c_str(),NULL);
tok = strtok(NULL,seps);
if (tok != NULL)
{
str += "\\";
str += tok;
}
}
return true;
}
return false;
}
I got error complain the CreateDirectory call:
*error C2664: ‘CreateDirectory’ : cannot convert parameter 1 from ‘const char ‘ to ‘LPCTSTR’
Searched online, seems I need some configuration on my VC2008 project to fix this. Anybody can tell me where and how?
You are passing a
const char*to a function expecting aTCHAR*.TCHARis defined as eithercharorwchar_tdepending on the compilation settings – and by default in VC2008 it iswchar_t. Your use ofstd::stringassumes thatTCHARischar, which causes the error you see.There are two reasonable fixes available to you:
Configuration Properties/General/Character SettoUse Multi-Byte Character Set.Or
TCHAR– you’d start this by replacing any use ofstd::stringorstd::wstringwithstd::basic_string<TCHAR>(using an appropriate typedef) and wrap string literals in the_Tor_TEXTmacro.