DeviceIoControl

若这个参数为0,则对象将以独占的方式打开。(通常serial port 是以这种方式)
CreateFile
dwShareMode
If this parameter is 0 (zero) and CreateFile succeeds, the object cannot be shared and cannot
be opened again until the handle is closed

下面是打开一个串口的举例:
 // Open the serial port.
 m_hPort = CreateFile (TEXT("Com1:"), // Pointer to the name of the port
  GENERIC_READ | GENERIC_WRITE, // Access (read-write) mode
  0,            // Share mode
  NULL,         // Pointer to the security attribute
  OPEN_EXISTING,// How to open the serial port
  0,            // Port attributes
  NULL);        // Handle to port with attribute to copy


下面是获取sdcard ID 的方法
 HANDLE hDSK = CreateFile(_T("DSK2:"),
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);
 if(hDSK!=INVALID_HANDLE_VALUE)
 {
 
 
  ok = DeviceIoControl(hDSK,//hDSK 是SDCARD 的设备句柄
   IOCTL_DISK_GET_STORAGEID,
   NULL,
   0,
   buf,//获取ID的缓冲区
   sizeof(buf),
   &c,
   NULL);
 
  CloseHandle(hDSK);
 }

DeviceIoControl

The DeviceIoControl function sends a control code directly to a specified device driver,
causing the corresponding device to perform the corresponding operation.


BOOL DeviceIoControl(
  HANDLE hDevice,
  DWORD dwIoControlCode,
  LPVOID lpInBuffer,
  DWORD nInBufferSize,
  LPVOID lpOutBuffer,
  DWORD nOutBufferSize,
  LPDWORD lpBytesReturned,
  LPOVERLAPPED lpOverlapped
);

ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.WIN32COM.v10.en/devio/base/calling_deviceiocontrol.htm

DeviceIoControl can accept a handle to a specific device. For example, to open a handle to the logical drive A: with

CreateFile, specify //./a:. Alternatively, you can use the names //./PhysicalDrive0, //./PhysicalDrive1, and so on, to open

handles to the physical drives on a system.
//获取一个句柄有两种方法,一种是直接指定设备的逻辑驱动名,另一种指定打开设备的物理驱动名称
Windows Me/98/95:  DeviceIoControl can only accept a handle to a virtual device driver. For example, to open a handle to the

system VxD with CreateFile, specify //./vwin32.

//应该以共享的方式打开设备,当打开串口是一个例外。
You should specify the FILE_SHARE_READ and FILE_SHARE_WRITE access flags when calling CreateFile to open a handle to a device

driver. However, when you open a communications resource, such as a serial port, you must specify exclusive access.


下面的DeviceToControl的control code(DeviceToControl 是通过向设备驱动程序发送control code来控制设备的)
For lists of supported control codes, see the following topics:
Communications Control Codes
Device Management Control Codes
Directory Management Control Codes
Disk Management Control Codes
File Management Control Codes
Power Management Control Codes
Volume Management Control Codes

下面是调用DeviceToControl的实例

Calling DeviceIoControl

An application can use the DeviceIoControl function to perform direct input and output operations on, or retrieve information

about, a floppy disk drive, hard disk drive, tape drive, or CD-ROM drive. The following example demonstrates how to retrieve

information about the first physical drive in the system. It uses the CreateFile function to retrieve the device handle to

the first physical drive, and then uses DeviceIoControl with the IOCTL_DISK_GET_DRIVE_GEOMETRY control code to fill a

DISK_GEOMETRY structure with information about the drive.


/* The code of interest is in the subroutine GetDriveGeometry. The
   code in main shows how to interpret the results of the call. */

#include <windows.h>
#include <winioctl.h>
#include <stdio.h>

//得到一个磁盘设备分区状况.
IOCTL_DISK_GET_DRIVE_GEOMETRY
The IOCTL_DISK_GET_DRIVE_GEOMETRY DeviceIoControl operation returns information about the physical disk's geometry: type,

number of cylinders, tracks per cylinder, sectors per track, and bytes per sector.

BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)
{
  HANDLE hDevice;               // handle to the drive to be examined
  BOOL bResult;                 // results flag
  DWORD junk;                   // discard results

  hDevice = CreateFile(".//PhysicalDrive0",  // drive to open通过物理设备名的方式打开设备。
                    0,                // no access to the drive
                    FILE_SHARE_READ | // share mode
                    FILE_SHARE_WRITE,
                    NULL,             // default security attributes
                    OPEN_EXISTING,    // disposition
                    0,                // file attributes
                    NULL);            // do not copy file attributes

  if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
  {
    return (FALSE);
  }

  bResult = DeviceIoControl(hDevice,  // device to be queried
      IOCTL_DISK_GET_DRIVE_GEOMETRY,  // operation to perform
                             NULL, 0, // no input buffer
                            pdg, sizeof(*pdg),     // output buffer
                            &junk,                 // # bytes returned
                            (LPOVERLAPPED) NULL);  // synchronous I/O

  CloseHandle(hDevice);

  return (bResult);
}

int main(int argc, char *argv[])
{
  DISK_GEOMETRY pdg;            // disk drive geometry structure
  BOOL bResult;                 // generic results flag
  ULONGLONG DiskSize;           // size of the drive, in bytes

  bResult = GetDriveGeometry (&pdg);

  if (bResult)
  {
    printf("Cylinders = %I64d/n", pdg.Cylinders);
    printf("Tracks/cylinder = %ld/n", (ULONG) pdg.TracksPerCylinder);
    printf("Sectors/track = %ld/n", (ULONG) pdg.SectorsPerTrack);
    printf("Bytes/sector = %ld/n", (ULONG) pdg.BytesPerSector);

    DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
      (ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
    printf("Disk size = %I64d (Bytes) = %I64d (Gb)/n", DiskSize,
           DiskSize / (1024 * 1024 * 1024));
  }
  else
  {
    printf ("GetDriveGeometry failed. Error %ld./n", GetLastError ());
  }

  return ((int)bResult);

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值