阅读:1172回复:0
关于动态卸载驱动的问题
如果我想卸载U盘(系统USBSTOR.SYS)我可否用下面的程序卸载。
// Test.cpp : Defines the entry point for the console application. // #include \"stdafx.h\" #include <windows.h> #include <winsvc.h> #include <conio.h> void DelSvr( char * szSvrName ); int main(int argc, char* argv[]) { HANDLE hWdm; printf(\"Hello World!\\n\"); SC_HANDLE hServiceMgr, hServiceTwdm; BOOL bRtn; DWORD dwRtn, dwSize = 256; char szDir[256]; if( argc > 1 ) { DelSvr( \"Twdm1\" ); return 0; } GetCurrentDirectory( dwSize, szDir ); strcat( szDir, \"\\\\Twdm.sys\" ); LPCTSTR lpszBinaryPathName = TEXT(szDir); hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if( hServiceMgr == NULL ) { printf( \"OpenSCManager() Faild %d ! \\n\", GetLastError() ); return 0; } else { printf( \"OpenSCManager() ok ! \\n\" ); } hServiceTwdm = CreateService( hServiceMgr, TEXT(\"Twdm1\"), TEXT(\"Twdm1\"), SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE, lpszBinaryPathName, NULL, NULL, NULL, NULL, NULL); if( hServiceTwdm == NULL ) { dwRtn = GetLastError(); if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_EXISTS ) { CloseServiceHandle( hServiceMgr ); printf( \"CrateService() Faild %d ! \\n\", dwRtn ); return 0; } else { printf( \"CrateService() Faild Service is ERROR_IO_PENDING or ERROR_SERVICE_EXISTS! \\n\" ); } hServiceTwdm = OpenService( hServiceMgr, TEXT(\"Twdm1\"), SERVICE_ALL_ACCESS ); if( hServiceTwdm == NULL ) { dwRtn = GetLastError(); CloseServiceHandle( hServiceMgr ); printf( \"OpenService() Faild %d ! \\n\", dwRtn ); return 0; } else { printf( \"OpenService() ok ! \\n\" ); } } else { printf( \"CrateService() ok ! \\n\" ); } bRtn = StartService( hServiceTwdm, NULL, NULL ); if( !bRtn ) { //ERROR_PATH_NOT_FOUND dwRtn = GetLastError(); if( dwRtn != ERROR_IO_PENDING && dwRtn != ERROR_SERVICE_ALREADY_RUNNING ) { printf( \"StartService() Faild %d ! \\n\", dwRtn ); CloseServiceHandle( hServiceTwdm ); CloseServiceHandle( hServiceMgr ); return 0; } else { if( dwRtn != ERROR_IO_PENDING ) { printf( \"StartService() Faild ERROR_IO_PENDING ! \\n\"); } else { printf( \"StartService() Faild ERROR_SERVICE_ALREADY_RUNNING ! \\n\"); } } } hWdm = CreateFile(\"\\\\\\\\.\\\\Twdm1\", GENERIC_WRITE | GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL ); if( hWdm != INVALID_HANDLE_VALUE ) { printf( \"Open Driver Twdm ok ! \\n\" ); } else { printf( \"Open Driver Twdm faild %d ! \\n\", GetLastError() ); } CloseHandle( hWdm ); CloseServiceHandle( hServiceTwdm ); CloseServiceHandle( hServiceMgr ); printf( \"按任意键 卸载驱动程序 !\\n\" ); getch(); DelSvr( \"Twdm1\" ); return 0; } void DelSvr( char * szSvrName ) { SC_HANDLE hServiceMgr, hServiceTwdm; SERVICE_STATUS SvrSta; hServiceMgr = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); if( hServiceMgr == NULL ) { printf( \"DelSvr::OpenSCManager() Faild %d ! \\n\", GetLastError() ); return; } else { printf( \"DelSvr::OpenSCManager() ok ! \\n\" ); } hServiceTwdm = OpenService( hServiceMgr, TEXT(szSvrName), SERVICE_ALL_ACCESS ); if( hServiceTwdm == NULL ) { CloseServiceHandle( hServiceMgr ); printf( \"DelSvr::OpenService() Faild %d ! \\n\", GetLastError() ); return; } else { printf( \"DelSvr::OpenService() ok ! \\n\" ); } if( !ControlService( hServiceTwdm, SERVICE_CONTROL_STOP , &SvrSta ) ) { printf( \"DelSvr::ControlService() Faild %d !\\n\", GetLastError() ); } else { printf( \"DelSvr::ControlService() ok !\\n\" ); } if( !DeleteService( hServiceTwdm ) ) { printf( \"DelSvr::DeleteSrevice() Faild %d !\\n\", GetLastError() ); } else { printf( \"DelSvr::DeleteSrevice() ok !\\n\" ); } CloseServiceHandle( hServiceTwdm ); CloseServiceHandle( hServiceMgr ); return; } |
|