BOOL RemoveDriver(PCSTR pszDriverName) { SC_HANDLE hSCManager; SC_HANDLE hService; BOOL bResult; StopDriver(pszDriverName); hSCManager=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hSCManager) { hService=OpenService(hSCManager, pszDriverName, SERVICE_ALL_ACCESS); CloseServiceHandle(hSCManager); if (hService) { bResult=DeleteService(hService); CloseServiceHandle(hService); } else return FALSE; } else return FALSE; return bResult; }
/**************************************************************************** * * FUNCTION: UnloadDeviceDriver( const TCHAR *) * * PURPOSE: Stops the driver and has the configuration manager unload it. * ****************************************************************************/ BOOL UnloadDeviceDriver( const TCHAR * Name,BOOL DRemove) { SC_HANDLE schSCManager; if (( strcmp(Name,"MultiDec BT-Treiber") == 0 ) || ( strcmp(Name,"MultiDec SAA-Treiber") == 0 ) || ( strcmp(Name,"MultiDec HAL-Treiber") == 0 )) return(TRUE); if ( Keep_Driver_Loaded == TRUE ) { /* if (( strcmp(Name,"MultiDec BT-Treiber") == 0 ) || ( strcmp(Name,"MultiDec SAA-Treiber") == 0 ) || ( strcmp(Name,"MultiDec HAL-Treiber") == 0 )) */ return(TRUE); }; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( StopDriver( schSCManager, Name ) == FALSE ) { CloseServiceHandle( schSCManager ); return(TRUE); }; if ( DRemove == TRUE ) RemoveDriver( schSCManager, Name ); CloseServiceHandle( schSCManager ); return TRUE; }
void DebuggerState::CleanUp() { // FIXME (obfuscated#): This is not a good API design! Replace with RemoveAllBreakpoints if (m_pDriver) m_pDriver->RemoveBreakpoint(cb::shared_ptr<DebuggerBreakpoint>()); StopDriver(); m_Breakpoints.clear(); }
static BOOL __cdecl stop_driver( void ) { SC_HANDLE schSCManager; BOOL ret = FALSE; schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if(!schSCManager) return(FALSE); if(StopDriver( schSCManager, sDriverShort )) ret = TRUE; CloseServiceHandle( schSCManager ); return( ret ); }
BOOL RegisterDriver(LPCWSTR lpDriverName, LPCWSTR lpPathName) { SC_HANDLE hSCManager; SC_HANDLE hService; hSCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hSCManager) return FALSE; retry: hService = CreateServiceW(hSCManager, lpDriverName, lpDriverName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, lpPathName, NULL, NULL, NULL, NULL, NULL); if (hService) { CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return TRUE; } else { DWORD err = GetLastError(); if (err == ERROR_SERVICE_MARKED_FOR_DELETE) { StopDriver(DRIVER_NAME); goto retry; } CloseServiceHandle(hSCManager); // return TRUE if the driver is already registered return (err == ERROR_SERVICE_EXISTS); } }
/**************************************************************************** * * FUNCTION: UnloadDeviceDriver( const TCHAR *) * * PURPOSE: Stops the driver and has the configuration manager unload it. * ****************************************************************************/ BOOL UnloadDeviceDriver( const TCHAR * Name ) { SC_HANDLE schSCManager; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); StopDriver( schSCManager, Name ); RemoveDriver( schSCManager, Name ); CloseServiceHandle( schSCManager ); return TRUE; }
VOID CDriver::UnDoCDriver() { // 关闭设备句柄 if(m_hDriver != INVALID_HANDLE_VALUE) ::CloseHandle(m_hDriver); // 如果创建了服务,就将之删除 if(m_bCreateService) { StopDriver(); ::DeleteService(m_hService); } // 关闭句柄 if(m_hService != NULL) ::CloseServiceHandle(m_hService); if(m_hSCM != NULL) ::CloseServiceHandle(m_hSCM); }
int _tmain(int argc, _TCHAR* argv[]) { /*if (extract_binaries()){ LoadDriver(_T("NPF"), _T("npf.sys")); StartDriver(_T("NPF")); WinExec("Example_Project.exe", SW_SHOW); }*/ /*TCHAR path[BUFF_PATH_SIZE] = _T(""); get_exe_directory(path); _tprintf(_T("location: %s\n"),path); get_file_dir(path); _tprintf(_T("location: %s\n"), path); get_exe_name(path); _tprintf(_T("name: %s\n"), path);*/ /*TCHAR dir[BUFF_PATH_SIZE] = _T(""); size_t len = 0; get_file_dir(dir); len = _tcslen(dir); dir[len] = _T('\\'); get_exe_name(&dir[len+1]); _tprintf(_T("full path: %s\n"), dir);*/ /*TCHAR dir[BUFF_PATH_SIZE] = _T(""); get_full_working_path(dir, _T("test.sys")); _tprintf(_T("location: %s\n"), dir);*/ TCHAR dir[BUFF_PATH_SIZE] = _T(""); get_full_working_path(dir, _T("npf.sys")); if (extract_binaries()){ StopDriver(_T("NPF")); unLoadDriver(_T("NPF")); LoadDriver(_T("NPF"), dir); StartDriver(_T("NPF")); WinExec("Example_Project.exe", SW_SHOW); } //system("pause"); return 0; }
bool unLoadDriver(TCHAR *DriverName) { SC_HANDLE SCManager; SC_HANDLE Service; DWORD error = NULL; bool status = StopDriver(DriverName); if (status == TRUE) { return NULL; } // Establishes a connection to the service control manager on the specified computer. // Opens the specified service control manager database. SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); // Opens an existing service // Grabs handle to driver Service = OpenService(SCManager, DriverName, DELETE); error = GetLastError(); if (error > 0) { _tprintf(_T("[-] Error: %d\n"), error); CloseServiceHandle(SCManager); CloseServiceHandle(Service); return NULL; } DeleteService(Service); error = GetLastError(); if (error > 0) { _tprintf(_T("[-] Error: %d\n"), error); CloseServiceHandle(SCManager); CloseServiceHandle(Service); return NULL; } CloseServiceHandle(SCManager); CloseServiceHandle(Service); return NULL; }
/************************************* * int _cdecl main( ) * 功能 加载驱动,进行控制 **************************************/ int _cdecl main() { HANDLE hDevice; BOOL bRc; ULONG bytesReturned; DWORD errNum = 0; UCHAR driverLocation[MAX_PATH]; SC_HANDLE schSCManager;// 服务控制器句柄 // 打开服务控制器,后续安装、启动都会使用到。 schSCManager = OpenSCManager(NULL, // 本机 NULL, // 本机数据库 SC_MANAGER_ALL_ACCESS // 存取权限 ); if (!schSCManager) { // 打开失败 printf("Open SC Manager failed! Error = %d \n", GetLastError()); return 1; } // 获得驱动文件的路径 if (!GetDriverPath(driverLocation)) { return 1; } // 安装驱动服务 if (InstallDriver(schSCManager, DRIVER_NAME, driverLocation )) { // 安装成功,启动服务,运行驱动 if(!StartDriver(schSCManager, DRIVER_NAME )) { printf("Unable to start driver. \n"); return 1; } } else { // 安装失败,删除驱动。 RemoveDriver(schSCManager, DRIVER_NAME ); printf("Unable to install driver. \n"); return 1; } // 打开驱动,获得控制所用的句柄 // 由驱动创建的符号链接 hDevice = CreateFile( "\\\\.\\IoctlTest", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if ( hDevice == INVALID_HANDLE_VALUE ) { printf ( "Error: CreatFile Failed : %d\n", GetLastError()); return 1; } // 打印,输入输出。 printf("InputBuffer Pointer = %p, BufLength = %d\n", InputBuffer, sizeof(InputBuffer)); printf("OutputBuffer Pointer = %p BufLength = %d\n", OutputBuffer, sizeof(OutputBuffer)); // 输入到内核的数据, lstrcpy(InputBuffer, "This String is from User Application; using IOCTL_XIOCTL_BUFFER"); printf("\nCalling DeviceIoControl IOCTL_XIOCTL_BUFFER:\n"); // 清空输出缓存 memset(OutputBuffer, 0, sizeof(OutputBuffer)); // 进行IO控制, bRc = DeviceIoControl ( hDevice,// 句柄 (DWORD) IOCTL_XIOCTL_BUFFER,// IOCTL &InputBuffer,// 输入数据 strlen ( InputBuffer )+1,// 输入数据的长度 &OutputBuffer,// 输出数据 sizeof( OutputBuffer),// 输出数据长度 &bytesReturned,// 实际输出的数据长度 NULL ); // 判断是否成功 if ( !bRc ) { printf ( "Error in DeviceIoControl : %d", GetLastError()); return 1; } // 打印从内核输出的内容 printf(" OutBuffer (%d): %s\n", bytesReturned, OutputBuffer); // 关闭句柄 CloseHandle ( hDevice ); // 停止运行 StopDriver(schSCManager, DRIVER_NAME ); // 删除服务 RemoveDriver(schSCManager, DRIVER_NAME ); // 关闭服务控制器 CloseServiceHandle (schSCManager); return 0; }
void AudioDriver::Stop() { isPlaying_=false ; hasData_=false ; StopDriver() ; }
BOOLEAN ManageDriver( IN LPCTSTR DriverName, IN LPCTSTR ServiceName, IN USHORT Function ) { SC_HANDLE schSCManager; BOOLEAN rCode = TRUE; // // Insure (somewhat) that the driver and service names are valid. // if (!DriverName || !ServiceName) { printf("Invalid Driver or Service provided to ManageDriver() \n"); return FALSE; } // // Connect to the Service Control Manager and open the Services database. // schSCManager = OpenSCManager(NULL, // local machine NULL, // local database SC_MANAGER_ALL_ACCESS // access required ); if (!schSCManager) { printf("Open SC Manager failed! Error = %d \n", GetLastError()); return FALSE; } // // Do the requested function. // switch( Function ) { case DRIVER_FUNC_INSTALL: // // Install the driver service. // if (InstallDriver(schSCManager, DriverName, ServiceName )) { // // Start the driver service (i.e. start the driver). // rCode = StartDriver(schSCManager, DriverName ); } else { // // Indicate an error. // rCode = FALSE; } break; case DRIVER_FUNC_REMOVE: // // Stop the driver. // StopDriver(schSCManager, DriverName ); // // Remove the driver service. // RemoveDriver(schSCManager, DriverName ); // // Ignore all errors. // rCode = TRUE; break; default: printf("Unknown ManageDriver() function. \n"); rCode = FALSE; break; } // // Close handle to service control manager. // if (schSCManager) { CloseServiceHandle(schSCManager); } return rCode; } // ManageDriver
/********************************************* * Function to Unload a driver * ********************************************/ DWORD TDriver::UnloadDriver(BOOL forceClearData) { DWORD retCode = DRV_SUCCESS; //if the driver is started, first i will stop it if(m_bStarted) { if((retCode = StopDriver()) == DRV_SUCCESS) { //i only remove it, if it is mark to be removable if(m_bRemovable) { //open service and delete it SC_HANDLE SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if(SCManager == NULL) { return(DRV_ERROR_SCM); } SC_HANDLE SCService = OpenService(SCManager, m_strDriverName, SERVICE_ALL_ACCESS); if(SCService != NULL) { if(!DeleteService(SCService)) { retCode = DRV_ERROR_REMOVING; } else { retCode = DRV_SUCCESS; } } else { retCode = DRV_ERROR_SERVICE; } CloseServiceHandle(SCService); SCService = NULL; CloseServiceHandle(SCManager); SCManager = NULL; //if all ok, update the state if(retCode == DRV_SUCCESS) { m_bLoaded = FALSE; } } } } //if the driver is initialized... if(m_binitialized) { //if there was some problem but i mark foreceClear, i will remove the data if(retCode != DRV_SUCCESS && forceClearData == FALSE) { return(retCode); } //update the state m_binitialized = FALSE; } return(retCode); }
DWORD MyDriver::UnloadDriver(BOOL forceClearData) { DWORD retCode = DRV_SUCCESS; if (started) { if ((retCode = StopDriver()) == DRV_SUCCESS) { if(removable) { SC_HANDLE SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (SCManager == NULL) return DRV_ERROR_SCM; SC_HANDLE SCService = OpenServiceA(SCManager, driverName, SERVICE_ALL_ACCESS); if (SCService != NULL) { if(!DeleteService(SCService)) retCode = DRV_ERROR_REMOVING; else retCode = DRV_SUCCESS; } else retCode = DRV_ERROR_SERVICE; CloseServiceHandle(SCService); SCService = NULL; CloseServiceHandle(SCManager); SCManager = NULL; if(retCode == DRV_SUCCESS) loaded = FALSE; } } } if(initialized) { if(retCode != DRV_SUCCESS && forceClearData == FALSE) return retCode; initialized = FALSE; if(driverPath != NULL) { free(driverPath); driverPath = NULL; } if(driverDosName != NULL) { free(driverDosName); driverDosName = NULL; } if(driverName != NULL) { free(driverName); driverName = NULL; } } return retCode; }
//Function to Unload a driver DWORD TDriver::UnloadDriver(BOOL forceClearData) { DWORD retCode = DRV_SUCCESS; //if the driver is started, first i will stop it if (started) { if ((retCode = StopDriver()) == DRV_SUCCESS) { //i only remove it, if it is mark to be removable if(removable) { //open service and delete it SC_HANDLE SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (SCManager == NULL) return DRV_ERROR_SCM; SC_HANDLE SCService = OpenService(SCManager, driverName, SERVICE_ALL_ACCESS); if (SCService != NULL) { if(!DeleteService(SCService)) retCode = DRV_ERROR_REMOVING; else retCode = DRV_SUCCESS; } else retCode = DRV_ERROR_SERVICE; CloseServiceHandle(SCService); SCService = NULL; CloseServiceHandle(SCManager); SCManager = NULL; //if all ok, update the state if(retCode == DRV_SUCCESS) loaded = FALSE; } } } //if the driver is initialized... if(initialized) { //if there was some problem but i mark foreceClear, i will remove the data if(retCode != DRV_SUCCESS && forceClearData == FALSE) return retCode; //update the state initialized = FALSE; //free memory if(driverPath != NULL) { free(driverPath); driverPath = NULL; } if(driverDosName != NULL) { free(driverDosName); driverDosName = NULL; } if(driverName != NULL) { free(driverName); driverName = NULL; } } return retCode; }
int main(int argc, char *argv[]) { SC_HANDLE schSCManager; BOOL remove = FALSE; char *test_file = NULL; char *curr_dep; char ServiceName[256] = ""; char ServiceExe[256] = ""; curr_dep = &DependencyList[0]; for (;;) { ++argv; --argc; if (argv[0] == NULL) break; if (argv[0][0] != '-') break; switch( argv[0][1] ) { case 'r': remove = TRUE; break; case 'd': strcpy( curr_dep, &argv[0][2] ); curr_dep += strlen( curr_dep ) + 1; break; case 's': StartType = atoi( &argv[0][2] ); break; case 'e': ErrorControl = atoi( &argv[0][2] ); break; case 't': test_file = &argv[0][2]; break; case 'q': Quiet = TRUE; break; case 'h': Usage(); break; default: fprintf( stderr, "Invalid option '%c'\n", argv[0][1] ); Usage(); break; } } // Handle defaults if driver names are not specified if (curr_dep == &DependencyList[0]) { strcpy(curr_dep, "ParPort"); curr_dep += strlen(curr_dep) + 1; } if (argc < 1) strcpy(ServiceName,"DbgPort"); else strcpy(ServiceName,argv[0]); if (argc < 2) { GetSystemDirectory(ServiceExe,sizeof(ServiceExe)); strcat(ServiceExe,"\\drivers\\dbgport.sys"); } else strcpy(ServiceExe,argv[1]); if (GetVersion() & 0x80000000) { if (!Quiet) printf( "Not on Windows NT, can not install driver.\n" ); return 0; } if (test_file != NULL && OpenDevice(test_file)) { if (!Quiet) printf( "Driver already running\n" ); return 0; } schSCManager = OpenSCManager (NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if (schSCManager == NULL) { fprintf( stderr, "Can not open service manager (%ld)\n", GetLastError() ); return 1; } if (remove) { StopDriver( schSCManager, ServiceName ); RemoveDriver( schSCManager, ServiceName ); } else if (ServiceExe == NULL) { fprintf( stderr, "Missing service executable\n" ); Usage(); } else { *curr_dep = '\0'; if (InstallDriver( schSCManager, ServiceName, ServiceExe ) ) { if (StartDriver( schSCManager, ServiceName ) ) { if (test_file != NULL ) { if (OpenDevice( test_file ) ) if( !Quiet ) printf( "Driver Installation SUCCESS\n" ); else fprintf ( stderr, "Driver not started\n" ); } } } } CloseServiceHandle (schSCManager); return 0; }