1
votes

I have an MFC App which fires up a separate thread for downloading some files via cURL. At the start it downloads a text file with file sizes and last write times. Then it checks the files on disk and queues it for downloading if it has different values.. The problem is; the CreateFile call in the thread arbitrarily returns INVALID_HANDLE_VALUE. I always do CloseHandle() after a successful CreateFile(). The failing files are just random. Sometimes a file in the root dir, another time a file in a nested directory. The problem is not related to localization or directory/file names since sometimes all checks pass but sometimes don't. GetLastError() return 2 or 3 on occasion which are "File not found" / "Path not found" respectively.

When I put the function checking the file write times and size straight into the OnInitDialog() function, everything works. This smells like a multithreading issue but I double-checked everything from memory allocations to file handles.

The same code works in a console application also in a separate thread.

The platform is Win7 64bit.
Linking statically to the runtime and MFC.

1
Are you using absolute paths or relative paths? Maybe another thread is changing the current directory, causing your relative paths to go invalid. - Raymond Chen
I can not thank you enough... :) Make it an answer so I can up it. - fgungor
Go ahead and submit your own answer and accept it. - Raymond Chen

1 Answers

1
votes

in my case GetCurrentDirectory() returned the system32 path after some time so my code failed because of credentials. I fixed the issue by determining file paths manually (getting the exe path at the start and use it from there on...) . Make sure you are not trying to write to/read from a privileged location on disk. Check your paths.