ssffeng
驱动牛犊
驱动牛犊
  • 注册日期2003-11-06
  • 最后登录2005-04-08
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1907回复:4

怎么知道creatfile()打开的是那个端口?

楼主#
更多 发布于:2004-04-13 17:06
我用的是采用CY7c68001,驱动程序是用DS生成的,读写设备都有两个端口可以读写,我怎么知道creatfile()打开的是那个端口?是不是只有知道那个端口读写的时候才能用writefile()和readfile()读写设备?
  我刚开始写应用程序,所以什么都不明白
  还望高手赐教!
  多谢了!!!
bakerj
驱动牛犊
驱动牛犊
  • 注册日期2004-02-26
  • 最后登录2005-08-27
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
沙发#
发布于:2004-04-14 01:07
CreateFile 传进去的文件名可以区分
programming is game
y5318
驱动中牛
驱动中牛
  • 注册日期2001-09-18
  • 最后登录2018-05-29
  • 粉丝1
  • 关注0
  • 积分14分
  • 威望22点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
  • 社区居民
板凳#
发布于:2004-04-14 07:42
CreateFile

The CreateFile function creates or opens a file, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, or pipe. The function returns a handle that can be used to access the object.


Windows Me/98/95:  You cannot open a directory, physical disk, or volume using CreateFile.



HANDLE CreateFile(
  LPCTSTR lpFileName,
  DWORD dwDesiredAccess,
  DWORD dwShareMode,
  LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  DWORD dwCreationDisposition,
  DWORD dwFlagsAndAttributes,
  HANDLE hTemplateFile
);

Parameters
lpFileName
[in] Pointer to a null-terminated string that specifies the name of the object to create or open.
In the ANSI version of this function, the name is limited to MAX_PATH characters. To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend \"\\\\?\\\" to the path. For more information, see Naming a File.


Windows Me/98/95:  This string must not exceed MAX_PATH characters.


dwDesiredAccess
[in] Access to the object. For a list of values, see File Security and Access Rights. You cannot request an access mode that conflicts with the sharing mode specified in a previous open request whose handle is still open.
If this parameter is zero, the application can query file and device attributes without accessing the device. This is useful if an application wants to determine the size of a floppy disk drive and the formats it supports without requiring a floppy in the drive. It can also be used to test for the file\'s or directory\'s existence without opening it for read or write access.

dwShareMode
[in] Sharing mode of the object. You cannot request a sharing mode that conflicts with the access mode specified in a previous open request whose handle is still open.
If this parameter is zero and CreateFile succeeds, the object cannot be shared and cannot be opened again until the handle is closed. For more information about sharing violations, see the Remarks section.

To enable other processes to share the object while your process has it open, use a combination of one or more of the following values to specify the type of access they can request when they open the object. These sharing options remain in effect until you close the handle to the object.

Value Meaning
FILE_SHARE_DELETE Enables subsequent open operations on the object to request delete access. Otherwise, other processes cannot open the object if they request delete access.
If the object has already been opened with delete access, the sharing mode must include this flag.


Windows Me/98/95:  This flag is not supported.

 
FILE_SHARE_READ Enables subsequent open operations on the object to request read access. Otherwise, other processes cannot open the object if they request read access.
If the object has already been opened with read access, the sharing mode must include this flag.
 
FILE_SHARE_WRITE Enables subsequent open operations on the object to request write access. Otherwise, other processes cannot open the object if they request write access.
If the object has already been opened with write access, the sharing mode must include this flag.
 

lpSecurityAttributes
[in] Pointer to a SECURITY_ATTRIBUTES structure that determines whether the returned handle can be inherited by child processes. If lpSecurityAttributes is NULL, the handle cannot be inherited.
The lpSecurityDescriptor member of the structure specifies a security descriptor for the object. If lpSecurityAttributes is NULL, the object gets a default security descriptor. The ACLs in the default security descriptor for a file or directory are inherited from its parent directory. Note that the target file system must support security on files and directories for this parameter to have an effect on them. (This is indicated when GetVolumeInformation returns FS_PERSISTENT_ACLS.) CreateFile ignores lpSecurityDescriptor when opening an existing file, but continues to use the other structure members.

dwCreationDisposition
[in] Action to take on files that exist, and which action to take when files do not exist. For more information about this parameter, see the Remarks section. This parameter must be one of the following values. Value Meaning
CREATE_ALWAYS Creates a new file. If the file exists, the function overwrites the file, clears the existing attributes, combines the specified file attributes and flags with FILE_ATTRIBUTE_ARCHIVE, but does not set the security descriptor specified by the SECURITY_ATTRIBUTES structure.
CREATE_NEW Creates a new file. The function fails if the specified file already exists.  
OPEN_ALWAYS Opens the file, if it exists. If the file does not exist, the function creates the file as if dwCreationDisposition were CREATE_NEW.
OPEN_EXISTING Opens the file. The function fails if the file does not exist.
For a discussion of why you should use OPEN_EXISTING for devices, see Remarks.
 
TRUNCATE_EXISTING Opens the file and truncates it so that its size is zero bytes. The calling process must open the file with the GENERIC_WRITE access right. The function fails if the file does not exist.  

dwFlagsAndAttributes
[in] File attributes and flags.
The following file attributes and flags are used only for file objects, not other types of objects created by CreateFile.

When CreateFile opens an existing file, it combines the file flags with existing file attributes, and ignores any supplied file attributes.

This parameter can include any combination of the file attributes (noting that all other file attributes override FILE_ATTRIBUTE_NORMAL).

Attribute Meaning
FILE_ATTRIBUTE_ARCHIVE The file should be archived. Applications use this attribute to mark files for backup or removal.
FILE_ATTRIBUTE_ENCRYPTED The file or directory is encrypted. For a file, this means that all data in the file is encrypted. For a directory, this means that encryption is the default for newly created files and subdirectories. For more information, see File Encryption.
This flag has no effect if FILE_ATTRIBUTE_SYSTEM is also specified.
 
FILE_ATTRIBUTE_HIDDEN The file is hidden. It is not to be included in an ordinary directory listing.
FILE_ATTRIBUTE_NORMAL The file has no other attributes set. This attribute is valid only if used alone.
FILE_ATTRIBUTE_NOT_CONTENT_INDEXED The file will not be indexed by the content indexing service.  
FILE_ATTRIBUTE_OFFLINE The data of the file is not immediately available. This attribute indicates that the file data has been physically moved to offline storage. This attribute is used by Remote Storage, the hierarchical storage management software. Applications should not arbitrarily change this attribute.
FILE_ATTRIBUTE_READONLY The file is read only. Applications can read the file but cannot write to it or delete it.
FILE_ATTRIBUTE_SYSTEM The file is part of or is used exclusively by the operating system.  
FILE_ATTRIBUTE_TEMPORARY The file is being used for temporary storage. File systems avoid writing data back to mass storage if sufficient cache memory is available, because often the application deletes the temporary file shortly after the handle is closed. In that case, the system can entirely avoid writing the data. Otherwise, the data will be written after the handle is closed.


This parameter can also include any combination of the following flags.


Flag Meaning
FILE_FLAG_BACKUP_SEMANTICS Indicates that the file is being opened or created for a backup or restore operation. The system ensures that the calling process overrides file security checks, provided it has the SE_BACKUP_NAME and SE_RESTORE_NAME privileges. For more information, see Changing Privileges in a Token.
You can also set this flag to obtain a handle to a directory. Where indicated, a directory handle can be passed to some functions in place of a file handle.


Windows Me/98/95:  This flag is not supported.

 
FILE_FLAG_DELETE_ON_CLOSE Indicates that the operating system is to delete the file immediately after all of its handles have been closed, not just the handle for which you specified FILE_FLAG_DELETE_ON_CLOSE.
If there are existing open handles to the file, the call fails unless they were all opened with the FILE_SHARE_DELETE share mode.

Subsequent open requests for the file will fail, unless they specify the FILE_SHARE_DELETE share mode.
 
FILE_FLAG_NO_BUFFERING Instructs the system to open the file with no system caching. This flag has no effect on hard disk caching. When combined with FILE_FLAG_OVERLAPPED, the flag gives maximum asynchronous performance, because the I/O does not rely on the synchronous operations of the memory manager. However, some I/O operations will take longer, because data is not being held in the cache. Also, the file metadata may still be cached. To flush the metadata to disk, use the FlushFileBuffers function.
An application must meet certain requirements when working with files opened with FILE_FLAG_NO_BUFFERING:


File access must begin at byte offsets within the file that are integer multiples of the volume\'s sector size.
File access must be for numbers of bytes that are integer multiples of the volume\'s sector size. For example, if the sector size is 512 bytes, an application can request reads and writes of 512, 1024, or 2048 bytes, but not of 335, 981, or 7171 bytes.
Buffer addresses for read and write operations should be sector aligned (aligned on addresses in memory that are integer multiples of the volume\'s sector size). Depending on the disk, this requirement may not be enforced.
One way to align buffers on integer multiples of the volume sector size is to use VirtualAlloc to allocate the buffers. It allocates memory that is aligned on addresses that are integer multiples of the operating system\'s memory page size. Because both memory page and volume sector sizes are powers of 2, this memory is also aligned on addresses that are integer multiples of a volume\'s sector size.

An application can determine a volume\'s sector size by calling the GetDiskFreeSpace function.
 
FILE_FLAG_OPEN_NO_RECALL Indicates that the file data is requested, but it should continue to reside in remote storage. It should not be transported back to local storage. This flag is intended for use by remote storage systems.
FILE_FLAG_OPEN_REPARSE_POINT Specifying this flag inhibits the reparse behavior of NTFS reparse points. When the file is opened, a file handle is returned, whether the filter that controls the reparse point is operational or not. This flag cannot be used with the CREATE_ALWAYS flag.  
FILE_FLAG_OVERLAPPED Instructs the system to initialize the object, so that operations that take a significant amount of time to process return ERROR_IO_PENDING. When the operation is finished, the specified event is set to the signaled state.
When you specify FILE_FLAG_OVERLAPPED, the file read and write functions must specify an OVERLAPPED structure. That is, when FILE_FLAG_OVERLAPPED is specified, an application must perform overlapped reading and writing.

When FILE_FLAG_OVERLAPPED is specified, the system does not maintain the file pointer. The file position must be passed as part of the lpOverlapped parameter (pointing to an OVERLAPPED structure) to the file read and write functions.

This flag also enables more than one operation to be performed simultaneously with the handle (a simultaneous read and write operation, for example).
 
FILE_FLAG_POSIX_SEMANTICS Indicates that the file is to be accessed according to POSIX rules. This includes allowing multiple files with names, differing only in case, for file systems that support such naming. Use care when using this option because files created with this flag may not be accessible by applications written for MS-DOS or 16-bit Windows.  
FILE_FLAG_RANDOM_ACCESS Indicates that the file is accessed randomly. The system can use this as a hint to optimize file caching.
FILE_FLAG_SEQUENTIAL_SCAN Indicates that the file is to be accessed sequentially from beginning to end. The system can use this as a hint to optimize file caching. If an application moves the file pointer for random access, optimum caching may not occur; however, correct operation is still guaranteed.
Specifying this flag can increase performance for applications that read large files using sequential access. Performance gains can be even more noticeable for applications that read large files mostly sequentially, but occasionally skip over small ranges of bytes.
 
FILE_FLAG_WRITE_THROUGH Instructs the system to write through any intermediate cache and go directly to disk.
If FILE_FLAG_NO_BUFFERING is not also specified, so that system caching is in effect, then the data is written to the system cache, but is flushed to disk without delay.

If FILE_FLAG_NO_BUFFERING is also specified, so that system caching is not in effect, then the data is immediately flushed to disk without going through the system cache. The operating system also requests a write-through the hard disk cache to persistent media. However, not all hardware supports this write-through capability.
 


If the CreateFile function opens the client side of a named pipe, the dwFlagsAndAttributes parameter can also contain Security Quality of Service information. For more information, see Impersonation Levels. When the calling application specifies the SECURITY_SQOS_PRESENT flag, the dwFlagsAndAttributes parameter can contain one or more of the following values.


Value Meaning
SECURITY_ANONYMOUS Impersonate the client at the Anonymous impersonation level.
SECURITY_CONTEXT_TRACKING The security tracking mode is dynamic. If this flag is not specified, the security tracking mode is static.  
SECURITY_DELEGATION Impersonate the client at the Delegation impersonation level.
SECURITY_EFFECTIVE_ONLY Only the enabled aspects of the client\'s security context are available to the server. If you do not specify this flag, all aspects of the client\'s security context are available.
This allows the client to limit the groups and privileges that a server can use while impersonating the client.
 
SECURITY_IDENTIFICATION Impersonate the client at the Identification impersonation level.
SECURITY_IMPERSONATION Impersonate the client at the Impersonation impersonation level.

hTemplateFile
[in] Handle to a template file, with the GENERIC_READ access right. The template file supplies file attributes and extended attributes for the file being created. This parameter can be NULL.
If opening an existing file, CreateFile ignores the template file.


Windows Me/98/95:  The hTemplateFile parameter must be NULL. If you supply a handle, the call fails and GetLastError returns ERROR_NOT_SUPPORTED.


Return Values
If the function succeeds, the return value is an open handle to the specified file. If the specified file exists before the function call and dwCreationDisposition is CREATE_ALWAYS or OPEN_ALWAYS, a call to GetLastError returns ERROR_ALREADY_EXISTS (even though the function has succeeded). If the file does not exist before the call, GetLastError returns zero.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

Remarks
Use the CloseHandle function to close an object handle returned by CreateFile.


Windows Server 2003, Windows XP, Windows 2000:  A sharing violation will occur if an attempt is made to open a file or directory for deletion on a remote computer when the value of the dwDesiredAccess parameter is the DELETE access flag OR\'ed with any other access flag, and the remote file or directory has not been opened with FILE_SHARE_DELETE. To avoid the sharing violation in this scenario, open the remote file or directory with the DELETE access right only or call DeleteFile without first opening the file or directory for deletion.


Some file systems, such as NTFS, support compression or encryption for individual files and directories. On volumes formatted for such a file system, a new file inherits the compression and encryption attributes of its directory.

You cannot use CreateFile to control compression on a file or directory. See File Compression and Decompression for information on compressing files, and enabling or disabling file compression within a directory.

See File Encryption for more information on encrypting and decrypting files, and enabling or disabling file encryption within a directory.



Windows Server 2003, Windows XP, Windows 2000:  For backward compatibility purposes, CreateFile does not apply Windows 2000 inheritance rules when you specify a security descriptor in lpSecurityAttributes. To support inheritance on Windows 2000 and later, APIs that later query the security descriptor of this object may heuristically determine and report that inheritance is in effect. See Automatic Propagation of Inheritable ACEs for more information about inheritance rules in Windows 2000 and later operating systems, and how they differ from previous versions of Windows.



Windows Me/98/95:  CreateFileW is supported by the Microsoft Layer for Unicode. To use this, you must add certain files to your application, as outlined in Microsoft Layer for Unicode on Windows 95/98/Me Systems.


Files

If you are attempting to create a file on a floppy drive that does not have a floppy disk or a CD-ROM drive that does not have a CD, the system displays a message box asking the user to insert a disk or a CD, respectively. To prevent the system from displaying this message box, call the SetErrorMode function with SEM_FAILCRITICALERRORS.


Windows Server 2003, Windows XP, Windows 2000:  If CREATE_ALWAYS and FILE_ATTRIBUTE_NORMAL are specified, CreateFile will fail and set the last error to ERROR_ACCESS_DENIED if the file exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM attribute. To avoid the error, specify the same attributes as the existing file.


See Creating and Opening Files for a list of actions CreateFile takes to create a new file, or to open an existing file.

If you rename or delete a file, then restore it shortly thereafter, the system searches the cache for file information to restore. Cached information includes its short/long name pair and creation time.


Windows Me/98/95:  This remark does not apply.



If there are multiple attempts to open the same file, there may be sharing violations depending on the sharing modes and access modes across the set of calls. See Creating and Opening Files for details and examples.
If you call CreateFile on a file that is pending deletion as a result of a previous call to DeleteFile, the function fails. The operating system delays file deletion until all handles to the file are closed. GetLastError returns ERROR_ACCESS_DENIED.


Directories

An application cannot create a directory with CreateFile; it must call CreateDirectory or CreateDirectoryEx to create a directory. Opening a directory with CreateFile requires the FILE_FLAG_BACKUP_SEMANTICS flag.

When using CreateFile to open a directory during defragmentation of a FAT or FAT32 volume, do not specify the MAXIMUM_ALLOWED access right. Access to the directory will be denied if this is done. Specify the GENERIC_READ access right instead.


Physical Disks and Volumes

You can use the CreateFile function to open a physical disk drive or a volume. The function returns a handle that can be used with the DeviceIoControl function. This enables you to access the disk\'s partition table. It is potentially dangerous to do so, since an incorrect write to a disk could make its contents inaccessible. The following requirements must be met for such a call to succeed:


The caller must have administrative privileges. For more information, see Running with Special Privileges.
The dwCreationDisposition parameter must have the OPEN_EXISTING flag.
When opening a volume or floppy disk, the dwShareMode parameter must have the FILE_SHARE_WRITE flag.
When opening a physical drive, x, the lpFileName string should be of the form \\\\.\\PHYSICALDRIVE<x>. Hard disk numbers start at zero. The following table shows some example physical drive strings.

String Meaning
\\\\.\\PHYSICALDRIVE0 Opens the first physical drive.
\\\\.\\PHYSICALDRIVE2 Opens the third physical drive.

For an example showing how to open a physical drive, see Calling DeviceIoControl.

When opening a volume or floppy drive, the lpFileName string should be of the form \\\\.\\<x>:. Do not use a trailing backslash. This would indicate the root directory of the drive. The following table shows some example drive strings.

String Meaning
\\\\.\\A: Opens drive A (floppy drive).
\\\\.\\C: Opens drive C (volume).

You can also open a volume by referring to its volume name. For more information, see Naming a Volume.

Volume handles may be opened as noncached at the discretion of the file system, even when the noncached option is not specified with CreateFile. You should assume that all Microsoft file systems open volume handles as noncached. The restrictions on noncached I/O for files apply to volumes as well.

A file system may or may not require buffer alignment even though the data is noncached. However, if the noncached option is specified when opening a volume, buffer alignment is enforced regardless of the file system on the volume. It is recommended on all file systems that you open volume handles as noncached and follow the noncached I/O restrictions.


Tape Drives

You can open tape drives using a file name of the form \\\\.\\TAPE<x> where <x> is a number indicating which drive to open, starting with tape drive 0. To open tape drive 0 in an application written in C or C++, use the file name \"\\\\\\\\.\\\\TAPE0\". For more information on manipulating tape drives for backup or other applications, see Backup.


Windows Me/98/95:  Opening tape drives is not supported.



Communications Resources

The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, and the hTemplate parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O. For more information about communications, see Communications.


Consoles

The CreateFile function can create a handle to console input (CONIN$). If the process has an open handle to it as a result of inheritance or duplication, it can also create a handle to the active screen buffer (CONOUT$). The calling process must be attached to an inherited console or one allocated by the AllocConsole function. For console handles, set the CreateFile parameters as follows.

Parameters Value
lpFileName Use the CONIN$ value to specify console input and the CONOUT$ value to specify console output.
CONIN$ gets a handle to the console\'s input buffer, even if the SetStdHandle function redirected the standard input handle. To get the standard input handle, use the GetStdHandle function.

CONOUT$ gets a handle to the active screen buffer, even if SetStdHandle redirected the standard output handle. To get the standard output handle, use GetStdHandle.
 
dwDesiredAccess GENERIC_READ | GENERIC_WRITE is preferred, but either one can limit access.
dwShareMode When opening CONIN$, be sure to specify FILE_SHARE_READ. When opening CONOUT$, be sure to specify FILE_SHARE_WRITE.

If the calling process inherited the console or if a child process should be able to access the console, this parameter must be FILE_SHARE_READ | FILE_SHARE_WRITE.
 
lpSecurityAttributes If you want the console to be inherited, the bInheritHandle member of the SECURITY_ATTRIBUTES structure must be TRUE.
dwCreationDisposition You should specify OPEN_EXISTING when using CreateFile to open the console.
dwFlagsAndAttributes Ignored.
hTemplateFile Ignored.

The following list shows the effects of various settings of dwDesiredAccess and lpFileName.

lpFileName dwDesiredAccess Result
CON GENERIC_READ Opens console for input.
CON GENERIC_WRITE Opens console for output.
CON GENERIC_READ GENERIC_WRITE Causes CreateFile to fail; GetLastError returns ERROR_FILE_NOT_FOUND.

Windows Me/98/95:  Causes CreateFile to fail; GetLastError returns ERROR_PATH_NOT_FOUND.

 


Mailslots

If CreateFile opens the client end of a mailslot, the function returns INVALID_HANDLE_VALUE if the mailslot client attempts to open a local mailslot before the mailslot server has created it with the CreateMailSlot function. For more information about mailslots, see Mailslots.


Pipes

If CreateFile opens the client end of a named pipe, the function uses any instance of the named pipe that is in the listening state. The opening process can duplicate the handle as many times as required but, once opened, the named pipe instance cannot be opened by another client. The access specified when a pipe is opened must be compatible with the access specified in the dwOpenMode parameter of the CreateNamedPipe function. For more information about pipes, see Pipes.

ssffeng
驱动牛犊
驱动牛犊
  • 注册日期2003-11-06
  • 最后登录2005-04-08
  • 粉丝0
  • 关注0
  • 积分0分
  • 威望0点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
地板#
发布于:2004-04-14 11:37
在那儿能找到这方面的资料?
hellangel
驱动中牛
驱动中牛
  • 注册日期2004-02-16
  • 最后登录2016-04-19
  • 粉丝0
  • 关注0
  • 积分1002分
  • 威望236点
  • 贡献值0点
  • 好评度205点
  • 原创分1分
  • 专家分0分
地下室#
发布于:2004-04-15 09:34
msdn
春眠不觉晓,处处闻啼鸟。 夜来风雨声,花落知多少?
游客

返回顶部