windows 尝试 CreateDirectory,获取 char* 到 LPCWSTR 错误,愿意尝试另一个功能

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7016206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:48:10  来源:igfitidea点击:

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

c++windowswinapicreate-directory

提问by Rae

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.
  • 语言:C/C++
  • 操作系统:Windows
  • IDE:Visual Studio 2005
  • 我正在尝试使用 CreateDirectory(在 windows.h 的 #include 之后)从我程序中的函数创建一个目录。
  • 据说,第一个参数(路径)应该是 char*。但是,当我尝试编译时,出现以下错误:错误 C2664: 'CreateDirectoryW' : 无法将参数 1 从 'char *' 转换为 'LPCWSTR'
  • 我读到的是,我在 UNICODE 和 ANSI 之间存在某种问题。解决方案千差万别,我害怕破坏一些重要的东西,或者做一些非常愚蠢的事情。
  • 我非常愿意使用任何其他方法来创建新目录,如果存在的话,我不必找到其他库。
  • 我只辅修了 comp sci,坦率地说,我不知道为什么通过 stdio 打开、关闭、编辑和以其他方式访问文件如此容易,但是对目录做任何事情(特别是创建它们并找出它们是否存在)是一种疯狂鹅通过互联网的街道追逐。

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

请帮助我,要么修复当前对 CreateDirectory 的尝试,要么使用其他东西来创建目录。

Thank you!

谢谢!

回答by john

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.

这完全是疯了。Microsoft 有机制可以从同一代码中支持 ANSI(他们称之为)和 UNICODE,因为 Windows 95/8/Me 是 ANSI 操作系统,而 NT/XP/Vista 等是 UNICODE 操作系统。因此,如果您真的想要,您可以编写一组支持两个系统的代码,然后为每个系统重新编译。

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

但是谁对 Windows 95/98/Me 感兴趣呢?然而,这些东西让新手感到困惑。

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 macroCreateDirectory 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.

这是牛肉,在 Windows 中没有名为 CreateDirectory 的函数,有一个名为 CreateDirectoryW 的 UNICODE 函数和一个名为 CreateDirectoryA 的 ANSI 函数。根据您定义的编译器选项,CreateDirectory 被转换为 CreateDirectoryW 或 CreateDirectoryA。如果您最终使用 CreateDirectoryW(正如您显然所做的那样),您必须向它传递一个 Unicode 字符串,如果您使用 CreateDirectoryA,则您传递一个普通的 ASCII 字符串。

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

在您的情况下,最简单的事情就是忘记这一切,直接调用 CreateDirectoryA。

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.

为什么在 C++ 中创建目录如此困难?我猜是因为早在 70 年代 C 还是新事物时,并不是每个操作系统都有目录,所以这些东西从未进入语言标准。

回答by eran

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_tstrings, and not regular char. Making string literals wchar_t's is simple -

VS2005 默认是 Unicode,你最好保持这种方式。将来可能会为您节省很多问题。在 Unicode 构建CreateDirectory(和其他 Win32 函数)中需要wchar_t字符串,而不是常规的char. 制作字符串文字wchar_t很简单 -

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

L"Some string"使其始终为 Unicode,或_T("Some string")使其依赖于配置。

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.

不知道你具体是怎么调用的CreateDirectory,不过如果把字符串转成Unicode太麻烦的话,可以直接使用ANSI版本的- CreateDirectoryA。如果您想要更详细的答案,请发布一些代码。

回答by Andrew Lee

Try using #undef UNICODEbefore including windows.h. Unless you arereally coding Unicode, but that's another story.

#undef UNICODE在包含之前尝试使用windows.h。除非你真的unicode编码,但这是另一个故事。

With the new Visual Studio's, windows.hdefaults to Unicode version, so #undef UNICODEwill give you back the ANSI version. It will show in the stack trace -- you will call CreateDirectoryArather than CreateDirectoryW.

使用新的 Visual Studio,windows.h默认为 Unicode 版本,因此#undef UNICODE将返回 ANSI 版本。它将显示在堆栈跟踪中——您将调用CreateDirectoryA而不是CreateDirectoryW.

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

或者,直接调用CreateDirectoryA,但不知何故,我觉得这不是最佳实践。