CTL_CODE定义说明

来源:互联网 发布:毛利润算法 编辑:程序博客网 时间:2024/05/17 06:31

我们在说DeviceIoControl函数时其第二个参数dwIoControlCode就是由CTL_CODE宏定义的,下边我们可以了解一下CTL_CODE的内容。

CTL_CODE:用于创建一个唯一的32位系统I/O控制代码,这个控制代码包括4部分组成:DeviceType(设备类型,高16位(16-31位)),Access(访问限制,14-15位),Function(功能 2-13位),Method(I/O访问内存使用方式)。

This macro creates a unique system I/O control code (IOCTL).

#define CTL_CODE(DeviceType, Function, Method, Access) ( ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method))

Parameters(参数)

 

DeviceType
Defines the type of device for the given IOCTL.

This parameter can be no bigger than a WORD value.

The values used by Microsoft are in the range 0-32767; the values 32768-65535 are reserved for use by OEMs and IHVs.

The following device types are defined by the system:

  • FILE_DEVICE_BEEP
  • FILE_DEVICE_CD_ROM
  • FILE_DEVICE_CD_ROM_FILE_SYSTEM
  • FILE_DEVICE_CONTROLLER
  • FILE_DEVICE_DATALINK
  • FILE_DEVICE_DFS
  • FILE_DEVICE_DISK
  • FILE_DEVICE_DISK_FILE_SYSTEM
  • FILE_DEVICE_FILE_SYSTEM
  • FILE_DEVICE_INPORT_PORT
  • FILE_DEVICE_KEYBOARD
  • FILE_DEVICE_MAILSLOT
  • FILE_DEVICE_MIDI_IN
  • FILE_DEVICE_MIDI_OUT
  • FILE_DEVICE_MOUSE
  • FILE_DEVICE_MULTI_UNC_PROVIDER
  • FILE_DEVICE_NAMED_PIPE
  • FILE_DEVICE_NETWORK
  • FILE_DEVICE_NETWORK_BROWSER
  • FILE_DEVICE_NETWORK_FILE_SYSTEM
  • FILE_DEVICE_NULL
  • FILE_DEVICE_PARALLEL_PORT
  • FILE_DEVICE_PHYSICAL_NETCARD
  • FILE_DEVICE_PRINTER
  • FILE_DEVICE_SCANNER
  • FILE_DEVICE_SERIAL_MOUSE_PORT
  • FILE_DEVICE_SERIAL_PORT
  • FILE_DEVICE_SCREEN
  • FILE_DEVICE_SOUND
  • FILE_DEVICE_DEVICE_STREAMS
  • FILE_DEVICE_TAPE
  • FILE_DEVICE_TAPE_FILE_SYSTEM
  • FILE_DEVICE_TRANSPORT
  • FILE_DEVICE_UNKNOWN
  • FILE_DEVICE_VIDEO
  • FILE_DEVICE_VIRTUAL_DISK
  • FILE_DEVICE_WAVE_IN
  • FILE_DEVICE_WAVE_OUT
  • FILE_DEVICE_8042_PORT
  • FILE_DEVICE_NETWORK_REDIRECTOR
  • FILE_DEVICE_BATTERY
  • FILE_DEVICE_BUS_EXTENDER
  • FILE_DEVICE_MODEM
  • FILE_DEVICE_VDM
  • FILE_DEVICE_MASS_STORAGE
  • FILE_DEVICE_SMB
  • FILE_DEVICE_KS
  • FILE_DEVICE_CHANGER
  • FILE_DEVICE_SMARTCARD
  • FILE_DEVICE_ACPI
  • FILE_DEVICE_DVD
  • FILE_DEVICE_FULLSCREEN_VIDEO
  • FILE_DEVICE_DFS_FILE_SYSTEM
  • FILE_DEVICE_DFS_VOLUME

The following device types are specific to Windows CE:

  • FILE_DEVICE_HAL
  • FILE_DEVICE_CONSOLE
  • FILE_DEVICE_PSL
  • FILE_DEVICE_SERVICE
Function
Defines an action within the device category.

Function codes 0-2047 are reserved for Microsoft; codes 2048-4095 are reserved for OEMs and IHVs.

A function code can be no larger then 4095.

Method
Defines the method codes for how buffers are passed for I/O and file system controls.

The following values are possible for this parameter:

  • METHOD_BUFFERED
  • METHOD_IN_DIRECT
  • METHOD_OUT_DIRECT
  • METHOD_NEITHER

This field is ignored by Windows CE. You should always use the METHOD_BUFFERED value unless compatibility with Windows-based desktop platforms is required using a different Method value.

Access
Defines the access check value for any access.

The following table shows the possible flags for this parameter. The FILE_ACCESS_ANY is generally the correct value.

 

FlagDescriptionFILE_ANY_ACCESSRequest all access.FILE_READ_ACCESSRequest read access. Can be used with FILE_WRITE_ACCESS.FILE_WRITE_ACCESSRequest write access. Can be used with FILE_READ_ACCESS.

Return Values(返回值)

None.

Remarks(备注)

The macro can be used for defining IOCTL and FSCTL function control codes. All IOCTLs must be defined this way to ensure that values used by Microsoft, OEMs, and IHVs do not overlap.

The following illustration shows the format of the resulting IOCTL.

 

举例说明一下:

我定义两个IOCTL,一个用于对设备的读,一个用于对设备的写

#define ATST2004_IOCTL_READ CTL_CODE(FILE_DEVICE_UNKNOWN, 0x800, METHOD_BUFFERED, FILE_READ_DATA)

#define ATST2004_IOCTL_WRITE CTL_CODE(FILE_DEVICE_UNKNOWN, 0x801, METHOD_BUFFERED, FILE_WRITE_DATA)

在VC中使用不需要进行处理,假如我要在VB中使用这两个IOCTL,就需要进行查值计算了,计算后定义如下:

Private Const ATST2004_IOCTL_READ = &H226000

Private Const ATST2004_IOCTL_WRITE = &H22A004

关于Method(I/O访问内存使用方式),我们下篇在说。

 

/*-------------------------------------------------------------------------------------------------------------------------------*/

 

CTL_CODE(DeviceType, Function, Method, Access)

Supply the following parameters to the CTL_CODE macro:

DeviceType 
Identifies the device type. This value must match the value that is set in the DeviceType member of the driver's DEVICE_OBJECT structure. (See Specifying Device Types). Values of less than 0x8000 are reserved for Microsoft. Values of 0x8000 and higher can be used by vendors. Note that the vendor-assigned values set the Common bit. 

第2个参数:Function 
Identifies the function to be performed by the driver. Values of less than 0x800 are reserved for Microsoft. Values of 0x800 and higher can be used by vendors. Note that the vendor-assigned values set the Custom bit. 
TransferType 
Indicates how the system will pass data between the caller of DeviceIoControl (or IoBuildDeviceIoControlRequest) and the driver that handles the IRP. 
Use one of the following system-defined constants:

METHOD_BUFFERED 
Specifies the buffered I/O method, which is typically used for transferring small amounts of data per request. Most I/O control codes for device and intermediate drivers use this TransferType value. 
For information about how the system specifies data buffers for METHOD_BUFFERED I/O control codes, see Buffer Descriptions for I/O Control Codes.

For more information about buffered I/O, see Using Buffered I/O.

METHOD_IN_DIRECT or METHOD_OUT_DIRECT 
Specifies the direct I/O method, which is typically used for reading or writing large amounts of data, using DMA or PIO, that must be transferred quickly.
Specify METHOD_IN_DIRECT if the caller of DeviceIoControl or IoBuildDeviceIoControlRequest will pass data to the driver.

Specify METHOD_OUT_DIRECT if the caller of DeviceIoControl or IoBuildDeviceIoControlRequest will receive data from the driver.

For information about how the system specifies data buffers for METHOD_IN_DIRECT and METHOD_OUT_DIRECT I/O control codes, see Buffer Descriptions for I/O Control Codes.

For more information about direct I/O, see Using Direct I/O.

METHOD_NEITHER 
Specifies neither buffered nor direct I/O. The I/O manager does not provide any system buffers or MDLs. The IRP supplies the user-mode virtual addresses of the input and output buffers that were specified to DeviceIoControl or IoBuildDeviceIoControlRequest, without validating or mapping them.
For information about how the system specifies data buffers for METHOD_NEITHER I/O control codes, see Buffer Descriptions for I/O Control Codes.

This method can be used only if the driver can be guaranteed to be running in the context of the thread that originated the I/O control request. Only a highest-level kernel-mode driver is guaranteed to meet this condition, so METHOD_NEITHER is seldom used for the I/O control codes that are passed to low-level device drivers. 

With this method, the highest-level driver must determine whether to set up buffered or direct access to user data on receipt of the request, possibly must lock down the user buffer, and must wrap its access to the user buffer in a structured exception handler (see Handling Exceptions). Otherwise, the originating user-mode caller might change the buffered data before the driver can use it, or the caller could be swapped out just as the driver is accessing the user buffer. 

For more information, see Using Neither Buffered Nor Direct I/O.

RequiredAccess 
Indicates the type of access that a caller must request when opening the file object that represents the device (see IRP_MJ_CREATE). The I/O manager will create IRPs and call the driver with a particular I/O control code only if the caller has requested the specified access rights. RequiredAccess is specified by using the following system-defined constants:
FILE_ANY_ACCESS 
The I/O manager sends the IRP for any caller that has a handle to the file object that represents the target device object. 
FILE_READ_DATA 
The I/O manager sends the IRP only for a caller with read access rights, allowing the underlying device driver to transfer data from the device to system memory. 
FILE_WRITE_DATA 
The I/O manager sends the IRP only for a caller with write access rights, allowing the underlying device driver to transfer data from system memory to its device. 
FILE_READ_DATA and FILE_WRITE_DATA can be ORed together if the caller must have both read and write access rights.

原创粉丝点击