S
S
sddvxd2018-06-26 18:15:03
C++ / C#
sddvxd, 2018-06-26 18:15:03

How to compare unicode string from file and const wchar_t*?

Hello! I'm intercepting the movement of a file, in the file permission.txt, through \n, the paths to files that cannot be moved are written
. I can't figure out how to take a string from a file and compare it with LPCWSTR:

BOOL WINAPI MyMoveFileExW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName, DWORD dwFlags)
{
    std::wstring lpEx(lpExistingFileName);
    std::wifstream wif("C:\\cpp\\permission.txt", std::ios::binary);
    if (wif.is_open())
    {
        wif.imbue(std::locale(wif.getloc(), new std::codecvt_utf8<wchar_t, 0x10ffff, std::consume_header>));

        std::wstring wline;
        while (std::getline(wif, wline))
        {
            if(wline == lpEx){
                return FALSE;
            }
        }
        wif.close();
    }

  BOOL retValue;
  VirtualProtect((LPVOID)pOrigMBAddress_2, SIZE, myProtect_2, NULL);     // assign read write protection
  memcpy((LPVOID)pOrigMBAddress_2, oldBytes_2, SIZE);                            // restore backup
  retValue = pOrigMBAddress_2(lpExistingFileName, lpNewFileName, dwFlags);
  memcpy((LPVOID)pOrigMBAddress_2, JMP_2, SIZE);                                 // set the jump instruction again
  VirtualProtect((LPVOID)pOrigMBAddress_2, SIZE, oldProtect_2, NULL);    // reset protection

  return retValue;
}

Please tell me any option

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
res2001, 2018-06-26
@sddvxd

I would read the file in binary mode and convert the resulting byte array using the WinAPI functions: WideCharToMultiByte/MultiByteToWideChar. The file must be in a strictly specified, previously known encoding.
But it's not C++ style :-) Does
your file contain a BOM header? Judging by the documentation, std::consume_header requires you to use BOM to specify a specific encoding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question