CreateFile
: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
, the dwShareMode
parameter must be zero (exclusive access), and the hTemplateFile
parameter must be NULL
. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.DuplicateHandle
and pass that to whoever you want to grant access to the port after you've opened it. For further reading, check out this ancient article from MSDNCreateFile
for some name return INVALID_HANDLE_VALUE
- this mean that this name not exist. but this is of course false, because CreateFile
can fail by different reasons. you need call GetLastError
after fail. only if it return ERROR_FILE_NOT_FOUND
the name really not exist (ERROR_PATH_NOT_FOUND
can not be for '.COMX'
because path here always exist and correct). for com devices very common error was STATUS_ACCESS_DENIED
- because it have DO_EXCLUSIVE
flag. with this flag only one file on device can be open at a time. GUID_DEVINTERFACE_COMPORT
via CM_Get_Device_Interface_ListW
?ACPI#PNP0501#0#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
is symbolic link to PDO device Device00000034
(created by aspi.sys) and it have not DO_EXCLUSIVE
flag. despite this on second call of CreateFile i got access denied error. to this device FDO - DeviceSerial0
(?COM1
symbolic link to it) attached. it already have DO_EXCLUSIVE
flag. anyway SerialCreateOpen
(IRP_MJ_CREATE
procedure serial.sys) denied access create more than one file - at very begin in increment some counter in device extension, and if it != 1 - return STATUS_ACCESS_DENIED
?ACPI#PNP0501#0#{86e0d1e0-8089-11d0-9ce4-08003e301f73}
) which not exclusive device (setting the exclusive flag for the FDO has no effect here) - the create request begin execute on stack top from DeviceSerial0
and serial.sysenforce exclusivity themselves within theirSerialCreateOpen
routine.