Device input and output, that is, device I/O, can be divided into two modes: synchronous and asynchronous. For synchronous device I/O, the called API function is always returned after the device I/O is complete. Asynchronous device I/O can be implemented in multiple ways, but the fundamental principle is to get the "device I/O completion notification ".
This topic describes how to enable and disable a device. Note that the device here is not an entity like a keyboard or display. It is an abstract concept. It refers to an object that communicates with the outside world. It can accept external input and respond to external requests. It is called device I/O. This concept is abstract. These devices are often associated with a kernel object. To open these devices, you need to create related kernel objects.
These devices include files, directories, Logical Disk drivers, physical disk drivers, serial ports, parallel ports, mail slots, pipelines, sockets, And the console (as shown in the following table ):
Device |
Main Purpose |
File |
Save data |
Directory |
Attribute and File compression settings |
Logical Disk Drive |
Disk formatting |
Physical disk drive |
Access a partitioned table |
Serial Port |
Serial Data Transmission |
Parallel Port |
Multi-digit data transmission at the same time, mainly to transmit data to the printer |
Mail Trough |
One-to-multiple transmission of data is often applicable to a computer in a network that sends data to other machines. |
Named Pipe |
One-to-one data transmission is often applicable to a computer in a network that sends data to other machines. |
Anonymous Pipeline |
One-to-one data transmission, applicable to simple data transmission and not to networks |
Socket |
Send data in the form of a stream or datagram, suitable for communication in a network |
Console |
Buffer for display in a text window |
To use these devices, you must first enable them.
Windows tries to hide the differences between these devices. Therefore, the I/O operations on many devices can be completed through the same API function, as shown in the following table:
Device |
API functions and usage that are often used to open Devices |
File |
Createfile -- enable the device function. Set the parameter pszname to a file path name. |
Directory |
Createfile -- enable the device function. Set the parameter pszname to a directory name. Windows allows you to open a directory and call the createfile function by using the file _ flag_backup_semantics flag. After the directory is opened, it can be a directory attribute, that is, a folder attribute, such as normal, hidden, system, and read-only. |
Logical Disk Drive |
Createfile -- enable the device function. Set the parameter pszname to the string "\. \ x :". For example, to open the C drive, set it to \. \ c :". |
Physical disk drive |
Createfile -- enable the device function. Set the parameter pszname to \. \ physicaldrivex ". For example, open the first physical hard disk sector: You can call the createfile function as follows: Createfile (text ("\. \ physicaldrive0 "),...); In this way, you can open a physical disk drive and directly access the hard disk partition table. However, opening a physical disk driver is potentially dangerous, especially when a wrong write operation causes damage to the content of the physical disk. |
Serial Port |
Createfile -- enable the device function. Set the parameter pszname to "comx". For example, to enable the COM1 serial port device, set it to "COM1 ". |
Parallel Port |
Createfile -- enable the device function. Set the parameter pszname to "lptx". For example, open the LPT1 parallel port and set it to "LPT1 ". |
Mailbox (server) |
Createmailslot -- enable the device function. Set the parameter pszname to "\. \ Mailslot \ mailslotname", where "mailsoltname" is the name of the mail slot, which can be arbitrary. The preceding string is fixed. |
Mail Trough (client) |
Createfile -- enable the device function. Set the parameter pszname to "\ servername \ Mailslot \ mailslotname", where "mailsoltname" is the name of the mail slot and can be any. The preceding string is fixed. |
Named Pipe (Server) |
Createfile -- enable the device function. Set the parameter pszname to "\. \ PIPE \ pipename", where "pipename" is the name of the named pipe, which can be arbitrary and the previous string is fixed. |
Named Pipe (Client) |
Createfile -- enable the device function. Set the parameter pszname to "\ servername \ PIPE \ pipename", where "pipename" is the name of the named pipe, which can be arbitrary and the previous string is fixed. |
Anonymous Pipeline |
Createpipe -- enable the device function. Both the client and the server use this function to create or open an anonymous pipeline. |
Socket |
Socket -- create a socket descriptor accept, or acceptex. |
Console Console |
Createconsolescreenbuffer, getstdhandle -- enable the device function |
From the table above, we can find that many devices are created and opened using the createfile function. This function will be discussed later.
When the device is turned on, you get a device handle and you can use other functions to set the device.
For example, if a serial port is opened, you need to set its Transmission baud rate:
Bool setcommconfig (
Handle hcommdev,
Lpcommconfig PCC,
DWORD dwsize );
Alternatively, you have obtained a mail slot handle and can set the wait time for reading data:
Bool setmailslotinfo (
Handle hmailslot,
DWORD dwreadtimeout );
Finally, do not forget to close the handle to close the device correctly:
Bool closehandle (handle hobject ); Int closesocket (socket S); // closes a socket.
If you have a device handle, you can check the device type. By using the getfiletype function, the return value of this function indicates the type of device. For details, refer to msdn.
DWORD getfiletype (handle hdevice );
Now let's discuss the createfile function:
Handle createfile (
Pctstr pszname, // specifies the device type or a specific device entity
DWORD dwdesiredaccess, // access restriction
DWORD dw1_mode, // Sharing Mode
Psecurity_attributes PSA, // Security description Structure
DWORD dwcreationdisposition, // creation and opening Methods
DWORD dwflagsandattributes, // attribute flag, which is related to the buffer zone and file operation attributes
Handle hfiletemplate); // device template, a device handle
If the function succeeds, a handle is returned. If the function fails, invalid_handle_value (value:-1) is returned ).
If the last parameter hfiletemplate is set, create a device with the same properties according to the device represented by this parameter. Of course, the device indicated by this parameter must have the "readable" permission, that is, the "generic_read" permission.
For the specific usage of this function, refer to this book or msdn.