Chinaunix首页 | 论坛 | 博客
  • 博客访问: 455522
  • 博文数量: 724
  • 博客积分: 40000
  • 博客等级: 大将
  • 技术积分: 5010
  • 用 户 组: 普通用户
  • 注册时间: 2008-10-13 14:47
文章分类

全部博文(724)

文章存档

2011年(1)

2008年(723)

我的朋友

分类:

2008-10-13 17:18:48

如何建立应用程序和驱动程序间的通信

作者:

  驱动程序是为设备的硬件层编程服务的,但同样需要提供和应用程序进行通信的能力,从而最终达到应用程序控制设备的目的。本文主要讨论应用程序与驱动程序的通信和驱动程序与应用程序的通信。
  在Windows中,应用程序实现与WDM通信的过程是:应用程序先用CreateFile函数打开设备,然后用DeviceIoControl和WDM进行通信,包括从WDM读数据和写数据给WDM两种情况,也可用ReadFile从WDM中读数据或用WriteFile写数据给WDM。当应用程序退出时,用CloseHandle关闭设备。

以下是用DeviceIoControl写数据给WDM的源代码:

void Test_P9052_IOCTL_805_WriteBase3(void)
{
	ULONG	nOutput;						// Count written to bufOutput

	// 传给驱动程序要写入的参数和数据
	// 数组的第一个元素为写入的偏移地址,第二个元素为数据的个数,其它元素为写入的数据
	ULONG	bufInput[IOCTL_INBUF_SIZE+2];	
	ULONG   offset;       // 写入的偏移地址
	ULONG   num;          // 写入的初始数据,以此来产生一个数组
	
	printf("\n----------- ready for writing to Base3 -----------");
	
	//获取写入的偏移地址
	printf("\nPlease input the offset of the write operation(Hex):");
	scanf("%x",&offset);
//	//获取写入的数据个数
	bufInput[0]=offset;
	bufInput[1]=IOCTL_INBUF_SIZE;
	//获取写入的数据
	printf("\nPlease input the initial data to write(Hex):");
	scanf("%x",&num);
	for(ULONG j=0;j
以下是用DeviceIoControl从WDM中读数据的源代码:
void Test_P9052_IOCTL_804_ReadBase3(void)
{
	ULONG	bufOutput[IOCTL_OUTBUF_SIZE];	// 传出读取的数据缓冲区
	ULONG	nOutput;						// 实际读取的数据个数
        ULONG   bufInput[2];	 	            // 传入读取的参数
	ULONG   offset;                         // 要读取的偏移地址
	printf("\n----------- ready for reading from Base3 -----------");
	//获取读取的偏移地址:
	printf("\nPlease input the offset of read operation(Hex):");
	scanf("%x",&offset);
//	//获取读取的数据个数:
//	printf("\nPlease input the number of data to read(Dec):");
//	scanf("%d",&number);
	bufInput[0]=offset;
	bufInput[1]=IOCTL_OUTBUF_SIZE;
	// Call device IO Control interface (PCI9054_IOCTL_804_ReadBase3) in driver
	if (!DeviceIoControl(hDevice,
						 P9052_IOCTL_804_ReadBase3,
						 bufInput,
                         2*4,     // 字节
						 bufOutput,
						 IOCTL_OUTBUF_SIZE*4,//sizeof(bufOutput),
						 &nOutput,
						 NULL)
	   )
	{
		printf("\nERROR: DeviceIoControl returns %0x.", GetLastError());
		Exit(1);
	}
    printf("\n------>>>>>> data read <<<<<<------");
	for(ULONG i=0;i

驱动程序与应用程序的通信:
与此相对应,当驱动程序捕捉到特点事件(如中断)发生时,应当可以与应用程序进行通信。 以下是通过I/O请求包(IRP),驱动程序的读数据函数:
VOID P9052Device::Serial_P9052_IOCTL_804_ReadBase3_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;
	t << "Entering P9052Device::Serial_P9052_IOCTL_804_ReadBase3_Handler, " << I << EOL;
	KMemory Mem(I.Mdl());     
	// Use the memory object to create a pointer to the caller''s buffer
	PULONG	pOutBuffer	= (PULONG) Mem.MapToSystemSpace();  //输出缓冲区指针,传出读取的数据
	PULONG  pInBuffer       = (PULONG) I.IoctlBuffer();     //输入缓冲区指针
	ULONG   Offset;                     //读取的偏移地址
	Offset   = *pInBuffer;
	ULONG   count;                      //读取的数据个数
	count    = *(pInBuffer+1);
	m_IoPortRange1_ForBase3.ind(Offset,pOutBuffer,count);
	I.Information() = count;
	I.Status() = status;
	m_DriverManagedQueue.PnpNextIrp(I);
}
以下是通过IRP,驱动程序的写数据函数:
VOID P9052Device::Serial_P9052_IOCTL_805_WriteBase3_Handler(KIrp I)
{
	NTSTATUS status = STATUS_SUCCESS;
	t << "Entering P9052Device::Serial_P9052_IOCTL_805_WriteBase3_Handler, " << I << EOL;
	PULONG  pInBuffer  = (PULONG) I.IoctlBuffer(); //输入缓冲区指针
	ULONG   count;                        //从输入缓冲区要写入的数据个数
	count=*(pInBuffer+1);
	ULONG   offset;                       //偏移地址
	offset=*pInBuffer;
	PULONG  pBuffer  = pInBuffer+2;       //指向要写入的数据
	m_IoPortRange1_ForBase3.outd(offset,pBuffer,count);
	I.Information() = count;
	I.Status() = status;
	m_DriverManagedQueue.PnpNextIrp(I);
}
下图是运行时的界面:


图一 VC运行界面

  本文通过DriverWorks实现对PCI9052的数据写入、读出。读写方式是DirectIO方式。这是一种简单的方式,速度只能达到几M,如果是大批量的数据写入可以采用buffer的方式。
--------------------next---------------------




搞错了吧

控制读——用IoctlBuffer

控制写——用Mdl ( heartofdra 发表于 2007-3-6 12:10:00)
 
错呀,希望楼主修改
--------------------Configuration: hellowdm - Win32 Checked--------------------
Compiling resources...
Compiling...
hellowdm.cpp
E:\学习VC++资料\WINDOWS系统\驱动程序开发\Windows 2000驱动程序的设计\hellowdm.h(12) : fatal error C1083: Cannot open include file: 'wdm.h': No such file or directory
执行 cl.exe 时出错.

hellowdm.sys - 1 error(s), 0 warning(s)
The following environment variables were not found
$(CPU)
$(BASEDIR)

环境XP+VC6.0 ( sunjunfneg3 发表于 2005-11-7 2:05:00)
---------------------------------------
你懂不懂编程,不懂别叫... ( Mycro 发表于 2006-12-30 12:10:00)
 
出错呀,希望楼主修改
--------------------Configuration: hellowdm - Win32 Checked--------------------
Compiling resources...
Compiling...
hellowdm.cpp
E:\学习VC++资料\WINDOWS系统\驱动程序开发\Windows 2000驱动程序的设计\hellowdm.h(12) : fatal error C1083: Cannot open include file: 'wdm.h': No such file or directory
执行 cl.exe 时出错.

hellowdm.sys - 1 error(s), 0 warning(s)
The following environment variables were not found
$(CPU)
$(BASEDIR)

环境XP+VC6.0 ( sunjunfneg3 发表于 2005-11-7 2:05:00)
 
就是,是否用SHELL_POSTMESSAGE()? ( sxzllp 发表于 2004-11-27 11:55:00)
 
与此相对应,当驱动程序捕捉到特点事件(如中断)发生时,应当可以与应用程序进行通信
-------------------------
怎么通信的?没有看出来呀! ( supercar 发表于 2004-11-11 8:40:00)
 
.......................................................

--------------------next---------------------

阅读(391) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~