wesintj
驱动牛犊
驱动牛犊
  • 注册日期2002-12-05
  • 最后登录2007-04-29
  • 粉丝0
  • 关注0
  • 积分10分
  • 威望1点
  • 贡献值0点
  • 好评度1点
  • 原创分0分
  • 专家分0分
阅读:1170回复:0

关于动态卸载驱动的问题

楼主#
更多 发布于:2002-12-12 14:32
如果我想卸载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;

}
游客

返回顶部