드라이버 로딩 & 드라이버 언로딩

드라이버 로딩 및 언로딩에 사용하기 위한
StartDriver() 와 StopDriver()함수입니다. (MSDN 참조)

OpenService(), StartService().. DeleteService() 등등이 사용되어집니다.
테스트 해보시고 문제점이 있으면 코멘트 부탁 드립니다.

BOOL
StartDriver(LPCTSTR ServiceName,
LPCTSTR Description,
LPCTSTR DriverPath,
DWORD dwStartType)
{
SERVICE_STATUS ServiceStatus;
DWORD dwStartTickCount = 0;
DWORD dwWaitTime = 0;
DWORD dwOldCheckPoint = 0;

SC_HANDLE SCManagerHandle = NULL;
SC_HANDLE SCServiceHandle = NULL;

SCManagerHandle = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(SCManagerHandle == NULL)
{
return FALSE;
}

SCServiceHandle = OpenService(SCManagerHandle,
ServiceName,
SERVICE_ALL_ACCESS);
if(SCServiceHandle == NULL)
{
SCServiceHandle = CreateService(SCManagerHandle,
ServiceName,
Description,
SERVICE_ALL_ACCESS,
SERVICE_KERNEL_DRIVER,
dwStartType,
SERVICE_ERROR_NORMAL,
DriverPath,
NULL,
NULL,
NULL,
NULL,
NULL);
if(SCServiceHandle == NULL)
{
CloseServiceHandle(SCManagerHandle);
return FALSE;
}
}

if(StartService(SCServiceHandle, 0, NULL) == FALSE)
{
CloseServiceHandle(SCServiceHandle);
CloseServiceHandle(SCManagerHandle);
return FALSE;
}

if (QueryServiceStatus(SCServiceHandle, &ServiceStatus) == FALSE)
{
CloseServiceHandle(SCServiceHandle);
CloseServiceHandle(SCManagerHandle);
return FALSE;
}

dwStartTickCount = GetTickCount();
dwOldCheckPoint = ServiceStatus.dwCheckPoint;

while(ServiceStatus.dwCurrentState == SERVICE_START_PENDING)
{
dwWaitTime = ServiceStatus.dwWaitHint / 10;

if(dwWaitTime < 1000)
{
dwWaitTime = 1000;
}
else if(dwWaitTime > 10000)
{
dwWaitTime = 10000;
}

Sleep(dwWaitTime);

if(QueryServiceStatus(SCServiceHandle, &ServiceStatus) == FALSE)
{
break;
}

if(ServiceStatus.dwCheckPoint > dwOldCheckPoint)
{
dwStartTickCount = GetTickCount();
dwOldCheckPoint = ServiceStatus.dwCheckPoint;
}
else
{
if(GetTickCount() – dwStartTickCount > ServiceStatus.dwWaitHint)
{
break;
}
}
}

CloseServiceHandle(SCServiceHandle);
CloseServiceHandle(SCManagerHandle);

if(ServiceStatus.dwCurrentState == SERVICE_RUNNING)
{
return TRUE;
}

return FALSE;
}

BOOL
StopDriver(LPCTSTR ServiceName)
{
SC_HANDLE SCServiceHandle = NULL;
SC_HANDLE SCManagerHandle = NULL;

SCManagerHandle = OpenSCManager(NULL,
NULL,
SC_MANAGER_ALL_ACCESS);

if(SCManagerHandle == NULL)
{
return FALSE;
}

SCServiceHandle = OpenService(SCManagerHandle, ServiceName, SERVICE_ALL_ACCESS);

if(SCServiceHandle == NULL)
{
CloseServiceHandle(SCManagerHandle);
return FALSE;
}

if(ControlService(SCServiceHandle, SERVICE_CONTROL_STOP, &ServiceStatus) == TRUE)
{
Sleep(1000);

while(QueryServiceStatus(SCServiceHandle, &ServiceStatus) == TRUE)
{
if (ServiceStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
Sleep(1000);
}
else
{
break;
}
}

if(ServiceStatus.dwCurrentState != SERVICE_STOPPED)
{
CloseServiceHandle(SCServiceHandle);
CloseServiceHandle(SCManagerHandle);
return FALSE;
}
}

if(DeleteService(SCServiceHandle) == TRUE)
{
CloseServiceHandle(SCServiceHandle);
CloseServiceHandle(SCManagerHandle);
return TRUE;
}

CloseServiceHandle(SCServiceHandle);
CloseServiceHandle(SCManagerHandle);
return FALSE;
}

드라이버 로딩 & 드라이버 언로딩”에 대한 7개의 생각

  1. 핑백: homepage

  2. 핑백: provillus capsules

  3. 핑백: pocket pussy reviews

  4. 핑백: pocket pussy reviews

  5. 핑백: binaryoptionstrading201.com

  6. 핑백: t8 led

  7. 핑백: オンラインカジノ

답글 남기기

이메일은 공개되지 않습니다. 필수 입력창은 * 로 표시되어 있습니다.