28.2 Using the Comm Functions

28.2.1 Getting a Handle to a Communications Resource

An application uses CreateFile to open a handle to a communications resource. If the specified device is currently being used by another process, CreateFile will return INVALID_HANDLE_VALUE. The security attributes argument to CreateFile determines whether the handle can be inherited by child processes. Another option in this call is to open the handle for overlapped I/O by specifying the FILE_FLAG_OVERLAPPED flag. This allows the application to initiate an I/O operation (read, for example) and then proceed with other operations while the read executes in the background. Overlapped I/O is also necessary if you have more than one thread performing I/O operations, or if you want one thread to do an I/O operation while another thread is blocked calling WaitCommEvent.

The CreateFile call opens a handle to a communications resource that has been initialized and configured according to the values that were set up the last time the resource was opened. If the device has never been opened, it is configured using the system defaults. This allows settings specified by a MODE command to be retained when the device is reopened. The values inherited from the previous open include the configuration parameters of the Device Control Block (DCB) and the timeout values used in I/O operations.