I'm trying to prompt the user to enter a filename/path at the console and then attempt to open this file using CreateFile(). At the moment, the call to CreateFile() works if i use a hardcoded filename and the TEXT() macro. However, upon passing it user input the call fails and GetLastError() returns error 123 or "The filename, directory name, or volume label syntax is incorrect". Below is the relevant code, I'm pretty lost as to why this is happening.
LPTSTR dllPath;
LPDWORD dllPathLength;
dllPath = (LPTSTR)calloc(MAX_PATH, sizeof(TCHAR));
dllPathLength = new DWORD;
if(ReadConsole(hStdIn, dllPath, MAX_PATH, dllPathLength, NULL)==0)
{
_tprintf(TEXT("ReadConsole failed with error %d\n"), GetLastError());
return 1;
}
_tprintf(TEXT("File path entered: %s\n"), dllPath);
hDll = CreateFile(dllPath, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, NULL, NULL);
if (hDll == INVALID_HANDLE_VALUE)
{
_tprintf(TEXT("CreateFile failed with error %d\n"), GetLastError());
return 1;
}
For reference, to make it work with the hardcoded file path I replaced the "dllPath" parameter in the call to CreateFile() with "TEXT("C:\log.log")".
Any help would be much appreciated! Apologies in advance if this is an obvious mistake, i'm still trying to get used to Windows-style C programming, and never was very good with the regular style either.
DWORD dwDllPathLength = 0;and pass&dwDllPathLengthas the parameter. - BrendanMcK