Check file is open or not
C / C++ / MFC
5
Posts
5
Posters
0
Views
1
Watching
-
Try and open it with CreateFile[^], specifying 0 (no sharing) for the dwShareMode parameter. If the file is already open, CreateFile will return INVALID_HANDLE_VALUE and GetLastError will return ERROR_SHARING_VIOLATION. The code below will fail, printing the value of ERROR_SHARING_VIOLATION if the file a.a is already open.
HANDLE f = CreateFile("a.a", GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (f == INVALID_HANDLE_VALUE)
{
std::cout << GetLastError() << std::endl;
return 0;
} -