| 5290 | #define SERVICE_CMD_LENGTH (MAX_PATH * 2) |
| 5291 | |
| 5292 | int |
| 5293 | Ext2SetManagerAsService(BOOL bInstall) |
| 5294 | { |
| 5295 | SC_HANDLE hService; |
| 5296 | SC_HANDLE hManager; |
| 5297 | |
| 5298 | CHAR Target[SERVICE_CMD_LENGTH]; |
| 5299 | |
| 5300 | // get the filename of this executable |
| 5301 | if (GetModuleFileName(NULL, Target, SERVICE_CMD_LENGTH - 20) == 0) { |
| 5302 | AfxMessageBox("Unable to install Ext2Mgr as service", |
| 5303 | MB_ICONEXCLAMATION | MB_OK); |
| 5304 | return FALSE; |
| 5305 | } |
| 5306 | |
| 5307 | // append parameters to the end of the path: |
| 5308 | strcat(Target, " -service -hide"); |
| 5309 | |
| 5310 | // open Service Control Manager |
| 5311 | hManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); |
| 5312 | if (hManager == NULL) { |
| 5313 | AfxMessageBox("Ext2Mgr: cannot open Service Control Manager", |
| 5314 | MB_ICONEXCLAMATION | MB_OK); |
| 5315 | return FALSE; |
| 5316 | } |
| 5317 | |
| 5318 | if (bInstall) { |
| 5319 | |
| 5320 | // now create service entry for Ext2Mgr |
| 5321 | hService = CreateService( |
| 5322 | hManager, // SCManager database |
| 5323 | "Ext2Mgr", // name of service |
| 5324 | "Ext2 Volume Manger", // name to display |
| 5325 | SERVICE_ALL_ACCESS, // desired access |
| 5326 | SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS, |
| 5327 | // service type |
| 5328 | SERVICE_AUTO_START, // start type |
| 5329 | SERVICE_ERROR_NORMAL, // error control type |
| 5330 | Target, // service's binary |
| 5331 | NULL, // no load ordering group |
| 5332 | NULL, // no tag identifier |
| 5333 | NULL, // dependencies |
| 5334 | NULL, // LocalSystem account |
| 5335 | NULL); // no password |
| 5336 | |
| 5337 | if (hService == NULL) { |
| 5338 | DWORD error = GetLastError(); |
| 5339 | if (error == ERROR_SERVICE_EXISTS) { |
| 5340 | AfxMessageBox("Ext2Mgr service is already registered.", |
| 5341 | MB_ICONEXCLAMATION | MB_OK); |
| 5342 | } else { |
| 5343 | AfxMessageBox("Ext2Mgr service couldn't be registered.", |
| 5344 | MB_ICONEXCLAMATION | MB_OK); |
| 5345 | } |
| 5346 | } else { |
| 5347 | |
| 5348 | CloseServiceHandle(hService); |
| 5349 |
no test coverage detected