【问题标题】:Visual Studio 2019 / E0167 / "const char" [duplicate]Visual Studio 2019 / E0167 /“const char”[重复]
【发布时间】:2020-06-13 14:17:02
【问题描述】:

我有个小问题,谁能帮帮我? 问题是““const char *”类型的参数与“LPCWSTR”类型的参数不兼容

我觉得这样不行,"return (bool)CreateDirectory(path.c_str(), NULL)" ,但我无法意识到,“路径”引用的程序是为了什么。

非常感谢!

代码:

#ifndef IO_H
#define IO_H
#include <string>
#include <cstdlib>
#include <fstream>

namespace IO
{
    std::string GetOurPath(const bool append_seperator = false)
    {
        std::string appdata_dir(getenv("APPDATA"));
        std::string full = appdata_dir + "\\Microsoft\\CLR";
        return full + (append_seperator ? "\\" : "");
    }

    bool MkOneDr(std::string path)
    {
        return (bool)CreateDirectory(path.c_str(), NULL) ||
            GetLastError() == ERROR_ALREADY_EXISTS;
    }

}

#endif

【问题讨论】:

  • 使用 std::wstring 因为您使用的是宽字符。 LPCWSTR 是一个宽字符串。
  • @drescherjm ↓↓↓↓↓↓↓↓↓↓↓↓↓

标签: c++ winapi


【解决方案1】:

LPCWSTR 需要一个 Unicode UCS-16 字符数组,即 unsigned short []WCHAR []

要从字符串常量中获取它,您需要像这样添加 L 宏:

std::wstring s = L"\\Microsoft\\CLR";

您也可以使用mbstowcs 将ASCII 字符串转换为WCHAR 字符串,但是对于像您这样的简单短程序,最好直接使用WCHAR 字符串。

或者,您可以从项目设置中删除 DEFINE_UNICODE,并使用 ASCII 版本的 Win32 API。

【讨论】:

  • 没有称为"UCS-16"的字符编码。 Windows SDK 也没有使用名为 DEFINE_UNICODE 的预处理器符号。
  • 如果std::string 的编码正确,也可以调用CreateDirectoryA
  • 另外,L 不是宏,也不是 C++ 中的宽字符类型unsigned shortWCHAR。这是wchar_t
  • @IInspectable WCHAR 是 MS 定义 docs.microsoft.com/en-us/windows/win32/…
  • @IInspectable 对于 L 和 UNICODE 或 _UNICODE 等,请在此处阅读:docs.microsoft.com/en-us/windows/win32/learnwin32/…
猜你喜欢
  • 1970-01-01
  • 2013-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
相关资源
最近更新 更多