1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| #include <windows.h>
#include <stdio.h>
#include <stdlib.h>
BOOL InstallDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName, IN LPCTSTR ServiceExe);
BOOL RemoveDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName);
BOOL StartDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName);
BOOL StopDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName);
BOOL OpenDevice(IN LPCTSTR DriverName);
void __cdecl main(IN int argc, IN char *argv[])
{
SC_HANDLE schSCManager =NULL;
if(argc != 3)
{
printf("usage:\n");
printf("\t instdrv <driver name> <.sys location>\n");
printf("\t instdrv <driver name> remove\n\n");
exit(1);
}
schSCManager =OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if(!_stricmp(argv[2], "remove"))
{
StopDriver(schSCManager, argv[1]);
RemoveDriver(schSCManager, argv[1]);
}
else
{
InstallDriver(schSCManager, argv[1], argv[2]);
StartDriver(schSCManager, argv[1]);
OpenDevice(argv[1]);
}
CloseServiceHandle(schSCManager);
}
BOOL InstallDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName, IN LPCTSTR ServiceExe)
{
SC_HANDLE schService =NULL;
DWORD err;
schService = CreateService (SchSCManager, DriverName, DriverName, SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER,
SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, ServiceExe, NULL, NULL, NULL, NULL, NULL);
if(schService == NULL)
{
err =GetLastError();
if(err == ERROR_SERVICE_EXISTS){printf("failure: CreateService, ERROR_SERVICE_EXISTS\n");}
else{printf("failure: CreateService (0x%02x)\n", err);}
return FALSE;
}
else{printf("CreateService SUCCESS\n");}
CloseServiceHandle(schService);
return TRUE;
}
BOOL RemoveDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName)
{
SC_HANDLE schService =NULL;
BOOL ret;
schService =OpenService(SchSCManager, DriverName, SERVICE_ALL_ACCESS);
if(schService == NULL)
{
printf("failure: OpenService (0x%02x)\n", GetLastError());
return FALSE;
}
ret =DeleteService(schService);
if(ret){printf("DeleteService SUCCESS\n");}
else{printf("failure: DeleteService (0x%02x)\n", GetLastError());}
CloseServiceHandle(schService);
return ret;
}
BOOL StartDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName)
{
SC_HANDLE schService =NULL;
BOOL ret;
DWORD err;
schService =OpenService(SchSCManager, DriverName, SERVICE_ALL_ACCESS);
if(schService == NULL)
{
printf("failure: OpenService (0x%02x)\n", GetLastError());
return FALSE;
}
ret =StartService(schService, 0, NULL);
if(ret){printf("StartService SUCCESS\n");}
else
{
err =GetLastError();
if(err == ERROR_SERVICE_ALREADY_RUNNING){printf("failure: StartService, ERROR_SERVICE_ALREADY_RUNNING\n");}
else{printf("failure: StartService (0x%02x)\n", err);}
}
CloseServiceHandle(schService);
return ret;
}
BOOL StopDriver(IN SC_HANDLE SchSCManager, IN LPCTSTR DriverName)
{
SC_HANDLE schService =NULL;
BOOL ret;
SERVICE_STATUS serviceStatus;
schService =OpenService(SchSCManager, DriverName, SERVICE_ALL_ACCESS);
if (schService == NULL)
{
printf("failure: OpenService (0x%02x)\n", GetLastError());
return FALSE;
}
ret =ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus);
if (ret){printf("ControlService SUCCESS\n");}
else{printf("failure: ControlService (0x%02x)\n", GetLastError());}
CloseServiceHandle(schService);
return ret;
}
BOOL OpenDevice(IN LPCTSTR DriverName)
{
char completeDeviceName[64] = "";
LPCTSTR dosDeviceName =DriverName;
HANDLE hDevice =NULL;
BOOL ret;
strcat(completeDeviceName, "\\\\.\\");
strcat(completeDeviceName, dosDeviceName);
hDevice =CreateFile(completeDeviceName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hDevice == ((HANDLE)-1))
{
printf("Can't get a handle to %s\n", completeDeviceName);
ret =FALSE;
}
else
{
printf("CreateFile SUCCESS\n");
CloseHandle(hDevice);
ret =TRUE;
}
return ret;
} |
Partager