Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'CreateDirectoryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR' in OpenCV 2.4.5 and VS 2010

I was trying the sample code bagofwords_classification.cpp from openCV 2.4.5 to Visual Studio 2010 (VC++ based). But I found the error code :

error C2664: 'CreateDirectoryW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'   

Can you help me to give me the solution about that problem? Thanks. :)

Update v1:

static void makeDir( const string& dir )
{
#if defined WIN32 || defined _WIN32
    CreateDirectory( dir.c_str(), 0 );
#else
    mkdir( dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH );
#endif
}

static void makeUsedDirs( const string& rootPath )
{
    makeDir(rootPath + bowImageDescriptorsDir);
    makeDir(rootPath + svmsDir);
    makeDir(rootPath + plotsDir);
}
like image 845
Dominikus Willy Avatar asked Dec 12 '22 15:12

Dominikus Willy


2 Answers

You have code that calls CreateDirectory. When UNICODE is defined, that symbol is actually a macro for CreateDirectoryW; the intention is for you to use "ambiguous" function names when you're also using TCHAR instead of char or wchar_t, so you can switch between compiling for Unicode or Ansi programs.

However, std::string doesn't change according to UNICODE; it's always Ansi, so its c_str method always returns a char*, never wchar_t*. When you have a parameter that's always Ansi, you should explicitly call functions that are always Ansi, too. In this case, call CreateDirectoryA.

You could also consider using std::basic_string<TCHAR>, but that's probably heading in a direction you don't wish to go.

A quick fix would be to adjust your project settings so that UNICODE is no longer defined. Consult the documentation for your tool set to find out how to do that, or explore your IDE's project options.

like image 106
Rob Kennedy Avatar answered Dec 28 '22 07:12

Rob Kennedy


CreateDirectory will be defined as CreateDirectoryW which expects its parameters to be "wide" strings (UTF-16 encoded WCHAR*).

To create a wide string you can prepend L to a regular string.

CreateDirectory(L"mydir", NULL);

Alternatively, you can switch your project to multibyte encoding in the properties. This will mean that calling CreateDirectory will automatically use the CreateDirectoryA version of the function which accepts char* strings. These are expected to be in the multibyte encoding of the active codepage.

like image 29
user2093113 Avatar answered Dec 28 '22 07:12

user2093113