动态加载驱动程序第一次成功,可卸载后,第二次加载,就会出现StartService失败,错误代码是2 ????????
代码为:
BOOL InstallDevice()
{
if (m_hDevice!=INVALID_HANDLE_VALUE)
return FALSE;
///////////////////////////////////////////////////////////////////////////////////////
CHAR DriverFileName[80];
if (!GetSystemDirectory(DriverFileName, 55))
{
return FALSE;
}
strcat(DriverFileName,"\\Drivers\\ComSpy.sys");
if (!CopyFile(".\\ComSpy.sys", DriverFileName, FALSE))
{
return FALSE;
}
///////////////////////////////////////////////////////////////////////////////////////
SC_HANDLE sc=OpenSCManager(NULL,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
if (!sc)
{
AfxMessageBox(_T("You must have administrative priviledges to install device driver"),MB_OK);
return FALSE;
}
CString s=DriverFileName;
SC_HANDLE hSrv;
hSrv=CreateService(sc,_T("ComSpy"),_T("ComSpy"),SERVICE_ALL_ACCESS | GENERIC_EXECUTE | SERVICE_START | SERVICE_STOP,
SERVICE_KERNEL_DRIVER,SERVICE_DEMAND_START,SERVICE_ERROR_IGNORE,
s,
NULL,
NULL,
NULL,
NULL,
NULL);
DWORD dwRtn;
if(!hSrv)
{
dwRtn = GetLastError();
if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS )
{
CloseServiceHandle(sc);
AfxMessageBox(_T("Unexpected error. Cannot create service"),MB_OK);
return FALSE;
}
else
{
AfxMessageBox(_T("CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS!"),MB_OK);
}
// 驱动程序已经加载,只需要打开
hSrv = OpenService(sc,_T("ComSpy"),SERVICE_ALL_ACCESS );
if( hSrv == NULL )
{
dwRtn = GetLastError();
CloseServiceHandle( hSrv );
AfxMessageBox(_T("Unexpected error. OpenService() Faild"),MB_OK);
return FALSE;
}
else
{
AfxMessageBox(_T("OpenService() ok !"),MB_OK);
}
}
else
{
AfxMessageBox(_T("CrateService() ok !"),MB_OK);
}
CloseServiceHandle(sc);
if (!StartService(hSrv,0,NULL))
{
qq: AfxMessageBox(_T("Cannot start device. Sys maybe not found"),MB_OK);
DeleteService(hSrv);
CloseServiceHandle(hSrv);
return FALSE;
}
if(OpenDevice()==FALSE)
{
SERVICE_STATUS ss;
ControlService(hSrv,SERVICE_CONTROL_STOP,&ss);
goto qq;
}
CloseServiceHandle(hSrv);
AfxMessageBox(_T("ComSpy device driver successfully installed"),MB_OK);
return TRUE;
}
void RemoveDevice()
{
if (m_hDevice!=INVALID_HANDLE_VALUE)
CloseHandle(m_hDevice);
SC_HANDLE sc=OpenSCManager(NULL,SERVICES_ACTIVE_DATABASE,SC_MANAGER_ALL_ACCESS);
if (sc)
{
SC_HANDLE hSrv=OpenService(sc,_T("ComSpy"),SERVICE_START | SERVICE_STOP | DELETE);
CloseServiceHandle(sc);
if (hSrv)
{
SERVICE_STATUS ss;
//ControlService(hSrv,SERVICE_CONTROL_STOP,&ss);
//停止驱动程序,如果停止失败,只有重新启动才能,再动态加载。
if( !ControlService(hSrv,SERVICE_CONTROL_STOP,&ss) )
{
//printf( "DelSvr::ControlService() Faild %d !\n", GetLastError() );
AfxMessageBox(_T("DelSvr::ControlService() Faild"),MB_OK);
}
else
{
//printf( "DelSvr::ControlService() ok !\n" );
AfxMessageBox(_T("DelSvr::ControlService() ok !"),MB_OK);
}
//动态卸载驱动程序。
if( !DeleteService(hSrv))
{
//printf( "DelSvr:eleteSrevice() Faild %d !\n", GetLastError() );
AfxMessageBox(_T("DelSvr:eleteSrevice() Faild"),MB_OK);
}
else
{
//printf( "DelSvr:eleteSrevice() ok !\n" );
AfxMessageBox(_T("DelSvr:eleteSrevice() ok !"),MB_OK);
}
CloseServiceHandle(hSrv);
m_hDevice=INVALID_HANDLE_VALUE;
}
else
AfxMessageBox(_T("Error. Cannot remove ComSpy device driver"),MB_OK);
}
}
动态加载驱动程序第一次成功,可卸载后,第二次加载,就会出现StartService失败,错误代码是2
重启机器后又可以了.问为什么??????????????
提示:可以和SYS驱动有点关系,因用德国人的原代码生成的sys不出现这种情况,现在我只是在代码中增加了一个控制码及相关一小段程序.就不行了!难道ControlService不能停止sys引起的吗?可以卸载时提示全是成功的呀! 我怒了!!!!!!!
急切想知道是可能是那方面原因造成的及解决方法!!!!!!!!!!!!!!!!!!!