CreateFile参数设置为FILE_SHARE_READ,其他程序不能读?

C语言 码拜 9年前 (2015-10-23) 1695次浏览
#include <windows.h>
#include <stdio.h>
int main()
{
	DWORD dwBytes;
	char data[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
	HANDLE hFile = CreateFile("temp.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,CREATE_ALWAYS,FILE_FLAG_DELETE_ON_CLOSE,NULL);
	WriteFile(hFile,data,sizeof(data)-1,&dwBytes,NULL);
    
	getchar(); //此时,手动记事本打开temp.txt 不能打开说是占用(明明使用了FILE_SHARE_READ)
	CloseHandle(hFile);         
	return 0;
}
解决方案:5分
在第二参数中,加上  | GENERIC_READ试试
解决方案:15分
不知道是不是记事本打开文件是不共享的。在代码里再加一个CreateFile和WriteFile,两个CreateFile以共享模式打开同一个文件并写入发现没什么问题。但是假如一个CreateFile是以非共享方式打开文件,那它写入的时候就会error,觉得这就和记事本一样。
解决方案:5分
To get extended error information, call GetLastError.

解决方案:5分
首先,题外话写文件开share_read本来就是作死。

然后,Notepad根本不支持只读打开。

最后,搞清楚share_read是允许别的程序用读模式打开文件,这个时候假如别的程序请求读以外的权限就会失败,而不是只要请求了读权限就能成功。

解决方案:5分
_locking

Locks or unlocks bytes of a file.
int _locking( int handle, int mode, long nbytes );
Routine Required Header Optional Headers Compatibility

_locking <io.h> and <sys/locking.h> <errno.h> Win 95, Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version

LIBCMT.LIB Multithread static library, retail version

MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
_locking returns 0 if successful. A return value of –1 indicates failure, in which case errno is set to one of the following values:
EACCES
Locking violation (file already locked or unlocked).
EBADF
Invalid file handle.
EDEADLOCK
Locking violation. Returned when the _LK_LOCK or _LK_RLCK flag is specified and the file cannot be locked after 10 attempts.
EINVAL
An invalid argument was given to _locking.
Parameters
handle
File handle
mode
Locking action to perform
nbytes
Number of bytes to lock
Remarks
The _locking function locks or unlocks nbytes bytes of the file specified by handle. Locking bytes in a file prevents access to those bytes by other processes. All locking or unlocking begins at the current position of the file pointer and proceeds for the next nbytes bytes. It is possible to lock bytes past end of file.
mode must be one of the following manifest constants, which are defined in LOCKING.H:
_LK_LOCK
Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.
_LK_NBLCK
Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.
_LK_NBRLCK
Same as _LK_NBLCK.
_LK_RLCK
Same as _LK_LOCK.
_LK_UNLCK
Unlocks the specified bytes, which must have been previously locked.
Multiple regions of a file that do not overlap can be locked. A region being unlocked must have been previously locked. _locking does not merge adjacent regions; if two locked regions are adjacent, each region must be unlocked separately. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.
Example
/* LOCKING.C: This program opens a file with sharing. It locks

* some bytes before reading them, then unlocks them. Note that the

* program works correctly only if the file exists.

*/
#include <io.h>

#include <sys/types.h>

#include <sys/stat.h>

#include <sys/locking.h>

#include <share.h>

#include <fcntl.h>

#include <stdio.h>

#include <stdlib.h>
void main( void )

{

int  fh, numread;

char buffer[40];
/* Quit if can””t open file or system doesn””t

* support sharing.

*/

fh = _sopen( “locking.c”, _O_RDWR, _SH_DENYNO,

_S_IREAD | _S_IWRITE );

if( fh == -1 )

exit( 1 );
/* Lock some bytes and read them. Then unlock. */

if( _locking( fh, LK_NBLCK, 30L ) != -1 )

{

printf( “No one can change these bytes while I””m reading them\n” );

numread = _read( fh, buffer, 30 );

printf( “%d bytes read: %.30s\n”, numread, buffer );

lseek( fh, 0L, SEEK_SET );

_locking( fh, LK_UNLCK, 30L );

printf( “Now I””m done. Do what you will with them\n” );

}

else

perror( “Locking failed\n” );
_close( fh );

}
Output
No one can change these bytes while I””m reading them

30 bytes read: /* LOCKING.C: This program ope

Now I””m done. Do what you will with them
File Handling Routines
See Also   _creat, _open

解决方案:5分
Windows核心编程》

《深入解析Windows操作系统-Windows Internals》


CodeBye 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权 , 转载请注明CreateFile参数设置为FILE_SHARE_READ,其他程序不能读?
喜欢 (0)
[1034331897@qq.com]
分享 (0)