自己动手写驱动禁用电脑主板喇叭
这是自学Windows驱动开发的第三天,也是我所写的第一个完整的、有用的驱动程序,加之今天日子比较特殊,就用它来纪念今天吧~
众所周知,在我们电脑的主板上有个小喇叭,在计算机一切正常的情况下,你开机时所听到的那清脆的“嘀”一声就是它发出来的,其实要操纵它发出声是很简单的,Windows的Beep这个API就可以轻松办到,根本就不用进Ring0,但如果想对它进行更多的操作就得进Ring0了,要进Ring0,那就得靠驱动了。
话说302和303机房的机器都装了一个叫“增霸卡”的系统还原工具,该工具有一个缓冲区,当缓冲区装满之后,电脑主析的喇叭就会发出不间断的刺耳报警声,但计算机仍然可以正常使用,这也是我为什么要写这个小驱动的原因。
闲话不多说,还是那问老的:翠花,上代码~
/***************************************************
*FileName:RingControler.h
*Author:swtar
*Data:2011-3-31
***************************************************/
//这个预处理可以避免头文件的多次包含
#pragma once
//包含头文件
#ifdef _cplusplus
extern "C"
{
#endif
#include
#ifdef _cplusplus
}
#endif
//设置分布内存和非分布内存,初始化内存块
#define PAGEDCODE code_seg("PAGE") #define LOCKEDCODE code_seg()
#define INITCODE code_seg("INIT") #define PAGEDDATA data_seg("PAGE") #define LOCKEDDATA data_seg()
#define INITDATA data_seg("INIT") holes should correspond to each other, such as location not available round file trimming or drill holes again, but not with fire welding holes. 6.4 check on 6.4.1 Visual inspection of equipment shall be in good condition, clean, and the logo should be complete, clear, paint should be in good condition, no rust crack. 6.4.2 the dish equipment should
#define arraysize(p) (sizeof(p)/sizeof((p)[0])) //定义驱动扩展结构
typedef struct _DEVICE_EXTENSION{
PDEVICE_OBJECT pDevice;
UNICODE_STRING ustrDeviceName;
UNICODE_STRING ustrSymLinkName;
} DEVICE_EXTENSION,*PDEVICE_EXTENSION;
//声明函数
NTSTATUS DefaultRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp); VOID RingControlerUnload(IN PDRIVER_OBJECT pDriverObject); /***************************************************
*FileName:RingControler.cpp
*Author:swtar
*Data:2011-3-31
***************************************************/ //包含头文件
#include "RingControler.h"
//说明以下代码为初始代码
#pragma INITCODE
//驱动入口函数
extern "C" NTSTATUS DriverEntry(IN PDRIVER_OBJECT pDriverObject,IN UNICODE_STRING
pRegistryPath)
{
NTSTATUS status;
PDEVICE_OBJECT pDevObj;
PDEVICE_EXTENSION pDevExt;
//指明派遣函数
pDriverObject->DriverUnload=RingControlerUnload;
pDriverObject->MajorFunction[IRP_MJ_CREATE]=DefaultRoutine;
pDriverObject->MajorFunction[IRP_MJ_CLOSE]=DefaultRoutine;
pDriverObject->MajorFunction[IRP_MJ_WRITE]=DefaultRoutine;
pDriverObject->MajorFunction[IRP_MJ_READ]=DefaultRoutine; //指明设备名称
holes should correspond to each other, such as location not available round file trimming or drill holes again, but not with fire welding holes. 6.4 check on 6.4.1 Visual inspection of equipment shall be in good condition, clean, and the logo should be complete, clear, paint should be in good condition, no rust crack. 6.4.2 the dish equipment should
UNICODE_STRING ustrDevName;
RtlInitUnicodeString(&ustrDevName,L"\\Device\\RingControl");
//创建设备
status=IoCreateDevice(pDriverObject,sizeof(DEVICE_EXTENSION),&(UNICODE_STRING)
ustrDevName,FILE_DEVICE_UNKNOWN,0,TRUE,&pDevObj);
if(!NT_SUCCESS(status))
{
return status;
}
pDevObj->Flags=DO_BUFFERED_IO;
pDevExt=(PDEVICE_EXTENSION)pDevObj->DeviceExtension;
pDevExt->pDevice=pDevObj;
pDevExt->ustrDeviceName=ustrDevName;
//创建符号链接
UNICODE_STRING ustrSymLinkName;
RtlInitUnicodeString(&ustrSymLinkName,L"\\??\\RingControler");
status=IoCreateSymbolicLink(&ustrSymLinkName,&ustrDevName);
if(!NT_SUCCESS(status))
{
IoDeleteDevice(pDevObj);
return status;
}
//断开主板喇叭与缓冲区的连接
UCHAR value=READ_PORT_UCHAR((PUCHAR)0x61);
value=value&0xFC;
WRITE_PORT_UCHAR((PUCHAR)0x61,value);
return STATUS_SUCCESS;
}
//指明以下代码可以存在于分布内存中
#pragma PAGEDCODE
//驱动卸载函数
VOID RingControlerUnload(IN PDRIVER_OBJECT pDriverObject) {
PDEVICE_OBJECT pNextObj;
holes should correspond to each other, such as location not available round file trimming or drill holes again, but not with fire welding holes. 6.4 check on 6.4.1 Visual inspection of equipment shall be in good condition, clean, and the logo should be complete, clear, paint should be in good condition, no rust crack. 6.4.2 the dish equipment should
pNextObj=pDriverObject->DeviceObject;
while (pNextObj!=NULL)
{
PDEVICE_EXTENSION pDevExt=(PDEVICE_EXTENSION)pNextObj->DeviceExtension;
//删除符号链接
UNICODE_STRING pLinkName=pDevExt->ustrSymLinkName;
IoDeleteSymbolicLink(&pLinkName);
//删除设备
pNextObj=pNextObj->NextDevice;
IoDeleteDevice(pDevExt->pDevice);
}
}
#pragma PAGEDCODE
//默认派遣函数
NTSTATUS DefaultRoutine(IN PDEVICE_OBJECT pDevObj,IN PIRP pIrp)
{
NTSTATUS status=STATUS_SUCCESS;
// 完成IRP
pIrp->IoStatus.Status=status;
pIrp->IoStatus.Information=0;
IoCompleteRequest(pIrp,IO_NO_INCREMENT);
return status;
}
//以下是驱动加载程序的源码,该程序为Ring3程序
/***************************************************
*FileName:RingControlerDriverLoader.cpp
*Author:swtar
*Data:2011-3-31
***************************************************/ #include
//Windows程序入口函数
int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR
lpCmdLine,int nCmdShow)
{
char szDriverName[]="RingControler";
char szImagePath[MAX_PATH];
holes should correspond to each other, such as location not available round file trimming or drill holes again, but not with fire welding holes. 6.4 check on 6.4.1 Visual inspection of equipment shall be in good condition, clean, and the logo should be complete, clear, paint should be in good condition, no rust crack. 6.4.2 the dish equipment should
ZeroMemory(szImagePath,MAX_PATH);
GetFullPathName("RingControlerDriver.sys",MAX_PATH,szImagePath,NULL);
SC_HANDLE hScm=NULL,hDriver=NULL;
BOOL bRet=FALSE;
//打开Windows服务管理器
hScm=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(hScm==NULL)
{
MessageBox(NULL,"打开Windows服务管理器失败","程序错误",MB_OK|MB_ICONSTOP);
return 0;
}
//创建服务
hDriver=CreateService(hScm,szDriverName,szDriverName,SERVICE_ALL_ACCESS,SERVIC
E_KERNEL_DRIVER,SERVICE_DEMAND_START,SERVICE_ERROR_IGNORE,szImagePath,NULL,NULL
,NULL,NULL,NULL);
if(hDriver==NULL)
{
MessageBox(NULL,"创建服务失败","程序错误",MB_OK|MB_ICONSTOP);
return 0;
}
//打开服务
hDriver=OpenService(hScm,szDriverName,SERVICE_ALL_ACCESS);
if(hDriver==NULL)
{
MessageBox(NULL,"打开服务失败","程序错误",MB_OK|MB_ICONSTOP);
return 0;
}
//启动服务
bRet=StartService(hDriver,NULL,NULL);
if(!bRet)
{
MessageBox(NULL,"启动服务失败","程序错误",MB_OK|MB_ICONSTOP);
return 0;
}
//关闭服务句柄
holes should correspond to each other, such as location not available round file trimming or drill holes again, but not with fire welding holes. 6.4 check on 6.4.1 Visual inspection of equipment shall be in good condition, clean, and the logo should be complete, clear, paint should be in good condition, no rust crack. 6.4.2 the dish equipment should
CloseServiceHandle(hScm);
CloseServiceHandle(hDriver);
}
holes should correspond to each other, such as location not available round file trimming or drill holes again, but not with fire welding holes. 6.4 check on 6.4.1 Visual inspection of equipment shall be in good condition, clean, and the logo should be complete, clear, paint should be in good condition, no rust crack. 6.4.2 the dish equipment should