|
以下内容含脚本,或可能导致页面不正常的代码
<h1 class="title_txt"><img height="16" alt="原创" src="http://blog.csdn.net/images/authorship.gif" width="15" border="0"/> 实战DeviceIoControl 之一:通过API访问设备驱动程序<cite class="fav_csdnstylebykimi"><a class="fav_csdnstylebykimi" title="收藏到我的网摘中,并分享给我的朋友" href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();">收藏</a></cite></h1>
<div class="blogstory">
<style>
P.bhw98
{
PADDING-RIGHT: 0px;
PADDING-LEFT: 0px;
FONT-SIZE: 9pt;
PADDING-BOTTOM: 0px;
MARGIN: 10px 0px 5px;
LINE-HEIGHT: normal;
PADDING-TOP: 0px;
FONT-FAMILY: Verdana, Arial
}
PRE.bhw98
{
FONT-SIZE: 9pt;
PADDING-RIGHT: 5px;
PADDING-LEFT: 5px;
PADDING-BOTTOM: 5px;
MARGIN: 5px 0px;
LINE-HEIGHT: normal;
PADDING-TOP: 5px;
BACKGROUND-COLOR: #f0f0f0
}
PRE.diag
{
FONT-SIZE: 9pt;
PADDING-RIGHT: 5px;
PADDING-LEFT: 5px;
PADDING-BOTTOM: 5px;
MARGIN: 5px 0px;
LINE-HEIGHT: normal;
PADDING-TOP: 5px;
}
CODE.bhw98
{
FONT-SIZE: 9pt;
COLOR: #000000
}
TABLE.bhw98
{
BORDER-RIGHT: #808080 1px solid;
BORDER-TOP: #808080 1px solid;
FONT-SIZE: 9pt;
MARGIN: 3px 0px 10px;
BORDER-LEFT: #808080 1px solid;
LINE-HEIGHT: normal;
BORDER-BOTTOM: #808080 1px solid;
FONT-FAMILY: Verdana, Arial
}
TD.bhw98
{
BORDER-RIGHT: darkgray 1px solid;
PADDING-RIGHT: 10px;
BORDER-TOP: darkgray 1px solid;
PADDING-LEFT: 5px;
FONT-SIZE: 9pt;
PADDING-BOTTOM: 0px;
MARGIN: 0px;
BORDER-LEFT: darkgray 1px solid;
LINE-HEIGHT: normal;
PADDING-TOP: 3px;
BORDER-BOTTOM: darkgray 1px solid;
FONT-FAMILY: Verdana, Arial;
BACKGROUND-COLOR: #f0f0f0
}
STRONG.bhw98
{
FONT-WEIGHT: bolder;
FONT-SIZE: 20pt;
COLOR: #228b22;
FONT-STYLE: italic;
FONT-FAMILY: Verdana, Arial
}
LI.bhw98
{
FONT-SIZE: 9pt;
MARGIN: 3px 0px 0px 3px;
LINE-HEIGHT: normal;
FONT-FAMILY: Verdana, Arial
}
H1.bhw98
{
MARGIN-TOP: 25px;
FONT-WEIGHT: bolder;
FONT-SIZE: 12pt;
MARGIN-BOTTOM: 5px;
LINE-HEIGHT: normal;
FONT-FAMILY: Verdana, Arial
}
H2.bhw98
{
MARGIN-TOP: 20px;
FONT-WEIGHT: bolder;
FONT-SIZE: 10.5pt;
MARGIN-BOTTOM: 5px;
LINE-HEIGHT: normal;
FONT-FAMILY: Verdana, Arial
}
H3.bhw98
{
MARGIN-TOP: 15px;
FONT-WEIGHT: bolder;
FONT-SIZE: 9pt;
MARGIN-BOTTOM: 5px;
LINE-HEIGHT: normal;
FONT-FAMILY: Verdana, Arial
}
SPAN.key
{
COLOR: #0000ff
}
SPAN.num
{
COLOR: #800000
}
SPAN.str
{
COLOR: #8b008b
}
SPAN.rem
{
COLOR: #008000
}
</style>
<p class="bhw98"><strong class="bhw98">Q</strong> 在NT/2000/XP中,我想用VC编写应用程序访问硬件设备,如获取磁盘参数、读写绝对扇区数据、测试光驱实际速度等,该从哪里入手呢?
<p class="bhw98"><strong class="bhw98">A</strong> 在NT/2000/XP中,应用程序可以通过API函数DeviceIoControl来实现对设备的访问—获取信息,发送命令,交换数据等。利用该接口函数向指定的设备驱动发送正确的控制码及数据,然后分析它的响应,就可以达到我们的目的。
<p class="bhw98">DeviceIoControl的函数原型为 <pre class="bhw98"><code class="bhw98">BOOL DeviceIoControl(
HANDLE hDevice, <span class="rem">// 设备句柄</span>
DWORD dwIoControlCode, <span class="rem">// 控制码</span>
LPVOID lpInBuffer, <span class="rem">// 输入数据缓冲区指针</span>
DWORD nInBufferSize, <span class="rem">// 输入数据缓冲区长度</span>
LPVOID lpOutBuffer, <span class="rem">// 输出数据缓冲区指针</span>
DWORD nOutBufferSize, <span class="rem">// 输出数据缓冲区长度</span>
LPDWORD lpBytesReturned, <span class="rem">// 输出数据实际长度单元长度</span>
LPOVERLAPPED lpOverlapped <span class="rem">// 重叠操作结构指针</span>
);
</code></pre>
<p class="bhw98">设备句柄用来标识你所访问的设备。
<p class="bhw98">发送不同的控制码,可以调用设备驱动程序的不同类型的功能。在头文件winioctl.h中,预定义的标准设备控制码,都以IOCTL或FSCTL开头。例如,IOCTL_DISK_GET_DRIVE_GEOMETRY是对物理驱动器取结构参数(介质类型、柱面数、每柱面磁道数、每磁道扇区数等)的控制码,FSCTL_LOCK_VOLUME是对逻辑驱动器的卷加锁的控制码。
<p class="bhw98">输入输出数据缓冲区是否需要,是何种结构,以及占多少字节空间,完全由不同设备的不同操作类型决定。在头文件winioctl.h中,已经为标准设备预定义了一些输入输出数据结构。重叠操作结构指针设置为NULL,DeviceIoControl将进行阻塞调用;否则,应在编程时按异步操作设计。
<p class="bhw98"><strong class="bhw98">Q</strong> 设备句柄是从哪里获得的?
<p class="bhw98"><strong class="bhw98">A</strong> 设备句柄可以用API函数CreateFile获得。它的原型为 <pre class="bhw98"><code class="bhw98">HANDLE CreateFile(
LPCTSTR lpFileName, <span class="rem">// 文件名/设备路径</span>
DWORD dwDesiredAccess, <span class="rem">// 访问方式</span>
DWORD dwShareMode, <span class="rem">// 共享方式</span>
LPSECURITY_ATTRIBUTES lpSecurityAttributes, <span class="rem">// 安全描述符指针</span>
DWORD dwCreationDisposition, <span class="rem">// 创建方式</span>
DWORD dwFlagsAndAttributes, <span class="rem">// 文件属性及标志</span>
HANDLE hTemplateFile <span class="rem">// 模板文件的句柄</span>
);
</code></pre>
<p class="bhw98">CreateFile这个函数用处很多,这里我们用它“打开”设备驱动程序,得到设备的句柄。操作完成后用CloseHandle关闭设备句柄。
<p class="bhw98">与普通文件名有所不同,设备驱动的“文件名”(常称为“设备路径”)形式固定为“\\.\DeviceName”(注意在C程序中该字符串写法为“\\\\.\\DeviceName”),DeviceName必须与设备驱动程序内定义的设备名称一致。
<p class="bhw98">一般地,调用CreateFile获得设备句柄时,访问方式参数设置为0或GENERIC_READ|GENERIC_WRITE,共享方式参数设置为FILE_SHARE_READ|FILE_SHARE_WRITE,创建方式参数设置为OPEN_EXISTING,其它参数设置为0或NULL。
<p class="bhw98"><strong class="bhw98">Q</strong> 可是,我怎么知道设备名称是什么呢?
<p class="bhw98"><strong class="bhw98">A</strong> 一些存储设备的名称是微软定义好的,不可能有什么变化。大体列出如下
<table class="bhw98" align="center">
<tbody>
<tr>
<td class="bhw98">软盘驱动器
<td class="bhw98">A:, B:
<tr>
<td class="bhw98">硬盘逻辑分区
<td class="bhw98">C:, D:, E:, ...
<tr>
<td class="bhw98">物理驱动器
<td class="bhw98">PHYSICALDRIVEx
<tr>
<td class="bhw98">CD-ROM, DVD/ROM
<td class="bhw98">CDROMx
<tr>
<td class="bhw98">磁带机
<td class="bhw98">TAPEx </td></tr></tbody></table>
<p class="bhw98">其中,物理驱动器不包括软驱和光驱。逻辑驱动器可以是IDE/SCSI/PCMCIA/USB接口的硬盘分区(卷)、光驱、MO、CF卡等,甚至是虚拟盘。x=0,1,2 ……
<p class="bhw98">其它的设备名称需通过驱动接口的GUID调用设备管理函数族取得,这里暂不讨论。
<p class="bhw98"><strong class="bhw98">Q</strong> 请举一个简单的例子说明如何通过DeviceIoControl访问设备驱动程序。
<p class="bhw98"><strong class="bhw98">A</strong> 这里有一个从MSDN上摘抄来的demo程序,演示在NT/2000/XP中如何通过DeviceIoControl获取硬盘的基本参数。 <pre class="bhw98"><code class="bhw98"><span class="rem">/* The code of interest is in the subroutine GetDriveGeometry. The
code in main shows how to interpret the results of the IOCTL call. */</span>
<span class="key">#include</span> <span class="str"><windows.h></span>
<span class="key">#include</span> <span class="str"><winioctl.h></span>
BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)
{
HANDLE hDevice; <span class="rem">// handle to the drive to be examined</span>
BOOL bResult; <span class="rem">// results flag</span>
DWORD junk; <span class="rem">// discard results</span>
hDevice = CreateFile(<span class="str">"\\\\.\\PhysicalDrive0"</span>, <span class="rem">// drive to open</span>
<span class="num">0</span>, <span class="rem">// no access to the drive</span>
FILE_SHARE_READ | <span class="rem">// share mode</span>
FILE_SHARE_WRITE,
NULL, <span class="rem">// default security attributes</span>
OPEN_EXISTING, <span class="rem">// disposition</span>
<span class="num">0</span>, <span class="rem">// file attributes</span>
NULL); <span class="rem">// do not copy file attributes</span>
<span class="key">if</span> (hDevice == INVALID_HANDLE_VALUE) <span class="rem">// cannot open the drive</span>
{
<span class="key">return</span> (FALSE);
}
bResult = DeviceIoControl(hDevice, <span class="rem">// device to be queried</span>
IOCTL_DISK_GET_DRIVE_GEOMETRY, <span class="rem">// operation to perform</span>
NULL, <span class="num">0</span>, <span class="rem">// no input buffer</span>
pdg, <span class="key">sizeof</span>(*pdg), <span class="rem">// output buffer</span>
&junk, <span class="rem">// # bytes returned</span>
(LPOVERLAPPED) NULL); <span class="rem">// synchronous I/O</span>
CloseHandle(hDevice);
<span class="key">return</span> (bResult);
}
<span class="key">int</span> main(<span class="key">int</span> argc, <span class="key">char</span> *argv[])
{
DISK_GEOMETRY pdg; <span class="rem">// disk drive geometry structure</span>
BOOL bResult; <span class="rem">// generic results flag</span>
ULONGLONG DiskSize; <span class="rem">// size of the drive, in bytes</span>
bResult = GetDriveGeometry (&pdg);
<span class="key">if</span> (bResult)
{
printf(<span class="str">"Cylinders = %I64d\n"</span>, pdg.Cylinders);
printf(<span class="str">"Tracks per cylinder = %ld\n"</span>, (ULONG) pdg.TracksPerCylinder);
printf(<span class="str">"Sectors per track = %ld\n"</span>, (ULONG) pdg.SectorsPerTrack);
printf(<span class="str">"Bytes per sector = %ld\n"</span>, (ULONG) pdg.BytesPerSector);
DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *
(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;
printf(<span class="str">"Disk size = %I64d (Bytes) = %I64d (Mb)\n"</span>, DiskSize,
DiskSize / (<span class="num">1024</span> * <span class="num">1024</span>));
}
<span class="key">else</span>
{
printf(<span class="str">"GetDriveGeometry failed. Error %ld.\n"</span>, GetLastError());
}
<span class="key">return</span> ((<span class="key">int</span>)bResult);
}
</code></pre>
<p class="bhw98"><strong class="bhw98">Q</strong> 如果将设备名换成“A:”就可以取A盘参数,换成“CDROM0”就可以取CDROM参数,是这样吗?
<p class="bhw98"><strong class="bhw98">A</strong> 这个问题暂不做回答。请动手试一下。
<p class="bhw98">现在我们总结一下通过DeviceIoControl访问设备驱动程序的“三步曲”:首先用CreateFile取得设备句柄,然后用DeviceIoControl与设备进行I/O,最后别忘记用CloseHandle关闭设备句柄。
</div> |
|