0

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?

5YrsLaterDBA
  • 33,370
  • 43
  • 136
  • 210

1 Answers1

3

You are passing a const char* to a function expecting a TCHAR*.

TCHAR is defined as either char or wchar_t depending on the compilation settings - and by default in VC2008 it is wchar_t. Your use of std::string assumes that TCHAR is char, which causes the error you see.

There are two reasonable fixes available to you:

  • In your project settings, change Configuration Properties/General/Character Set to Use Multi-Byte Character Set.

Or

  • Refactor your code to account for the potentially different definitions of TCHAR - you'd start this by replacing any use of std::string or std::wstring with std::basic_string<TCHAR> (using an appropriate typedef) and wrap string literals in the _T or _TEXT macro.
JoeG
  • 12,994
  • 1
  • 38
  • 63