Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to CreateDirectory, getting char* to LPCWSTR error, willing to try another function

I've tried Googling this, and there are so many answers based on various specific situations that frankly I'm more stuck than I was when I started.

The facts are these:

  • Language: C/C++
  • OS: Windows
  • IDE: Visual Studio 2005
  • I'm trying to create a directory from a function in my program, using CreateDirectory (after a #include of windows.h).
  • Supposedly, the first parameter (a path) should be a char*. However, when I try to compile, I get the following error: error C2664: 'CreateDirectoryW' : cannot convert parameter 1 from 'char *' to 'LPCWSTR'
  • What I've read is that I have some sort of issue between UNICODE and ANSI. The solutions vary wildly and I'm afraid of breaking something important, or doing something very stupid.
  • I am perfectly willing to use any other method of creating a new directory, if one exists without me having to find some other library.
  • I only minored in comp sci, and frankly I have no idea why it's so easy to open, close, edit, and otherwise access files through stdio, but doing anything with directories (specifically making them and finding out if they exist) is a wild goose chase through the streets of the Internet.

Please help me, either to fix the current attempt at CreateDirectory or to use something else to create a directory.

Thank you!

like image 245
Rae Avatar asked Aug 10 '11 18:08

Rae


2 Answers

This is completely crazy. Microsoft have mechanisms to support both ANSI (as they call it) and UNICODE from the same code, because Windows 95/8/Me were ANSI operating systems and NT/XP/Vista etc are UNICODE operating systems. So if you really want to you can write one set of code that supports both systems and just recompile for each system.

But who is interested in Windows 95/98/Me any more? Yet this stuff carries on confusing newbies.

Here's the beef, there is no function called CreateDirectory in Windows, there a UNICODE function called CreateDirectoryW and an ANSI function called CreateDirectoryA. The macro CreateDirectory is converted to CreateDirectoryW or CreateDirectoryA depending on which compiler options you have defined. If you end up using CreateDirectoryW (as you evidentally did) the you must pass a Unicode string to it, if you use CreateDirectoryA then you pass a normal ASCII string.

The simplest thing in your case would be to forget about all this and just call CreateDirectoryA directly.

CreateDirectoryA("c:\\some\\directory", NULL);

Why it is so hard to create a directory in C++? I would guess that because back in the 70s when C was new, not every operating system had directories so this stuff never made it into the language standard.

like image 193
john Avatar answered Oct 06 '22 21:10

john


VS2005 is Unicode by default, and you should better keep it that way. Might save you a lot of issues in the future. In Unicode builds CreateDirectory (and other Win32 functions) expect wchar_t strings, and not regular char. Making string literals wchar_t's is simple -

L"Some string" to make it always Unicode, or _T("Some string") to make it configuration dependent.

I don't know how exactly are you calling CreateDirectory, but if converting the string to Unicode is too much trouble, you can use the ANSI version directly - CreateDirectoryA. Post some code if you want a more detailed answer.

like image 28
eran Avatar answered Oct 06 '22 22:10

eran