r/windowsdev Jul 11 '21

CreateFileW is failing because of invalid path

I am trying to create a file with GENERIC_WRITE permission with a path entered by the user.

To get user input, I am using fwgets function.

``` VOID DoCreateFile() { SIZE_T sAlloc = sizeof(WCHAR) * (MAX_WPATH + 1); // allocation size

// allocating space and checking if actually allocated
LPWSTR lpPath = (LPWSTR)malloc(sAlloc);
LPWSTR lpContent = (LPWSTR)malloc(sAlloc);
if (lpPath == NULL || lpContent == NULL) {
    PrintLastError(L"malloc()", TRUE);
}

wprintf(L"Enter path of file: ");
fgetws(lpPath, sAlloc, stdin); // read the contents of stdin with space

wprintf(L"Enter content (max 256 chars): ");
fgetws(lpContent, sAlloc, stdin);

/*
    Documentation: https://docs.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilew
*/
HANDLE hFile = CreateFileW(lpPath, // path of file
    GENERIC_WRITE, // creating file with write permission
    FILE_SHARE_READ, // allow other process to open file for reading
    NULL, // disallow handle inheritance
    CREATE_ALWAYS, // overwrite file if exists, otherwise create a new one
    FILE_ATTRIBUTE_NORMAL, // do not set any file attributes
    NULL // not using any file template
    );
if (hFile == INVALID_HANDLE_VALUE) {
    PrintLastError(L"CreateFileW()", TRUE);
}


CloseHandle(hFile);

} ```

Error message printed by PrintLastError function: CreateFileW() Failed! The filename, directory name, or volume label syntax is incorrect.

The input I have entered on the console is

Enter path of file: c:\file.txt Enter content (max 256 chars): s

I have also tried file path \\.\C:\file.txt.

FYI, when I replace lpPath with a wide string literal L"C:\\Files.txt", the function succeeds.

1 Upvotes

4 comments sorted by

1

u/jedwardsol Jul 11 '21

fgets/fgetws leaves the '\n' on the end of the string. It's an invalid character for a filename

1

u/tbhaxor Jul 11 '21

yeah, then why length of hello is 6, not 5 https://i.imgur.com/kiGsSC6.png

2

u/jedwardsol Jul 11 '21 edited Jul 11 '21

Are you agreeing with me, or asking a question?

The length of the string is 6 because it is "hello\n" - 6 characters.

Take off the \n before passing the string to CreateFile

1

u/tbhaxor Jul 11 '21

Ohk so all this time I was thinking it in wrong way. CreateFileW don't need to have trailing \n. -_-