10.1 turning the device on and off
10.1.1 The definition of a device -anything that can be communicated with in Windows.
(1) common equipment and uses
Equipment |
Use |
function to open the device |
File |
Permanently store any data |
CreateFile (Pszname is a pathname or a UNC path name) |
Directory |
Settings for properties and file compression |
Ditto, if you specify the FILE_FLAG_BACKUP_SEMANTICS flag, a directory opens. You can change the properties of a directory (such as normal or hidden) and their timestamps |
Logical Disk Drives |
Format Drive |
CreateFile (Pszname is \\.\x:) where x is the drive letter. can be used to format the drive or detect the size of the drive medium |
Physical Disk Drives |
accessing partitioned Tables |
CreateFile (Pszname is \\.\physicaldrivex:) opens the physical drive, whose x is the letter of the physical drive. such as \\.\PHYSICALDRIVE0, represents the first physical drive |
Serial |
Transmitting data over a telephone line |
CreateFile (Pszname for "COMx") |
and the port |
Transferring data to the printer |
CreateFile (Pszname for "LPTx") |
Mail slots |
One-to-many data transfer, usually via a network to another running Windows machine |
Mail slot server: Createmailslot (pszname for "\\.\mailslot\mailslotname") Mail Slot client: Createmailslot (pszname as "\\servername\mailslot\mailslotname") |
Named pipes |
Single-to-one data transfer, usually via a network to another machine running Windows |
Server side: Createnamepipe (Pszname for "\\.\pipe\pipename") Client: CreateFile (Pszname for "\\servername\pipe\pipename") |
Anonymous pipeline |
One-to-one data transfer on a single machine (never Across networks) |
CreatePipe used to open servers and clients |
Sockets |
Transmission of a message or data stream, usually via a network to any machine that supports the use of a word (the machine does not necessarily run the Windows operating system |
Socket, accept, or AcceptEx |
Control Desk |
Screen caching in the text window |
Createconsolescreenbuffer or GetStdHandle |
(2) Set the parameters for communication with the device, such as Setcommconfig set the baud rate of the serial port; setmailslotinfo sets the timeout value for the time-out message slot.
(3) Turn off communication: For most devices, call CloseHandle, but socket with closesocket
(4) Get the device type: Getfiletype, the return value is as follows
①file_type_unknown: Unknown type ②file_type_disk: Disk File
③file_type_char: Character file, commonly referred to as a common port and device or console
④file_type_pipe: Named pipes or anonymous pipelines
10.1.2 Look at CreateFile
(1) CreateFile function
Parameters |
Describe |
Pszname |
Can be either a device type or an instance of a device of that class |
dwDesiredAccess |
How to transfer data 0: Can only change the configuration of the device, can not read data or write data Generic_read: Allow read-only access to the device Generic_write: Write-only access (note not readable) generic_read| Generic_write: Readable and writable |
dwShareMode |
Specify shared privileges for Devices ①0: Exclusive access to devices . The CreateFile call fails if the device is already open. Similarly, if we successfully open the device, then the subsequent CreateFile will fail. ②file_share_read: Shared Read Only (cannot modify device data) A, Our CreateFile will fail if the device has been opened in write-only or exclusive mode. B, if we open successfully, subsequent calls to CreateFile using Generic_write will fail. ②file_share_write: share only write (cannot read device data) A, Our CreateFile will fail if the device has been opened in read-only or exclusive mode. B, if we open successfully, subsequent calls to CreateFile using Generic_read will fail. ③file_share_write| File_share_read: Shared read/write A, our CreateFile will fail if the device is already open exclusively. B, if we open successfully, then the subsequent requirements Exclusive Read ( is dwsharemode=0, Span style= "color: #ff0000;" >dwdesiredaccess=generic_read), exclusive write , or exclusive read/write call CreateFile will fail. ④file_share_delete: When you delete or move a file, it is flagged for deletion and is actually deleted when all open handles are closed. |
Psa |
The security attribute, usually set to NULL, Returns a handle that is not inheritable by default. |
Dwcreatedisposition |
The meaning of the file is greater than the type of other devices Create_new: Create a new file, CreateFile fails if there is a file with the same name Create_always: Creates a new file, overwriting the old file, regardless of whether it has the same name open_existing: Open the file or device that you own, and if it does not, CreateFile fails. If you are opening a device other than a file, you must set the flag. Open_always: If the file already exists, open it directly. If it does not exist, it is created and then opened. Truncate_exisiting: Open your own file and truncate the file size to 0 |
dwFlagsAndAttributes |
Used to fine-tune communication with the device (if it is a file, set the properties of the file)( see 2nd later analysis ) |
Hfiletemplate |
can be null, or it can be a file handle ① If Hfiletemplate identifies a file handle, the dwFlagsAndAttributes parameter is ignored and the Hfiletemplate template file's properties are used instead, but hfiletemplate must first be generic_ Read Open. ② hfiletemplate is ignored if CreateFile is to be opened instead of creating a new file |
return value |
Success: Return file or device handle Failure: Invalid_handle_value (-1), note not null! |
10th. Synchronizing device I/O and asynchronous device I/O (1)