提问者:小点点

Visual Studio 2019/E0167/"常量字符“


我有个小问题,有人能帮我吗? 问题是“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

共1个答案

匿名用户

LPCWSTR需要一个Unicode UCS-16字符数组,该数组为unsigned short[]wchar[]

要从字符串常量中获得该值,您需要在l宏前添加如下所示:

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

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

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