-1

I have a little problem, anyone can help me? The problem is "argument of type "const char *" is incompatible with parameter of type "LPCWSTR""

I think this not ok, "return (bool)CreateDirectory(path.c_str(), NULL)" , but i cannot realise it, that for what... the program the "path" cites.

Many thanks!

Code:

#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
bolov
  • 72,283
  • 15
  • 145
  • 224

1 Answers1

0

LPCWSTR expects a Unicode UCS-16 character array, which is unsigned short [] or WCHAR [].

To get that from a string constant you would need to prepend the L macro like this:

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

You can also convert ASCII string to WCHAR string using mbstowcs, but for a simple short program like yours it is better to work with WCHAR strings directly.

Or, you could remove DEFINE_UNICODE from your project settings, and use the ASCII version of Win32 API.

Lev M.
  • 6,088
  • 1
  • 10
  • 23
  • 2
    There is no character encoding called *"UCS-16"*. There's also no preprocessor symbol used by the Windows SDK that is called `DEFINE_UNICODE`. – IInspectable Jun 13 '20 at 16:04
  • Also one could call `CreateDirectoryA` if the `std::string` is in the proper encoding. – Phil1970 Jun 13 '20 at 16:15
  • Also, `L` is not a macro, nor is the type of a wide character in C++ `unsigned short` or `WCHAR`. It's `wchar_t`. – IInspectable Jun 13 '20 at 16:27
  • @IInspectable WCHAR is an MS define https://learn.microsoft.com/en-us/windows/win32/extensible-storage-engine/wchar – Lev M. Jun 13 '20 at 18:18
  • @IInspectable as for L and UNICODE or _UNICODE etc, read here: https://learn.microsoft.com/en-us/windows/win32/learnwin32/working-with-strings – Lev M. Jun 13 '20 at 18:19
  • @IInspectable and there is USC-16 encoding it is another *valid* name for UTF-16: https://en.wikipedia.org/wiki/UTF-16 Please check your facts before commenting – Lev M. Jun 13 '20 at 18:21
  • I know, what `WCHAR` is in the Windows SDK. That doesn't make it the type of a wide character in C++, though. I also know what an `L`-prefix to a string literal means in C++. It's not a macro. Likewise, I understand, what the preprocessor symbols `UNICODE` and `_UNICODE` control. It is the former that needs to be defined here. The Wikipedia entry doesn't contain the term *"UCS-16"*, leaving me puzzled as to why it would lend itself to argue for it being a valid name for UTF-16. So after checking the facts, there's not much in this proposed answer that isn't wrong. – IInspectable Jun 13 '20 at 19:02
  • @IInspectable if you look a the code and the tags in the question, you will see it is about Windows API, not clean standard C++ so the answer needs a type supported by the API. I will grant you that the L prefix may be a compiler built-in and not a macro (I am not sure and don't want to dig), but that implementation detail does not alter the correctness of my proposed code. Also, the first paragraph of the Wikipedia page I link contains the following sentence: "encoding known as UCS-2 (for 2-byte Universal Character Set)" Also, why not post a correct answer if you know working code? – Lev M. Jun 13 '20 at 19:19
  • Because this question has been asked a billion times before, and there are many, many duplicates readily available. Hence I closed it. That's how Stack Overflow is meant to work. – IInspectable Jun 13 '20 at 19:21