<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>零度x&#039;s blog &#187; irp</title>
	<atom:link href="http://www.lingdux.com/tag/irp/feed" rel="self" type="application/rss+xml" />
	<link>http://www.lingdux.com</link>
	<description>Take it slowly,it&#039;s OK,it&#039;s OK.</description>
	<lastBuildDate>Wed, 09 Mar 2011 14:16:17 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>驱动学习笔记&#8211;irp</title>
		<link>http://www.lingdux.com/2009/76.html</link>
		<comments>http://www.lingdux.com/2009/76.html#comments</comments>
		<pubDate>Fri, 26 Jun 2009 17:41:21 +0000</pubDate>
		<dc:creator>零度x</dc:creator>
				<category><![CDATA[驱动学习]]></category>
		<category><![CDATA[irp]]></category>
		<category><![CDATA[学习]]></category>
		<category><![CDATA[派遣函数]]></category>
		<category><![CDATA[笔记]]></category>
		<category><![CDATA[驱动]]></category>

		<guid isPermaLink="false">http://www.lingdux.com/?p=76</guid>
		<description><![CDATA[irp是Windows内核中的一种非常重要的数据结构。上层应用程序与底层驱动程序通信时，应用程序会发出I/O请求，操作系统将相应的I/O请求转换成相应的IRP，不同的IRP会根据类型被分派到不同的派遣例程中进行处理。

irp相当于ring3下的消息，应用程序对驱动程序进行操作的时候会发出相应的消息，驱动程序根据这些消息做出相应的操作。这些操作通过我们自己编写的派遣函数来决定执行什么样的操作。

看下面流程： 

<span class="readmore"><a href="http://www.lingdux.com/2009/76.html" title="驱动学习笔记&#8211;irp">阅读全文——共1904字</a></span>]]></description>
			<content:encoded><![CDATA[<p>irp是Windows内核中的一种非常重要的数据结构。上层应用程序与底层驱动程序通信时，应用程序会发出I/O请求，操作系统将相应的I/O请求转换成相应的IRP，不同的IRP会根据类型被分派到不同的派遣例程中进行处理。</p>
<p>irp相当于ring3下的消息，应用程序对驱动程序进行操作的时候会发出相应的消息，驱动程序根据这些消息做出相应的操作。这些操作通过我们自己编写的派遣函数来决定执行什么样的操作。</p>
<p>看下面流程： </p>
<p>1.为不同的irp类型设置派遣函数</p>
<p>2.编写派遣函数来处理收到不懂类型的irp的不同操作</p>
<p>3.<a href="http://www.lingdux.com/2009/61.html" target="_blank">创建设备与符号链接</a></p>
<p>4.<a href="http://www.lingdux.com/2009/61.html" target="_blank">在卸载历程中删除设备与符号链接</a></p>
<p>流程很简单，但是处理起来就不那么简单了。</p>
<p>代码如下：</p>
<p><span id="more-76"></span></p>
<p>#include “ntddk.h”<br />
VOID Unload(IN PDRIVER_OBJECT DriverObject)//卸载时删除符号链接<br />
{<br />
 UNICODE_STRING symLinkName;<br />
 RtlInitUnicodeString(&amp;symLinkName,L”\\??\\mysymboliclink”);<br />
 IoDeleteSymbolicLink(&amp;symLinkName);<br />
 IoDeleteDevice(DriverObject-&gt;DeviceObject);<br />
 KdPrint((“Device Delete Success\n”));<br />
}<br />
NTSTATUS MyMajor(IN PDEVICE_OBJECT Device,IN PIRP irp)<br />
{<br />
 PIO_STACK_LOCATION stack = IoGetCurrentIrpStackLocation(irp);//动态获取irp的IO堆栈<br />
 irp-&gt;IoStatus.Status = STATUS_SUCCESS;//填充返回的状态值<br />
 irp-&gt;IoStatus.Information = 0;<br />
 //判断是否为打开驱动的irp，如果是则输出“IRP_MJ_CREATE”<br />
 if (stack-&gt;MajorFunction==IRP_MJ_CREATE)<br />
 {<br />
  KdPrint((“IRP_MJ_CREATE\n”));<br />
 }<br />
 //判断是否为关闭驱动的irp，如果是则输出“IRP_MJ_CLOS”<br />
 if (stack-&gt;MajorFunction==IRP_MJ_CLOSE)<br />
 {<br />
  KdPrint((“IRP_MJ_CLOSE\n”));<br />
 }<br />
 IoCompleteRequest( irp, IO_NO_INCREMENT );//结束掉irp<br />
 return irp-&gt;IoStatus.Status;//返回<br />
}<br />
NTSTATUS DriverEntry(IN PDRIVER_OBJECT DriverObject,IN PUNICODE_STRING RegistryPath)<br />
{<br />
 PDEVICE_OBJECT mydevice;//定义设备对象<br />
 UNICODE_STRING devicename;//定义设备名称<br />
 UNICODE_STRING symboliclinkname;//定义符号连接名称<br />
 //为不同的irp类型设置派遣函数<br />
 DriverObject-&gt;MajorFunction[IRP_MJ_CREATE]=MyMajor;//打开<br />
 DriverObject-&gt;MajorFunction[IRP_MJ_CLOSE]=MyMajor;//关闭<br />
 DriverObject-&gt;DriverUnload=Unload;</p>
<p> RtlInitUnicodeString(&amp;devicename,L”\\Device\\mydevice”);//初始化设备名称<br />
 RtlInitUnicodeString(&amp;symboliclinkname,L”\\??\\mysymboliclink”);//初始化符号连接名称<br />
 //创建设备<br />
 IoCreateDevice(<br />
  DriverObject,//驱动对象<br />
  0,<br />
  &amp;devicename,//设备名称<br />
  FILE_DEVICE_UNKNOWN,//类型<br />
  0,<br />
  FALSE,<br />
  &amp;mydevice//设备对象<br />
  );<br />
 //创建符号链接<br />
 IoCreateSymbolicLink(<br />
  &amp;symboliclinkname,//符号链接名称<br />
  &amp;devicename//设备名称<br />
  );</p>
<p> return STATUS_SUCCESS;<br />
}</p>
<p>下面写一个简单的vc程序打开然后关闭驱动，打开的时候会发送给两个消息转换成irp就是IRP_MJ_CREATE和IRP_MJ_CLOSE,然后截获到输出“IRP_MJ_CREATE和IRP_MJ_CLOSE”。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.lingdux.com/2009/76.html/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

