static OsPath DriverPathname() { const char* const bits = Is64BitOs()? "64" : ""; #ifdef NDEBUG const char* const debug = ""; #else const char* const debug = "d"; #endif char filename[PATH_MAX]; sprintf_s(filename, ARRAY_SIZE(filename), "aken%s%s.sys", bits, debug); return wutil_ExecutablePath() / filename; }
int DoDriverInstall() { SC_HANDLE hManager, hService = NULL; BOOL bOK = FALSE, bRet; hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hManager == NULL) goto error; hService = CreateService (hManager, L"Bluefisher", L"Bluefisher", SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_SYSTEM_START, SERVICE_ERROR_NORMAL, !Is64BitOs () ? L"System32\\drivers\\bluefish.sys" : L"SysWOW64\\drivers\\bluefish-x64.sys", NULL, NULL, NULL, NULL, NULL); if (hService == NULL) goto error; else CloseServiceHandle (hService); hService = OpenService (hManager, L"Bluefisher", SERVICE_ALL_ACCESS); if (hService == NULL) goto error; bRet = StartService (hService, 0, NULL); if (bRet == FALSE) goto error; bOK = TRUE; error: if (bOK == FALSE && GetLastError () != ERROR_SERVICE_ALREADY_RUNNING) { MessageBoxW (NULL, L"DRIVER_INSTALL_FAILED", L"Failed to install the driver.", MB_ICONHAND); } else bOK = TRUE; if (hService != NULL) CloseServiceHandle (hService); if (hManager != NULL) CloseServiceHandle (hManager); return 0; }
// Install and start driver service and mark it for removal (non-install mode) static int DriverLoad () { HANDLE file = INVALID_HANDLE_VALUE; WIN32_FIND_DATA find = {0}; SC_HANDLE hManager = NULL, hService = NULL; WCHAR driverPath[MAX_PATH*2] = {0}; BOOL res = FALSE; WCHAR *tmp; GetModuleFileName (NULL, driverPath, sizeof (driverPath)); tmp = wcsrchr (driverPath, '\\'); if (!tmp) { wcscpy_s(driverPath, MAX_PATH * 2, L"."); tmp = driverPath + 1; } wcscpy_s (tmp, MAX_PATH, !Is64BitOs () ? L"\\Bluefish.sys" : L"\\Bluefish-x64.sys"); file = FindFirstFile (driverPath, &find); if (file == INVALID_HANDLE_VALUE) { wprintf (L"DRIVER_NOT_FOUND, was the driver stored in %s?\n", driverPath ); return ERR_DONT_REPORT; } FindClose (file); hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS); if (hManager == NULL) { if (GetLastError () == ERROR_ACCESS_DENIED) { wprintf (L"ADMIN_PRIVILEGES_DRIVER No admin privileges of this driver.\n", 0); return ERR_DONT_REPORT; } return ERR_OS_ERROR; } hService = OpenService (hManager, L"Bluefisher", SERVICE_ALL_ACCESS); if (hService != NULL) { // Remove stale service (driver is not loaded but service exists) wprintf(L"Driver is not loaded but the service exits.\n"); DeleteService (hService); CloseServiceHandle (hService); Sleep (500); } //Clear the Error SetLastError(0); wprintf( L"Installing Bluefisher driver...\n"); wprintf( L"Driver Path: %s\n", driverPath ); hService = CreateService (hManager, L"Bluefisher", L"Bluefisher", SERVICE_ALL_ACCESS, SERVICE_KERNEL_DRIVER, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, driverPath, NULL, NULL, NULL, NULL, NULL); if (hService == NULL) { wprintf(L"Bluefisher was not created. Error: %d.\n", GetLastError() ); SetLastError(0); CloseServiceHandle (hManager); return ERR_OS_ERROR; } CloseServiceHandle (hService); hService = OpenService (hManager, L"Bluefisher", SERVICE_ALL_ACCESS); if (hService == NULL) { wprintf(L"Bluefisher was not opened. Error: %d.\n", GetLastError() ); SetLastError(0); CloseServiceHandle (hManager); return ERR_OS_ERROR; } if( !StartService (hService, 0, NULL) ) { wprintf(L"Bluefisher was not started. Error: %d.\n", GetLastError() ); SetLastError(0); CloseServiceHandle (hManager); } //DeleteService (hService); CloseServiceHandle (hManager); CloseServiceHandle (hService); if( GetLastError() != 0 ) { wprintf(L"Error: %d.\n", GetLastError() ); }else { wprintf(L"Bluefisher driver installed successfully.\n" ); } return !res ? ERR_OS_ERROR : ERROR_SUCCESS; }