Rae
Rae

Reputation: 309

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:

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

Thank you!

Upvotes: 9

Views: 6571

Answers (3)

john
john

Reputation: 87932

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.

Upvotes: 12

Eran
Eran

Reputation: 22020

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.

Upvotes: 2

Andrew Lee
Andrew Lee

Reputation: 2603

Try using #undef UNICODE before including windows.h. Unless you are really coding Unicode, but that's another story.

With the new Visual Studio's, windows.h defaults to Unicode version, so #undef UNICODE will give you back the ANSI version. It will show in the stack trace -- you will call CreateDirectoryA rather than CreateDirectoryW.

Or, just call CreateDirectoryA directly, but somehow I feel that is not a best practice.

Upvotes: 1

Related Questions