Created
August 20, 2013 09:21
-
-
Save vol4ok/6279233 to your computer and use it in GitHub Desktop.
Driver manager module. Allow to load, start, stop and unload driver on all version of Windows
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
#include "drv_mgr.h" | |
SC_HANDLE install_driver(const wchar_t *name, const wchar_t *path) | |
{ | |
SC_HANDLE h_scm; | |
SC_HANDLE h_svc = INVALID_HANDLE_VALUE; | |
if (!path || !name) | |
return INVALID_HANDLE_VALUE; | |
h_scm = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); | |
if (h_scm) { | |
h_svc = CreateServiceW( | |
h_scm,name,name, | |
SERVICE_ALL_ACCESS, | |
SERVICE_KERNEL_DRIVER, | |
SERVICE_DEMAND_START, | |
SERVICE_ERROR_IGNORE, | |
path, NULL, NULL, NULL, NULL, NULL); | |
if (!h_svc) | |
h_svc = OpenServiceW(h_scm, name, SERVICE_ALL_ACCESS); | |
CloseServiceHandle(h_scm); | |
} | |
return h_svc; | |
} | |
SC_HANDLE open_driver(const wchar_t *name) | |
{ | |
SC_HANDLE h_scm; | |
SC_HANDLE h_svc = NULL; | |
if (!name) | |
return INVALID_HANDLE_VALUE; | |
h_scm = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS); | |
if (h_scm) { | |
h_svc = OpenServiceW(h_scm, name, SERVICE_ALL_ACCESS); | |
CloseServiceHandle(h_scm); | |
} | |
return h_svc; | |
} | |
DWORD query_status(SC_HANDLE h_svc) | |
{ | |
SERVICE_STATUS_PROCESS ssp; | |
ULONG bytes; | |
ssp.dwCurrentState = 0; | |
QueryServiceStatusEx( | |
h_svc, | |
SC_STATUS_PROCESS_INFO, | |
(LPBYTE)&ssp, | |
sizeof(SERVICE_STATUS_PROCESS), | |
&bytes); | |
return ssp.dwCurrentState; | |
} | |
int start_driver(SC_HANDLE h_svc) | |
{ | |
ULONG status = 0; | |
if (!h_svc) | |
return 0; | |
status = query_status(h_svc); | |
if (status != SERVICE_RUNNING) | |
return StartServiceW(h_svc, 0, NULL); | |
return 1; | |
} | |
int stop_driver(SC_HANDLE h_svc) | |
{ | |
ULONG status = 0; | |
SERVICE_STATUS svc_status; | |
if (!h_svc) | |
return 0; | |
status = query_status(h_svc); | |
if (status != SERVICE_STOPPED) | |
return ControlService(h_svc, SERVICE_CONTROL_STOP, &svc_status); | |
return 1; | |
} | |
int uninstall_driver(SC_HANDLE h_svc) | |
{ | |
ULONG status = 0; | |
status = DeleteService(h_svc); | |
CloseServiceHandle(h_svc); | |
return status; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _DRV_MGR_ | |
#define _DRV_MGR_ | |
SC_HANDLE install_driver(const wchar_t *name, const wchar_t *path); | |
SC_HANDLE open_driver(const wchar_t *name); | |
int start_driver(SC_HANDLE h_svc); | |
int stop_driver(SC_HANDLE h_svc); | |
DWORD query_status(SC_HANDLE h_svc) ; | |
int uninstall_driver(SC_HANDLE h_svc); | |
#endif /* _DRV_MGR_ */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment