/**************************************************************************** * * FUNCTION: LoadDeviceDriver( const TCHAR, const TCHAR, HANDLE *) * * PURPOSE: Registers a driver with the system configuration manager * and then loads it. * ****************************************************************************/ BOOL LoadDeviceDriver( const TCHAR * Name, const TCHAR * Path, HANDLE * lphDevice ) { SC_HANDLE schSCManager; BOOL okay; schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); // Remove old instances RemoveDriver( schSCManager, Name ); // Ignore success of installation: it may already be installed. InstallDriver( schSCManager, Name, Path ); // Ignore success of start: it may already be started. StartDriver( schSCManager, Name ); // Do make sure we can open it. okay = OpenDevice( Name, lphDevice ); CloseServiceHandle( schSCManager ); return okay; }
/**************************************************************************** * * FUNCTION: LoadDeviceDriver( const TCHAR, const TCHAR, HANDLE *) * * PURPOSE: Registers a driver with the system configuration manager * and then loads it. * ****************************************************************************/ BOOL LoadDeviceDriver( const TCHAR * Name, const TCHAR * Path, PDWORD Error ) { SC_HANDLE schSCManager; BOOL okay; schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if(schSCManager) { // Remove previous instance // RemoveDriver( schSCManager, Name ); // Ignore success of installation: it may already be installed. InstallDriver( schSCManager, Name, Path ); // Ignore success of start: it may already be started. okay = StartDriver( schSCManager, Name ); *Error = GetLastError(); CloseServiceHandle( schSCManager ); } return okay; }
/************************************* * 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; }
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 Load the driver DWORD TDriver::LoadDriver(BOOL start) { //if the driver is already started, i havent to do nothing if(m_bLoaded) { return(DRV_SUCCESS); } if(!m_binitialized) { return(DRV_ERROR_NO_INITIALIZED); } //Open Service manager to create the new "service" SC_HANDLE SCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); DWORD retCode = DRV_SUCCESS; if(SCManager == NULL) { return(DRV_ERROR_SCM); } //Create the driver "service" SC_HANDLE SCService = CreateService(SCManager, // SCManager database m_strDriverName, // nombre del servicio m_strDriverName, // nombre a mostrar SERVICE_ALL_ACCESS, // acceso total SERVICE_KERNEL_DRIVER, // driver del kernel SERVICE_DEMAND_START, // comienzo bajo demanda SERVICE_ERROR_NORMAL, // control de errores normal m_strDriverPath, // path del driver NULL, // no pertenece a un grupo NULL, // sin tag NULL, // sin dependencias NULL, // cuenta local del sistema NULL // sin password ); //if i cant create, first i check if the driver already was loaded. if(SCService == NULL) { SCService = OpenService(SCManager, m_strDriverName, SERVICE_ALL_ACCESS); if(SCService == NULL) { retCode = DRV_ERROR_SERVICE; } } CloseServiceHandle(SCService); SCService = NULL; CloseServiceHandle(SCManager); SCManager = NULL; //if all ok, update the state and start if necessary if(retCode == DRV_SUCCESS) { m_bLoaded = TRUE; if(start) { retCode = StartDriver(); } } return(retCode); }
/**************************************************************************** * * FUNCTION: LoadDeviceDriver( const TCHAR, const TCHAR, HANDLE *) * * PURPOSE: Registers a driver with the system configuration manager * and then loads it. * ****************************************************************************/ BOOL LoadDeviceDriver( const TCHAR * Name, const TCHAR * Path, HANDLE * lphDevice, PDWORD Error, BOOL extended) { SC_HANDLE schSCManager; BOOL okay; if (extended) { schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if (schSCManager != NULL) { // Remove old instances //RemoveDriver( schSCManager, Name ); // Ignore success of installation: it may already be installed. okay = InstallDriver( schSCManager, Name, Path ); /* if (okay == FALSE) { if (Error != NULL) *Error = GetLastError(); CloseServiceHandle( schSCManager ); return FALSE; } */ // Ignore success of start: it may already be started. okay = StartDriver( schSCManager, Name ); if (okay == FALSE) { if (Error != NULL) { *Error = GetLastError(); if ((*Error != ERROR_SERVICE_ALREADY_RUNNING)) { CloseServiceHandle( schSCManager ); return FALSE; } } else { CloseServiceHandle( schSCManager ); return FALSE; } } CloseServiceHandle( schSCManager ); } else { if (Error != NULL) *Error = GetLastError(); return FALSE; } } // Do make sure we can open it. okay = OpenDevice( Name, lphDevice ); if (okay == FALSE) { if (Error != NULL) *Error = GetLastError(); } return okay; }
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; }