Hunt_web.tw

Hi Everyone,

Got 2 questions need some advice,

First, I opened a file in generic read & write mode plus the share for read. According to my own view of the share logic, the 2nd open to the same file should be generic_read + share_read, however this is incorrect. My sample code is attached for review,

LRESULT CMainDlg::OnClickBtnTest(WORD /*wNotifyCode*/, WORD /*wID*/, HWND /*hWndCtl*/, BOOL& /*bHandled*/)
{
TCHAR buf[512]; HANDLE h1, h2; DWORD dwErr;
if (INVALID_HANDLE_VALUE ==
(h1 = CreateFile(_T("abc.abc"), GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)))
{
dwErr = GetLastError();
_stprintf(buf, _T("1st open: dwErr = %u. "), dwErr);
MessageBox(buf, _T("Msg"), MB_ICONASTERISK);
}
else
{
if (INVALID_HANDLE_VALUE !=
/* 1*/ // (h2 = CreateFile(_T("abc.abc"), GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL)))
/* 2*/ // (h2 = CreateFile(_T("abc.abc"), GENERIC_READ, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL)))
/* 3*/ // (h2 = CreateFile(_T("abc.abc"), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL)))
/* 4*/ (h2 = CreateFile(_T("abc.abc"), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL)))
CloseHandle(h2);
else
{
dwErr = GetLastError();
_stprintf(buf, _T("2nd open: dwErr = %u. "), dwErr);
MessageBox(buf, _T("Msg"), MB_ICONASTERISK);
}
CloseHandle(h1);
}
return 0;
}

the answer of #1, 2 & 4 are all dwErr = 32 (ERROR_SHARING_VIOLATION).

Guess which one is the right style for 2nd open My choice is #4, but the VS2005 compiler chosed the #3. Can anyone help me with an explaination

My 2nd Question is,

How should I set the mode of text or binary with CreateFile() I have tried to use _set_fmode(). Is it correct and do I have any other choice Also how to set CreateFile into "APPEND" mode

Thanks for any help & tips in advance,

Hunt



Re: Windows SDK File share problem

Baris ERGUN

1-)  file = CreateFile(m_szFileName,           // open MYFILE.TXT
        FILE_READ_ATTRIBUTES,           // open for reading
        FILE_SHARE_READ,           // share for reading
        NULL,                      // no security
        OPEN_EXISTING,             // existing file only
        FILE_ATTRIBUTE_NORMAL,     // normal file
        NULL); 
     file2 = CreateFile(m_szZipName,           // open MYFILE.TXT
        FILE_WRITE_ATTRIBUTES,           // open for execution
        FILE_SHARE_READ,           // share for reading
        NULL,                      // no security
        OPEN_EXISTING,             // existing file only
        FILE_ATTRIBUTE_NORMAL,     // normal file
        NULL);

That is to say try FILE_READ_ATTRIBUTES    or FILE_WRITE_ATTRIBUTES  as second parameter.

2-)CreateFile Handles both text and binary automatically unlike fread and fopen functions where you have to specify the type of the file ro be read either text or binary

 

3_) FILE_MODE_APPEND This constant allows a text file to be opened or created in append mode. When a file is opened in append mode using OpenFile, the file pointer is at the end of the file. You can use the WriteLine function to append lines to the end of the file. Files created using CreateFile are new (empty), so lines appended to the files are written at the beginning of the files. Note that if you open a file in append mode, the GetLine function will fail if you call it because the file pointer is at the end of the file.

 






Re: Windows SDK File share problem

Hunt_web.tw

Dear Raris,

Thx for your kind response and your info about those oflag constants (oflag - ref: CRT routine <open.c>).

You have mentioned about "CreateFile Handles both text and binary automatically", please advice where I can find its detail reference

Also,

The main part of my question is focused on "File Sharing", how the "1st file opener" who set the oflag and shflag wolud influence the "2nd file opener" in file usage and his arguments setting. I have found a reference table of MSDN recently on following link,

http://msdn.microsoft.com/library/default.asp url=/library/en-us/fileio/fs/creating_and_opening_files.asp

But, I think the table logic for file sharing is incorrect.

Since I am not an expert of C++ or Win32 API. It would be very appreciate if someone can give me some opinion on the reason how and why the "file sharing logic" is set like the above table

Hunt





Re: Windows SDK File share problem

Baris ERGUN

Actually this question is better to ask in Windows SDK Forum. But an answer to your question would be I could not find any information mentioning about reading a file in binary or text mode on CreateFile or ReadFile references which are Windows API Functions. I think Microsoft guys handled the recognition of binary and text file inside the API. Becuase what these functions doing must be using of fread and fopen like functions in a more intelligent manner.