MFC清除CMOS密码

这个只是根据昨天的清除CMOS密码的驱动程序对应的一个SDK

有一个资源,记得将驱动文件添加上来哦!

下面是部分代码(VC++6.0编译)

void CCMOSDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	bool LoadNTDriver(char* DriverName,char* DriverPathName);
	bool UnLoadNTDriver(char* DriverName);
	bool ReleaseRes(CString FileName,WORD ResID,CString strFileType);
	CString DriverName_CMOS,PathName_CMOS;
	DriverName_CMOS="C:\\delcmos.sys";
	PathName_CMOS="C:\\delcmos.sys";
	if(!ReleaseRes("C:\\delcmos.sys",IDR_SYS1,"SYS")) //释放驱动文件
		MessageBox("发生错误,驱动文件无法释放!","错误:");
	if(!LoadNTDriver(DriverName_CMOS.GetBuffer(256),PathName_CMOS.GetBuffer(256))) /* 安装驱动 */
		MessageBox("加载驱动失败!","错误:");
	else if(!UnLoadNTDriver(DriverName_CMOS.GetBuffer(256))) /* 安装后卸载 */
		MessageBox("驱动加载成功,但是卸载失败!","错误:");
	else
		MessageBox("清除CMOS密码成功!","信息:");
}
/* 安装驱动 */
bool LoadNTDriver(char* DriverName,char* DriverPathName)
{
	SC_HANDLE hSCM=NULL;
	SC_HANDLE hService=NULL;
	bool retn = false;
	hSCM=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
	if(!hSCM)
	{
		if(hService)
			CloseServiceHandle(hService);
		if(hSCM)
			CloseServiceHandle(hSCM);
		return retn;
	}
	hService=CreateService(
		hSCM,
		DriverName,
		DriverName,
		SERVICE_ALL_ACCESS,
		SERVICE_KERNEL_DRIVER,
		SERVICE_DEMAND_START,
		SERVICE_ERROR_IGNORE,
		DriverPathName,
		NULL,
		NULL,
		NULL,
		NULL,
		NULL
		);
	DWORD dwError;
	if(!hService)
	{
		dwError=GetLastError(); /* 返回错误结果 */
		if(dwError!=ERROR_IO_PENDING && dwError!=ERROR_SERVICE_EXISTS)
		{
			if(hService)
				CloseServiceHandle(hService);
			if(hSCM)
				CloseServiceHandle(hSCM);
			return retn;
		}
	}
	hService=OpenService(hSCM,DriverName,SERVICE_ALL_ACCESS);
	if(!hService)
	{
		if(hService)
			CloseServiceHandle(hService);
		if(hSCM)
			CloseServiceHandle(hSCM);
		return retn;
	}
	retn=StartService(hService,NULL,NULL);
	if(!retn)
	{
		if(hService)
			CloseServiceHandle(hService);
		if(hSCM)
			CloseServiceHandle(hSCM);
		return retn;
	}
	retn = true;
	if(hService)
		CloseServiceHandle(hService);
	if(hSCM)
		CloseServiceHandle(hSCM);
	return retn;
}
//卸载驱动
bool UnLoadNTDriver(char* DriverName)
{
	SC_HANDLE hscm=NULL;
	SC_HANDLE hservice=NULL;
	SERVICE_STATUS SvrSta;
	bool retn=false;
	hscm=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
	if(!hservice)
	{
		if(hscm)
			CloseServiceHandle(hscm);
		if(hservice)
			CloseServiceHandle(hservice);
		return retn;
	}
	if(!hservice)
	{
		if(hscm)
			CloseServiceHandle(hscm);
		if(hservice)
			CloseServiceHandle(hservice);
		return retn;
	}
	if(!ControlService(hscm,SERVICE_CONTROL_STOP,&SvrSta))
	{
		retn=false;
		if(hscm)
			CloseServiceHandle(hscm);
		if(hservice)
			CloseServiceHandle(hservice);
		return retn;
	}
	if(!DeleteService(hservice))
	{
		retn=false;
		if(hscm)
			CloseServiceHandle(hscm);
		if(hservice)
			CloseServiceHandle(hservice);
		return retn;
	}
	else
		retn=true;
	if(hscm)
		CloseServiceHandle(hscm);
	if(hservice)
		CloseServiceHandle(hservice);
	return retn;
}
bool ReleaseRes(CString strFileName,WORD wResID,CString strFileType)
{
	DWORD dwWrite=0;
    // 创建文件   
    HANDLE hFile=CreateFile(strFileName,GENERIC_WRITE,FILE_SHARE_WRITE,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);  
    if (hFile==INVALID_HANDLE_VALUE)  
		return false;   
    // 查找资源文件中、加载资源到内存、得到资源大小   
	HRSRC hrsc=FindResource(NULL,MAKEINTRESOURCE(wResID), strFileType);  
	HGLOBAL hG=LoadResource(NULL,hrsc);  
	DWORD dwSize=SizeofResource(NULL,hrsc);  
	if (!(hrsc&&hG&&dwSize))
		return false;
    // 写入文件   
	WriteFile(hFile,hG,dwSize,&dwWrite,NULL);
	CloseHandle(hFile);  
	return true;  
}


全部文件在这里:http://pan.baidu.com/s/1qWqx5j2

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值