//加载核心驱动 BOOL loadKD(LPCTSTR DriverPath, LPCTSTR Servicename) { BOOL bResult=true; SC_HANDLE hSCManager=NULL; SC_HANDLE hService=NULL; hSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if ( hSCManager != NULL ) { hService = CreateService( hSCManager, Servicename, Servicename, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, DriverPath, NULL, NULL, NULL, NULL, NULL ); if ( hService != NULL ) { if ( StartService( hService, 0, NULL ) == 0 ) { DeleteService(hService); bResult = false; } if ( CloseServiceHandle(hService) == 0 ) { bResult = false; } } else { if ( GetLastError() == ERROR_SERVICE_EXISTS ) { hService = OpenService( hSCManager, Servicename, SERVICE_ALL_ACCESS ); if ( hService != NULL ) { if ( StartService( hService, 0, NULL ) == 0 ) { bResult = false; } if ( CloseServiceHandle(hService) == 0 ) { bResult = false; } } else { bResult = false; } } else { bResult = false; } } if ( CloseServiceHandle(hSCManager) == 0 ) { bResult = false; } } else { bResult = false; } return bResult; } //卸载核心驱动 BOOL unloadKD(LPCTSTR ServiceName) { BOOL bResult=true; SC_HANDLE hSCManager=NULL; SC_HANDLE hService=NULL; SERVICE_STATUS ServiceStatus; hSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if ( hSCManager != NULL ) { hService = OpenService( hSCManager, ServiceName, SERVICE_ALL_ACCESS ); if ( hService != NULL ) { if ( ControlService( hService, SERVICE_CONTROL_STOP, &ServiceStatus ) != 0 ) { if ( DeleteService(hService) == 0 ) { bResult = false; } } else { bResult = false; } if ( CloseServiceHandle(hService) == 0 ) { bResult = false; } } else { bResult = false; } if ( CloseServiceHandle(hSCManager) == 0 ) { bResult = false; } } else { bResult = false; } return bResult; }