CreateFileW GetLastError Returns 2

  c++, file, registry
void * hFile = CreateFileW
(
    L"\.OurFile",        // name of the write
    GENERIC_WRITE,          // open for writing
    0,                      // do not share
    NULL,                   // default security
    CREATE_NEW,             // create new file only
    FILE_ATTRIBUTE_NORMAL,  // normal file
    NULL
);

if (!hFile || hFile == INVALID_HANDLE_VALUE)
{
   std::cout << "hFile GetLastError(): 0x" << std::hex << GetLastError() << std::endl;
   std::getchar();
}

My code above is attempting to create a file. But the weird thing is, GetLastError() returns 2 somehow? I check Microsofts documentation and 2 gets returned if the file does not exist. But i am trying to create a file and not open an existing one. If anyone has a solution please let me know. Thank you all for your time.

Source: Windows Questions C++

LEAVE A COMMENT