Skip to content

Instantly share code, notes, and snippets.

@hkuno9000
Last active December 22, 2022 18:18
Show Gist options
  • Save hkuno9000/ac9ef084c8431ea148a5 to your computer and use it in GitHub Desktop.
Save hkuno9000/ac9ef084c8431ea148a5 to your computer and use it in GitHub Desktop.
CheetSheet file share mode

Win32/ATL

<WinNT.h>
#define FILE_SHARE_READ                 0x00000001
#define FILE_SHARE_WRITE                0x00000002
#define FILE_SHARE_DELETE               0x00000004

CreateFile(filename, access, sharemode, creation, attributes, ...);
CAtlFile::Open(filename, access, sharemode, creation, attributes, ...);
sharemode description
0 share none, deny read/write
FILE_SHARE_READ share read, deny write
FILE_SHARE_WRITE share write, deny read
FILE_SHARE_READ + FILE_SHARE_WRITE share read/write, deny none

Visual C++ CRT

<share.h>
#define _SH_DENYRW      0x10    /* deny read/write mode */
#define _SH_DENYWR      0x20    /* deny write mode */
#define _SH_DENYRD      0x30    /* deny read mode */
#define _SH_DENYNO      0x40    /* deny none mode */
#define _SH_SECURE      0x80    /* secure mode */

_sopen,   _wsopen(filename, oflag, shflag, pmode);
_fsopen, _wfsopen(filename, mode, shflag);
std::fstream::open(filename, iosmode, _prot = ios::base::_Openprot); // _Openprot = _SH_DENYNO
_open, _wopen(filename, mode, pmode);     // always _SH_DENYNO @see VS2019-CRT-SRC
fopen, _wfopen(filename, mode);           // always _SH_DENYNO @see VS2019-CRT-SRC
fopen_s, _wfopen_s(&fp, filename, mode); // always _SH_SECURE @see VS2019-CRT-SRC
shflag description
0 error: Invalid sharing flag
_SH_DENYRW share none, deny read/write
_SH_DENYWR share read, deny write
_SH_DENYRD share write, deny read
_SH_DENYNO share read/write, deny none
_SH_SECURE if mode is "r" then _SH_DENYWR, else _SH_DENYRW

VS2019-CRT-SRC

  • C:\Program Files (x86)\Windows Kits\10\Source\10.0.18362.0\ucrt\lowio\open.cpp # _open, _wopen
  • C:\Program Files (x86)\Windows Kits\10\Source\10.0.18362.0\ucrt\stdio\fopen.cpp # fopen, fopen_s, _wfopen, _wfopen_s

MFC

<afx.h>
class CFile {
public:
// Flag values
	enum OpenFlags {
		modeRead =         (int) 0x00000,
		modeWrite =        (int) 0x00001,
		modeReadWrite =    (int) 0x00002,
		shareCompat =      (int) 0x00000,
		shareExclusive =   (int) 0x00010,
		shareDenyWrite =   (int) 0x00020,
		shareDenyRead =    (int) 0x00030,
		shareDenyNone =    (int) 0x00040,
		modeNoInherit =    (int) 0x00080,
		modeCreate =       (int) 0x01000,
		modeNoTruncate =   (int) 0x02000,
		typeText =         (int) 0x04000, // typeText and typeBinary are
		typeBinary =       (int) 0x08000, // used in derived classes only
		osNoBuffer =       (int) 0x10000,
		osWriteThrough =   (int) 0x20000,
		osRandomAccess =   (int) 0x40000,
		osSequentialScan = (int) 0x80000,
		};

// Constructors
	CFile();
	CFile(HANDLE hFile);
	CFile(LPCTSTR lpszFileName, UINT nOpenFlags);
	virtual BOOL Open(LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL);
nOpenFlags description
0 share none, deny read/write
shareCompat share none, deny read/write
shareExclusive share none, deny read/write
shareDenyWrite share read, deny write
shareDenyRead share write, deny read
shareDenyNone share read/write, deny none
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment