0

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.

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'

I have already tried using wstring and wchar but it gives error again. Can anyone please help me to sortout this issue. looking for correction in code

  • Possible duplicate of [What is the expected input type of FindFirstFile?](https://stackoverflow.com/questions/17534946/what-is-the-expected-input-type-of-findfirstfile) – user202729 Mar 13 '18 at 03:18
  • tl;dr: Use `string::c_str()`. Convert to wide if needed. – user202729 Mar 13 '18 at 03:19
  • 1
    You are using wide-character version of ``FindFirstFile``, replace ``string desk=getenv("DESKTOP");`` with ``wstring desk=_wgetenv(L"DESKTOP");`` and ``FindFirstFileW(desk, &fd);`` with ``FindFirstFileW(desk.c_str(), &fd);`` – Asesh Mar 13 '18 at 03:20
  • I think you need to combine these suggestions. First use Asesh's to get a `wstring` containing the desktop path, then adapt user202729's, to call `desk.c_str()` to get a `WCHAR *` suitable for `FindFirstFileW` – lockcmpxchg8b Mar 13 '18 at 03:27
  • 1
    I strongly suggest using `SHGetFolderPath()` or `SHGetKnownFolderPath()` to get the actual desktop path, and then use `Path(Cch)AddBackslash/Ex()` to make sure it has a trailing slash, or better use `Path(Cch)Append/Ex()` or `Path(Cch)Combine/Ex()` to build up valid paths from multiple components – Remy Lebeau Mar 13 '18 at 04:49
  • i am getting error when combining 2 wstrings i.e first desk and other name of file with wildcard ERROR : debug assertion failed invalid null pointer – Ahmed Mehtab Mar 13 '18 at 11:38

2 Answers2

0

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. Generally the string types they will accept is LPCWSTR (long pointer to wide constant string) or LPCSTR (long pointer to constant string).

First argument of FindFirstFileW() takes a LPCWSTR.

LPCWSTR is a typedef for const wchar_t*. You are passing an std::string, so it's the wrong type.

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 A I think stand for wide and ANSI.

Zebrafish
  • 11,682
  • 3
  • 43
  • 119
0

Since you're calling Win32 functions directly, consistency suggests using GetEnvironmentVariableW instead of getenv. The reliance on a DESKTOP variable is already very Windows-specific; this isn't portable code by any means.

MSalters
  • 173,980
  • 10
  • 155
  • 350