NDASDI_API BOOL WINAPI NdasDiInstallDriverService( LPCTSTR SourceFilePath, LPCTSTR ServiceName, LPCTSTR DisplayName, LPCTSTR Description, DWORD ServiceType, DWORD StartType, DWORD ErrorControl, LPCTSTR LoadOrderGroup, LPDWORD lpdwTagId, LPCTSTR Dependencies) { BOOL fSuccess = FALSE; HRESULT hr = S_FALSE; const TCHAR DRIVERS_DIR[] = _T("\\DRIVERS\\"); TCHAR szDriverFilePath[MAX_PATH] = {0}; DPInfo(_FT("Installing a driver service") _T(" from %s as %s(%s), load order %s, depends %s.\n"), SourceFilePath, DisplayName, ServiceName, LoadOrderGroup, Dependencies); LPTSTR lpFileName = NULL; LPTSTR lpszFullSrcPath = pResolveFullPath( SourceFilePath, NULL, &lpFileName, 0); if (NULL == lpszFullSrcPath) { DPErrorEx(_FT("Resolving full path of %s failed: "), SourceFilePath); return FALSE; } // We should LocalFree(lpszFullSrcPath). // Instead of manual Freeing, attach it to autoresource AutoHLocal hLocal = (HLOCAL) lpszFullSrcPath; // gets c:\windows\system32 UINT cch = GetSystemDirectory(szDriverFilePath, MAX_PATH); if (0 == cch) { DPErrorEx(_FT("Getting a system directory failed: ")); return FALSE; } // append \drivers\ -> "c:\windows\system32\drivers\" hr = StringCchCat(szDriverFilePath, MAX_PATH, DRIVERS_DIR); _ASSERTE(SUCCEEDED(hr)); if (FAILED(hr)) { DPErrorEx(_FT("Concating %s, %s failed: "), szDriverFilePath, DRIVERS_DIR); return FALSE; } // append mydriver.sys -> "c:\windows\system32\drivers\mydrivers.sys" hr = StringCchCat(szDriverFilePath, MAX_PATH, lpFileName); _ASSERTE(SUCCEEDED(hr)); if (FAILED(hr)) { DPErrorEx(_FT("Concating %s, %s failed: "), szDriverFilePath, lpFileName); return FALSE; } // copy to the system driver path DPInfo(_FT("Copying %s to %s.\n"), lpszFullSrcPath, szDriverFilePath); fSuccess = CopyFile(lpszFullSrcPath, szDriverFilePath, FALSE); if (!fSuccess) { DPErrorEx(_FT("Copying %s to %s failed: "), lpszFullSrcPath, szDriverFilePath); return FALSE; } // call actual service installer return NdasDiInstallService( ServiceName, DisplayName, Description, SERVICE_ALL_ACCESS, ServiceType, StartType, SERVICE_ERROR_NORMAL, szDriverFilePath, LoadOrderGroup, lpdwTagId, Dependencies, NULL, NULL); }
int svcinst(int argc, LPTSTR *argv) { typedef enum { SVC_INSTALL, SVC_REMOVE, SVC_FIND } COMMAND_TYPE; struct { COMMAND_TYPE type; LPCTSTR cmd; int argc; BOOL op; } opts[] = { { SVC_INSTALL, L"install", 3, FALSE}, { SVC_REMOVE, L"remove", 1, FALSE}, { SVC_FIND, L"find", 1, FALSE}, }; COMMAND_TYPE CmdType; const DWORD nopts = RTL_NUMBER_OF(opts); DWORD i = 0; for (; i < nopts; ++i) { if (lstrcmpi(opts[i].cmd, argv[0]) == 0) { if ((argc - 1) != opts[i].argc) { _ftprintf(stderr, _T("Invalid command arguments.\n")); return 254; } CmdType = opts[i].type; break; } } if (i == nopts) { _ftprintf(stderr, _T("Invalid command.\n")); return 254; } if (SVC_INSTALL == CmdType) { LPTSTR szBinaryPath = argv[1]; LPTSTR szServiceName = argv[2]; LPTSTR szDisplayName = argv[3]; BOOL fSuccess = NdasDiInstallService( szServiceName, szDisplayName, _T(""), SERVICE_WIN32_OWN_PROCESS, SERVICE_DEMAND_START, SERVICE_ERROR_NORMAL, szBinaryPath, NULL, NULL, NULL, NULL, NULL); if (!fSuccess) { printErrorText(); return 254; } else { _ftprintf(stdout, _T("Service %s installed successfully.\n"), szServiceName); return 0; } } else if (SVC_REMOVE == CmdType) { LPTSTR szServiceName = argv[1]; BOOL fSuccess = NdasDiDeleteService(szServiceName); if (!fSuccess) { printErrorText(); return 254; } else { _ftprintf(stdout, _T("Service %s deleted successfully.\n"), szServiceName); return 0; } } else if (SVC_FIND == CmdType) { LPTSTR szServiceName = argv[1]; BOOL fPendingDeletion; BOOL fSuccess = NdasDiFindService(szServiceName, &fPendingDeletion); if (!fSuccess) { printErrorText(); return 254; } else { _ftprintf(stdout, _T("Service %s exists.%s\n"), szServiceName, fPendingDeletion ? _T(" (Marked for deletion)") : _T("")); return 0; } } else { _ftprintf(stderr, _T("Unknown command.\n")); return 254; } }