阅读:2923回复:6
怎么实现自动安装NDIS驱动呢?
我写了个NDIS驱动,怎么实现自动安装NDIS驱动呢?大虾救命啊
|
|
最新喜欢:![]() |
沙发#
发布于:2003-09-29 14:29
如果是imd或相似的,可以用snetcfg
|
|
|
板凳#
发布于:2003-09-30 08:48
snetcfg是微软DDK中自带的吗?
是不是每次安装后的,重次安装要重启动才可以,这样搞得人很郁闷 |
|
地板#
发布于:2003-10-29 16:21
是啊,重新安装驱动肯定要重起的,没办法 :D
|
|
|
地下室#
发布于:2007-04-03 16:47
如果是一般的驱动,而不是虚拟硬件的,可以使用作为内核服务的驱动模式动态加载。
|
|
|
5楼#
发布于:2007-04-03 16:49
驱动动态加载 c++
// DriverLoad.cpp: implementation of the CDriverLoad class. // ////////////////////////////////////////////////////////////////////// #include "stdafx.h" #include "DriverLoad.h" #include <windows.h> #include <winsvc.h> #ifdef _DEBUG #undef THIS_FILE static char THIS_FILE[]=__FILE__; #define new DEBUG_NEW #endif ////////////////////////////////////////////////////////////////////// // Construction/Destruction ////////////////////////////////////////////////////////////////////// CDriverLoad::CDriverLoad() { } CDriverLoad::~CDriverLoad() { } const int C_TRY_STARTSVR_COUNT = 20; BOOL CDriverLoad::DriverLoad(const char *DriverName, const char *DriverImagePath, bool bBootStart, DWORD *out_pdwError ) { SC_HANDLE hSCManager, hService; if (!DriverName || !DriverImagePath) return FALSE; CString strSvcName; strSvcName = DriverName; DWORD dwError = 0; int nRemoveSvcCount = 0; goto_RE_CreateServcie: if (!(hSCManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS))) { TRACE1("\nload driver path :%s",DriverImagePath); TRACE ("\nOpenSCManager() failed %ld", GetLastError()); return FALSE; } hService = CreateService (hSCManager, strSvcName, strSvcName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, bBootStart ? SERVICE_BOOT_START|SERVICE_AUTO_START : SERVICE_DEMAND_START, SERVICE_ERROR_IGNORE,//SERVICE_ERROR_NORMAL, DriverImagePath, NULL, NULL, NULL, NULL, NULL); if (!hService) { dwError = GetLastError() ; if( dwError == ERROR_SERVICE_DISABLED || dwError == ERROR_SERVICE_DATABASE_LOCKED || dwError == ERROR_PROCESS_ABORTED || dwError == ERROR_PATH_NOT_FOUND || dwError == ERROR_FILE_NOT_FOUND) {//delete the driver service and reinstall CloseServiceHandle (hSCManager); DriverUnload(DriverName); if( nRemoveSvcCount ++ < 10) { goto goto_RE_CreateServcie; } else { TRACE1("\nCreateService() failed:%ld", GetLastError()); if( out_pdwError) { *out_pdwError = dwError; } return FALSE; } } else if(dwError == ERROR_SERVICE_MARKED_FOR_DELETE) {//use the new service name to start CloseServiceHandle (hSCManager); DriverUnload(DriverName); if( nRemoveSvcCount < C_TRY_STARTSVR_COUNT) { strSvcName.Format("%s%d", DriverName, nRemoveSvcCount++); TRACE("\n use the new driver service name:%s", strSvcName); goto goto_RE_CreateServcie; } } else if(dwError != ERROR_SERVICE_EXISTS) { TRACE1("\nCreateService() failed:%ld", GetLastError()); CloseServiceHandle (hSCManager); if( out_pdwError) { *out_pdwError = dwError; } return FALSE; } TRACE ("\nDriver already loaded, opening it for starting\n"); // try to open the service if(!(hService = OpenService (hSCManager, DriverName, SERVICE_ALL_ACCESS))) { dwError = GetLastError(); TRACE ("\n OpenService() failed:%ld", dwError); CloseServiceHandle (hSCManager); if( out_pdwError) { *out_pdwError = dwError; } return FALSE; } } SERVICE_STATUS ssStatus; dwError = NO_ERROR; if (!StartService (hService, 0, NULL)) { dwError = GetLastError(); if (dwError != ERROR_SERVICE_ALREADY_RUNNING && dwError != SERVICE_ERROR_IGNORE) { TRACE1("\n\nStartService() failed %ld \n",dwError); CloseServiceHandle (hService); CloseServiceHandle (hSCManager); if( out_pdwError) { *out_pdwError = dwError; } return FALSE; } } TRACE ("\nService start pending\n"); {//Service start pending // Check the status until the service is no longer start pending. if (!QueryServiceStatus( hService, // handle to service &ssStatus // address of structure ) ) { if( out_pdwError) { *out_pdwError = dwError; } TRACE ("\nStartService() failed QueryServiceStatus\n"); CloseServiceHandle (hService); CloseServiceHandle (hSCManager); return FALSE; } DWORD dwOldCheckPoint; DWORD dwStartTickCount; DWORD dwWaitTime; // Save the tick count and initial checkpoint. dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; while (ssStatus.dwCurrentState == SERVICE_START_PENDING) { // Do not wait longer than the wait hint. A good interval is // one tenth the wait hint, but no less than 1 second and no // more than 10 seconds. dwWaitTime = ssStatus.dwWaitHint / 10; if( dwWaitTime < 1000 ) dwWaitTime = 1000; else if ( dwWaitTime > 10000 ) dwWaitTime = 10000; Sleep( dwWaitTime ); // Check the status again. if (!QueryServiceStatus( hService, // handle to service &ssStatus // address of structure ) ) { break; } if ( ssStatus.dwCheckPoint > dwOldCheckPoint ) { // The service is making progress. dwStartTickCount = GetTickCount(); dwOldCheckPoint = ssStatus.dwCheckPoint; } else { if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint) { // No progress made within the wait hint break; } } }//end of while } CloseServiceHandle (hService); CloseServiceHandle (hSCManager); if (ssStatus.dwCurrentState != SERVICE_RUNNING) { dwError = GetLastError(); if( out_pdwError) { *out_pdwError = dwError; } TRACE1("\nStartService FAILED. %ld\n", dwError); return FALSE; } TRACE ("\nDriver already running\n"); return TRUE; } BOOL CDriverLoad::DriverUnload(const char *DriverName) { CString strSvcName; strSvcName = DriverName; int nRemoveSvcCount = 0; for( nRemoveSvcCount = 0; nRemoveSvcCount < C_TRY_STARTSVR_COUNT; nRemoveSvcCount ++) { TRACE1("\n CmdRemoveService() %s ", strSvcName); if( CmdRemoveService(strSvcName) ) { return TRUE; } strSvcName.Format("%s%d", DriverName, nRemoveSvcCount); } return FALSE; } BOOL CDriverLoad::CmdRemoveService(const char *DriverName) { SC_HANDLE schService; SC_HANDLE schSCManager; SERVICE_STATUS ServiceStatus; const int C_WAITSTOP_COUNT = 100; schSCManager = OpenSCManager( NULL, // machine (NULL == local) NULL, // database (NULL == default) SC_MANAGER_ALL_ACCESS // access required ); if ( schSCManager ) { schService = OpenService(schSCManager, DriverName, SERVICE_ALL_ACCESS); if (schService) { // try to stop the service if ( ControlService( schService, SERVICE_CONTROL_STOP, &ServiceStatus ) ) { TRACE1(TEXT("Stopping %s."), TEXT(DriverName)); Sleep( 100 ); int nCount = 0; while( QueryServiceStatus( schService, &ServiceStatus ) && nCount < C_WAITSTOP_COUNT) { nCount ++; if ( ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING ) { TRACE(TEXT(".")); Sleep( 100 ); } else break; } } // now remove the service if( DeleteService(schService) ) { CloseServiceHandle(schService); CloseServiceHandle(schSCManager); return TRUE; } CloseServiceHandle(schService); } CloseServiceHandle(schSCManager); } if( GetLastError() == ERROR_DATABASE_DOES_NOT_EXIST) return TRUE; return FALSE; } msn: lxp8@sina.com |
|
|
6楼#
发布于:2008-01-19 20:09
楼上指的是加载和卸载,楼主问的是把驱动安装到系统上,从无到有。
|
|
|