简体   繁体   English

LPCWSTR到代码c ++ VS 2010中的字符串转换问题

[英]LPCWSTR to string conversion issue in code c++ VS 2010

I am trying to find files with specific name and deleting them in c++ as this code works fine if i give direct desktop path to it L"path//" but as path of desktop is different due to different user and system so i what i am doing at the top is to get desktop path in string variable and assigning it rather than direct path. 我正在尝试查找具有特定名称的文件,并在c ++中删除它们,因为如果我给它直接桌面路径L“ path //”,此代码可以正常工作,但是由于用户和系统不同,桌面路径也不同,所以我在顶部要做的是获取字符串变量中的桌面路径并分配它,而不是直接路径。

string desk=getenv("DESKTOP");


        WIN32_FIND_DATAW fd;
        HANDLE hFind = FindFirstFileW(desk, &fd);
        if (hFind != INVALID_HANDLE_VALUE)
        {
            do
            {
                DeleteFileW((wstring(desk) + fd.cFileName).c_str());
            } while (FindNextFileW(hFind, &fd));
            FindClose(hFind);
        }

I am getting the following error 我收到以下错误

Error 4 error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'std::string' to 'LPCWSTR' 错误4错误C2664:'FindFirstFileW':无法将参数1从'std :: string'转换为'LPCWSTR'

I have already tried using wstring and wchar but it gives error again. 我已经尝试过使用wstring和wchar,但是它再次给出错误。 Can anyone please help me to sortout this issue. 谁能帮我解决这个问题。 looking for correction in code 寻找代码中的更正

Windows will usually have two versions of a function, an A suffix will generally be it accepts chars, a W suffix accepts a wchar_t, and without the suffix usually ends up as a macro for whatever character set is selected. Windows通常具有两个版本的函数,一个A后缀通常是它接受字符,一个W后缀可以接受wchar_t,而没有该后缀的结果通常是选择任何字符集的宏。 Generally the string types they will accept is LPCWSTR (long pointer to wide constant string) or LPCSTR (long pointer to constant string). 通常,它们将接受的字符串类型是LPCWSTR(指向宽常量字符串的长指针)或LPCSTR(指向常量字符串的长指针)。

First argument of FindFirstFileW() takes a LPCWSTR. FindFirstFileW()的第一个参数采用LPCWSTR。

LPCWSTR is a typedef for const wchar_t*. LPCWSTR是const wchar_t *的typedef。 You are passing an std::string, so it's the wrong type. 您正在传递一个std :: string,所以它是错误的类型。

Just be consistent with the string type, either do: 只要与字符串类型保持一致,请执行以下操作:

wstring desk = _wgetenv(L"DESKTOP");

string findDigitsInBinary(int A) {

    WIN32_FIND_DATAW fd;

    HANDLE hFind = FindFirstFileW(desk.c_str(), &fd); // First argument takes LPCWSTR

or: 要么:

string desk = getenv("DESKTOP");

string findDigitsInBinary(int A) {

    WIN32_FIND_DATAA fd;

    HANDLE hFind = FindFirstFileA(desk.c_str(), &fd); // First arg takes LPCSTR

Notice that in neither case you are passing the string class, but the character buffer held by the string. 注意,在任何情况下都不传递字符串类,而是传递字符串保留的字符缓冲区。

The suffixes W and AI think stand for wide and ANSI. 后缀W和AI表示Wide和ANSI。

Since you're calling Win32 functions directly, consistency suggests using GetEnvironmentVariableW instead of getenv . 由于直接调用Win32函数,因此一致性建议使用GetEnvironmentVariableW而不是getenv The reliance on a DESKTOP variable is already very Windows-specific; DESKTOP变量的依赖已经非常特定于Windows。 this isn't portable code by any means. 这绝对不是可移植的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM