CreateFile fails with error ERROR_SHARING_VIOLATION

18,267

Solution 1

There is nothing wrong with CreateFile - a sharing violation means that something else has the same file open. Which could be your own program, if you have the file open with a share mode of 0, you won't be able to open it again.

When you get the error you can use Process Explorer to determine what processes have the file open.

Solution 2

Is there anti-virus on the machine? Sometimes an AV's (or other software that monitors files) operations and timing can cause sharing conflicts.

This is particularly true if you're opening an existing file for exclusive access (this would be the case for the FILE_WRITE and FILE_APPEND cases if the file already exists).

Solution 3

I mean no disrespect, but I just shot myself in the foot last week on something similar:

Are you sure nothing else has the file open in a way which would prevent the access being requested?

In my case, I had used ctrl-Z in a Linux command window to suspend a program which created a socket connection, then I went to bed. Next morning after a few simple changes, I kept getting "unable to create socket: service in use" messages when running the program. Sadly, I spent hours debugging what I had broken. Once I killed the offending suspended process, it worked fine.

Share:
18,267
Lodle
Author by

Lodle

Im a c++ nut. :P

Updated on June 16, 2022

Comments

  • Lodle
    Lodle almost 2 years

    Im using the CreateFile api and some times it randomly fails with the error: ERROR_SHARING_VIOLATION.

    I have googled and there is almost nothing about this error. The strange thing is next time it is quite happy to open the same file.

    Here is my code:

    void FileHandle::open(const char* fileName, FILE_MODE mode)
    {
        if (m_bIsOpen)
            close();
    
        HANDLE fh = NULL;
    
        DWORD dwDesiredAccess  = GENERIC_READ;
        DWORD dwShareMode = FILE_SHARE_READ;
        DWORD dwCreationDisposition = OPEN_EXISTING;
    
        switch (mode)
        {
        case FILE_READ:
            break;
    
        case FILE_WRITE:
            dwDesiredAccess  = GENERIC_WRITE;
            dwShareMode = 0;
            dwCreationDisposition = CREATE_ALWAYS;
            break;
    
        case FILE_APPEND:
            dwDesiredAccess  = GENERIC_WRITE;
            dwShareMode = 0;
            dwCreationDisposition = OPEN_ALWAYS;
            break;
    
        default:
            throw gcException(ERR_INVALID, "The mode was invalid");
            break;
        }
    
        fh = CreateFile(fileName, dwDesiredAccess, dwShareMode, NULL, dwCreationDisposition, 0, NULL);
    
        if (!fh || fh == INVALID_HANDLE_VALUE)
            throw gcException(ERR_INVALIDFILE, GetLastError(), gcString("Failed to open the file {0}", fileName));
    
        m_hFileHandle = fh;
        m_bIsOpen = true;
    
        if (mode == FILE_APPEND)
        {
            DWORD high = 0;
            DWORD low = GetFileSize(fh, &high);
    
            uint64 pos = (((uint64)high)<<32) + (uint64)low;
            seek(pos);
        }
    }
    

    Am i doing something wrong or is there an issue with the api?

    Edit: Im using the full file name (i.e. C:\somefile.txt) and mode=FILE_WRITE

  • Lodle
    Lodle over 13 years
    No av. Happens about 1 out of every 20 goes
  • Lodle
    Lodle over 13 years
    Its creating a new file that never existed before and nothing else has the name of the new file at this point (its an image cache store).
  • Lodle
    Lodle over 13 years
    Just found out that a part of my code was downloading the same image twice in two different threads, thus the first thread would open the file and the second would die.
  • Paresh Mayani
    Paresh Mayani about 9 years
    Links to external resources are encouraged, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline.
  • AmigoJack
    AmigoJack almost 4 years
    web.archive.org/web/20150328004214/https://… still serves the link. Summary: the only cause is that the file IS already opened (but not shared to be opened by others).