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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
| #include <windows.h> #include <tchar.h> #include <Commctrl.h> #include <strsafe.h> #include "resource.h" #pragma comment(lib, "Comctl32.lib") #pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"") typedef struct _ITEMDATA{ TCHAR m_szServiceName[256]; TCHAR m_szDisplayName[256]; DWORD m_dwCurrentState; DWORD m_dwControlsAccepted; DWORD m_dwStartType; }ITEMDATA, * PITEMDATA;
HINSTANCE g_hInstance; HWND g_hwndDlg; HWND g_hwndList; BOOL g_bAscendingDisplayName; BOOL g_bAscendingCurrentState; BOOL g_bAscendingStartType; LPCTSTR arrlpStrCurrentState[] = { TEXT(""), TEXT("已停止"), TEXT("正在启动"), TEXT("正在停止"),TEXT("正在运行"), TEXT("正在继续"), TEXT("正在暂停"), TEXT("已暂停") }; LPCTSTR arrlpStrStartType[] = { TEXT("自动(用于驱动程序服务)"), TEXT("手动(用于驱动程序服务)"),TEXT("自动"), TEXT("手动"), TEXT("禁用") };
INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam); INT_PTR CALLBACK DialogProcCreateService(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
BOOL GetServiceList();
int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort);
VOID QueryServiceStatusAndConfig();
BOOL StartTheService();
BOOL ControlCurrentState(DWORD dwControl, DWORD dwNewCurrentState);
BOOL ChangeTheServiceConfig(DWORD dwStartType);
BOOL CreateAService(LPCTSTR lpBinaryPathName, LPCTSTR lpServiceName, LPCTSTR lpDisplayName, DWORD dwStartType, DWORD dwServiceType = SERVICE_WIN32_OWN_PROCESS, LPCTSTR lpDescription = NULL, DWORD dwDesiredAccess = SERVICE_ALL_ACCESS, DWORD dwErrorControl = SERVICE_ERROR_NORMAL);
BOOL DeleteTheService(); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { g_hInstance = hInstance; DialogBoxParam(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DialogProc, NULL); return 0; }; INT_PTR CALLBACK DialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam){ INT_PTR nResult; switch (uMsg){ case WM_INITDIALOG: { g_hwndDlg = hwndDlg; g_hwndList = GetDlgItem(hwndDlg, IDC_LIST_SERVICE); SendMessage(g_hwndList, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES); LVCOLUMN lvc; ZeroMemory(&lvc, sizeof(LVCOLUMN)); lvc.mask = LVCF_SUBITEM | LVCF_WIDTH | LVCF_TEXT; lvc.iSubItem = 0; lvc.cx = 260; lvc.pszText = (LPTSTR)TEXT("服务的显示名称"); SendMessage(g_hwndList, LVM_INSERTCOLUMN, 0, (LPARAM)&lvc); lvc.iSubItem = 1; lvc.cx = 80; lvc.pszText = (LPTSTR)TEXT("服务状态"); SendMessage(g_hwndList, LVM_INSERTCOLUMN, 1, (LPARAM)&lvc); lvc.iSubItem = 2; lvc.cx = 80; lvc.pszText = (LPTSTR)TEXT("启动类型"); SendMessage(g_hwndList, LVM_INSERTCOLUMN, 2, (LPARAM)&lvc); lvc.iSubItem = 3; lvc.cx = 300; lvc.pszText = (LPTSTR)TEXT("文件路径"); SendMessage(g_hwndList, LVM_INSERTCOLUMN, 3, (LPARAM)&lvc); lvc.iSubItem = 4; lvc.cx = 300; lvc.pszText = (LPTSTR)TEXT("服务描述"); SendMessage(g_hwndList, LVM_INSERTCOLUMN, 4, (LPARAM)&lvc); GetServiceList(); return TRUE; }; case WM_SIZE: { MoveWindow(g_hwndList, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE); return TRUE; }; case WM_NOTIFY: { if (((LPNMHDR)lParam)->code == LVN_COLUMNCLICK) { LPNMLISTVIEW pNMLV = (LPNMLISTVIEW)lParam; if (pNMLV->iSubItem < 3) { SendMessage(g_hwndList, LVM_SORTITEMS, pNMLV->iSubItem, (LPARAM)CompareFunc); switch (pNMLV->iSubItem) { case 0: { g_bAscendingDisplayName = ~g_bAscendingDisplayName; break; }; case 1: { g_bAscendingCurrentState = ~g_bAscendingCurrentState; break; }; case 2: { g_bAscendingStartType = ~g_bAscendingStartType; break; }; }; }; } else if (((LPNMHDR)lParam)->code == NM_RCLICK) { if (((LPNMITEMACTIVATE)lParam)->iItem < 0) return FALSE; POINT pt = { 0 }; GetCursorPos(&pt); TrackPopupMenu(GetSubMenu(GetMenu(hwndDlg), 0), TPM_LEFTALIGN | TPM_TOPALIGN, pt.x, pt.y, 0, hwndDlg, NULL); }; return TRUE; }; case WM_INITMENUPOPUP: { QueryServiceStatusAndConfig(); return TRUE; }; case WM_COMMAND: { switch (LOWORD(wParam)) { case ID_SERVICE_ADD: { nResult = DialogBoxParam(g_hInstance, MAKEINTRESOURCE(IDD_CREATESERVICE), hwndDlg, DialogProcCreateService, NULL); if (nResult == 2) MessageBox(hwndDlg, TEXT("创建服务成功"), TEXT("成功提示"), MB_OK); else if (nResult == 1) MessageBox(hwndDlg, TEXT("创建服务失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_DELETE: { if (DeleteTheService()) MessageBox(hwndDlg, TEXT("删除服务成功"), TEXT("成功提示"), MB_OK); else MessageBox(hwndDlg, TEXT("删除服务失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_START: { if (StartTheService()) MessageBox(hwndDlg, TEXT("启动服务成功"), TEXT("成功提示"), MB_OK); else MessageBox(hwndDlg, TEXT("启动服务失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_STOP: { if (ControlCurrentState(SERVICE_CONTROL_STOP, SERVICE_STOPPED)) MessageBox(hwndDlg, TEXT("停止服务成功"), TEXT("成功提示"), MB_OK); else MessageBox(hwndDlg, TEXT("停止服务失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_PAUSE: { if (ControlCurrentState(SERVICE_CONTROL_PAUSE, SERVICE_PAUSED)) MessageBox(hwndDlg, TEXT("暂停服务成功"), TEXT("成功提示"), MB_OK); else MessageBox(hwndDlg, TEXT("暂停服务失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_CONTINUE: { if (ControlCurrentState(SERVICE_CONTROL_CONTINUE, SERVICE_RUNNING)) MessageBox(hwndDlg, TEXT("继续服务成功"), TEXT("成功提示"), MB_OK); else MessageBox(hwndDlg, TEXT("继续服务失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_AUTO: { if (ChangeTheServiceConfig(SERVICE_AUTO_START)) MessageBox(hwndDlg, TEXT("设为自动启动成功"), TEXT("成功提示"), MB_OK); else MessageBox(hwndDlg, TEXT("设为自动启动失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_DEMAND: { if (ChangeTheServiceConfig(SERVICE_DEMAND_START)) MessageBox(hwndDlg, TEXT("设为手动启动成功"), TEXT("成功提示"), MB_OK); else MessageBox(hwndDlg, TEXT("设为手动启动失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_DISABLE: { if (ChangeTheServiceConfig(SERVICE_DISABLED)) MessageBox(hwndDlg, TEXT("设为已禁用成功"), TEXT("成功提示"), MB_OK); else MessageBox(hwndDlg, TEXT("设为已禁用失败"), TEXT("错误提示"), MB_OK); break; }; case ID_SERVICE_REFRESH: { g_bAscendingDisplayName = FALSE; GetServiceList(); break; }; case IDCANCEL: { EndDialog(hwndDlg, 0); break; }; }; return TRUE; }; }; return FALSE; }; BOOL GetServiceList() { SC_HANDLE hSCManager = NULL; LPBYTE lpServices = NULL; DWORD dwcbBufSize = 0; DWORD dwcbBytesNeeded; DWORD dwServicesReturned; DWORD dwResumeHandle = 0; LVITEM lvi = { 0 }; hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE); if (!hSCManager) return FALSE; EnumServicesStatusEx(hSCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, NULL, dwcbBufSize, &dwcbBytesNeeded, &dwServicesReturned, &dwResumeHandle, NULL); lpServices = new BYTE[dwcbBytesNeeded]; ZeroMemory(lpServices, dwcbBytesNeeded); dwResumeHandle = 0; EnumServicesStatusEx(hSCManager, SC_ENUM_PROCESS_INFO, SERVICE_WIN32, SERVICE_STATE_ALL, lpServices, dwcbBytesNeeded, &dwcbBytesNeeded, &dwServicesReturned, &dwResumeHandle, NULL); ZeroMemory(&lvi, sizeof(LVITEM)); int nCount = SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0); for (int i = 0; i < nCount; i++) { lvi.iItem = i; lvi.mask = LVIF_PARAM; SendMessage(g_hwndList, LVM_GETITEM, 0, (LPARAM)&lvi); delete (PITEMDATA)(lvi.lParam); }; SendMessage(g_hwndList, LVM_DELETEALLITEMS, 0, 0); LPENUM_SERVICE_STATUS_PROCESS pEnumServiceStatus = (LPENUM_SERVICE_STATUS_PROCESS)lpServices; ZeroMemory(&lvi, sizeof(LVITEM)); for (DWORD i = 0; i < dwServicesReturned; i++) { PITEMDATA pItemData = new ITEMDATA; ZeroMemory(pItemData, sizeof(ITEMDATA)); StringCchCopy(pItemData->m_szServiceName, _countof(pItemData->m_szServiceName), pEnumServiceStatus[i].lpServiceName); StringCchCopy(pItemData->m_szDisplayName, _countof(pItemData->m_szDisplayName), pEnumServiceStatus[i].lpDisplayName); pItemData->m_dwCurrentState = pEnumServiceStatus[i].ServiceStatusProcess.dwCurrentState; pItemData->m_dwControlsAccepted = pEnumServiceStatus[i].ServiceStatusProcess.dwControlsAccepted; lvi.iItem = SendMessage(g_hwndList, LVM_GETITEMCOUNT, 0, 0); lvi.mask = LVIF_TEXT | LVIF_PARAM; lvi.lParam = (LPARAM)pItemData; lvi.iSubItem = 0; lvi.pszText = pEnumServiceStatus[i].lpDisplayName; SendMessage(g_hwndList, LVM_INSERTITEM, 0, (LPARAM)&lvi); lvi.mask = LVIF_TEXT; lvi.iSubItem = 1; lvi.pszText = (LPTSTR)arrlpStrCurrentState[pEnumServiceStatus[i].ServiceStatusProcess.dwCurrentState]; SendMessage(g_hwndList, LVM_SETITEM, 0, (LPARAM)&lvi); SC_HANDLE hService; LPQUERY_SERVICE_CONFIG lpServiceConfig = NULL; DWORD dwcbBufSizeService = 0; DWORD dwcbBytesNeededService; hService = OpenService(hSCManager, pEnumServiceStatus[i].lpServiceName, SERVICE_QUERY_CONFIG); QueryServiceConfig(hService, NULL, 0, &dwcbBytesNeededService); lpServiceConfig = (LPQUERY_SERVICE_CONFIG)new BYTE[dwcbBytesNeededService]; ZeroMemory(lpServiceConfig, dwcbBytesNeededService); QueryServiceConfig(hService, lpServiceConfig, dwcbBytesNeededService, &dwcbBytesNeededService); lvi.iSubItem = 2; lvi.pszText = (LPTSTR)arrlpStrStartType[lpServiceConfig->dwStartType]; SendMessage(g_hwndList, LVM_SETITEM, 0, (LPARAM)&lvi); pItemData->m_dwStartType = lpServiceConfig->dwStartType; lvi.iSubItem = 3; lvi.pszText = lpServiceConfig->lpBinaryPathName; SendMessage(g_hwndList, LVM_SETITEM, 0, (LPARAM)&lvi); delete[]lpServiceConfig; LPBYTE lpBufferService2; DWORD dwcbBufSizeService2 = 0; DWORD dwcbBytesNeededService2; QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, NULL, 0, &dwcbBytesNeededService2); lpBufferService2 = new BYTE[dwcbBytesNeededService2]; ZeroMemory(lpBufferService2, dwcbBytesNeededService2); QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, lpBufferService2, dwcbBytesNeededService2, &dwcbBytesNeededService2); lvi.iSubItem = 4; lvi.pszText = ((LPSERVICE_DESCRIPTION)lpBufferService2)->lpDescription; SendMessage(g_hwndList, LVM_SETITEM, 0, (LPARAM)&lvi); delete[]lpBufferService2; CloseServiceHandle(hService); }; SendMessage(g_hwndList, LVM_SORTITEMS, 0, (LPARAM)CompareFunc); g_bAscendingDisplayName = ~g_bAscendingDisplayName; delete[]lpServices; CloseServiceHandle(hSCManager); return TRUE; }; int CALLBACK CompareFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort) { LPCTSTR lpStr1, lpStr2; switch (lParamSort) { case 0: { lpStr1 = ((PITEMDATA)lParam1)->m_szDisplayName; lpStr2 = ((PITEMDATA)lParam2)->m_szDisplayName; if (!g_bAscendingDisplayName) return CompareStringEx(LOCALE_NAME_USER_DEFAULT, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, lpStr1, -1, lpStr2, -1, NULL, NULL, NULL) - 2; else return CompareStringEx(LOCALE_NAME_USER_DEFAULT, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, lpStr2, -1, lpStr1, -1, NULL, NULL, NULL) - 2; break; }; case 1: { lpStr1 = arrlpStrCurrentState[((PITEMDATA)lParam1)->m_dwCurrentState]; lpStr2 = arrlpStrCurrentState[((PITEMDATA)lParam2)->m_dwCurrentState]; if (!g_bAscendingCurrentState) return CompareStringEx(LOCALE_NAME_USER_DEFAULT, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, lpStr1, -1, lpStr2, -1, NULL, NULL, NULL) - 2; else return CompareStringEx(LOCALE_NAME_USER_DEFAULT, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, lpStr2, -1, lpStr1, -1, NULL, NULL, NULL) - 2; break; }; case 2: { lpStr1 = arrlpStrStartType[((PITEMDATA)lParam1)->m_dwStartType]; lpStr2 = arrlpStrStartType[((PITEMDATA)lParam2)->m_dwStartType]; if (!g_bAscendingStartType) return CompareStringEx(LOCALE_NAME_USER_DEFAULT, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, lpStr1, -1, lpStr2, -1, NULL, NULL, NULL) - 2; else return CompareStringEx(LOCALE_NAME_USER_DEFAULT, LINGUISTIC_IGNORECASE | SORT_DIGITSASNUMBERS, lpStr2, -1, lpStr1, -1, NULL, NULL, NULL) - 2; break; }; }; return 0; }; VOID QueryServiceStatusAndConfig() { int nSelected; LVITEM lvi = { 0 }; PITEMDATA pItemData; HMENU hMenu = GetMenu(g_hwndDlg); nSelected = SendMessage(g_hwndList, LVM_GETSELECTIONMARK, 0, 0); if (nSelected < 0) { EnableMenuItem(hMenu, ID_SERVICE_DELETE, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_START, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_STOP, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_PAUSE, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_CONTINUE, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_AUTO, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_DEMAND, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_DISABLE, MF_BYCOMMAND | MF_DISABLED); return; } else { EnableMenuItem(hMenu, ID_SERVICE_DELETE, MF_BYCOMMAND | MF_ENABLED); EnableMenuItem(hMenu, ID_SERVICE_START, MF_BYCOMMAND | MF_ENABLED); EnableMenuItem(hMenu, ID_SERVICE_STOP, MF_BYCOMMAND | MF_ENABLED); EnableMenuItem(hMenu, ID_SERVICE_PAUSE, MF_BYCOMMAND | MF_ENABLED); EnableMenuItem(hMenu, ID_SERVICE_CONTINUE, MF_BYCOMMAND | MF_ENABLED); EnableMenuItem(hMenu, ID_SERVICE_AUTO, MF_BYCOMMAND | MF_ENABLED); EnableMenuItem(hMenu, ID_SERVICE_DEMAND, MF_BYCOMMAND | MF_ENABLED); EnableMenuItem(hMenu, ID_SERVICE_DISABLE, MF_BYCOMMAND | MF_ENABLED); }; lvi.mask = LVIF_PARAM; lvi.iItem = nSelected; SendMessage(g_hwndList, LVM_GETITEM, 0, (LPARAM)&lvi); pItemData = (PITEMDATA)(lvi.lParam); if (pItemData->m_dwCurrentState == SERVICE_START_PENDING || pItemData->m_dwCurrentState == SERVICE_RUNNING) { EnableMenuItem(hMenu, ID_SERVICE_START, MF_BYCOMMAND | MF_DISABLED); if (!(pItemData->m_dwControlsAccepted & SERVICE_ACCEPT_STOP)) EnableMenuItem(hMenu, ID_SERVICE_STOP, MF_BYCOMMAND | MF_DISABLED); if (!(pItemData->m_dwControlsAccepted & SERVICE_ACCEPT_PAUSE_CONTINUE)) EnableMenuItem(hMenu, ID_SERVICE_PAUSE, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_CONTINUE, MF_BYCOMMAND | MF_DISABLED); } else if (pItemData->m_dwCurrentState == SERVICE_STOP_PENDING || pItemData->m_dwCurrentState == SERVICE_STOPPED) { EnableMenuItem(hMenu, ID_SERVICE_STOP, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_PAUSE, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_CONTINUE, MF_BYCOMMAND | MF_DISABLED); } else if (pItemData->m_dwCurrentState == SERVICE_PAUSE_PENDING || pItemData->m_dwCurrentState == SERVICE_PAUSED) { EnableMenuItem(hMenu, ID_SERVICE_START, MF_BYCOMMAND | MF_DISABLED); if (!(pItemData->m_dwControlsAccepted & SERVICE_ACCEPT_STOP)) EnableMenuItem(hMenu, ID_SERVICE_STOP, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_PAUSE, MF_BYCOMMAND | MF_DISABLED); if (!(pItemData->m_dwControlsAccepted & SERVICE_ACCEPT_PAUSE_CONTINUE)) EnableMenuItem(hMenu, ID_SERVICE_CONTINUE, MF_BYCOMMAND | MF_DISABLED); } else if (pItemData->m_dwCurrentState == SERVICE_CONTINUE_PENDING) { EnableMenuItem(hMenu, ID_SERVICE_START, MF_BYCOMMAND | MF_DISABLED); if (!(pItemData->m_dwControlsAccepted & SERVICE_ACCEPT_STOP)) EnableMenuItem(hMenu, ID_SERVICE_STOP, MF_BYCOMMAND | MF_DISABLED); if (!(pItemData->m_dwControlsAccepted & SERVICE_ACCEPT_PAUSE_CONTINUE)) EnableMenuItem(hMenu, ID_SERVICE_PAUSE, MF_BYCOMMAND | MF_DISABLED); EnableMenuItem(hMenu, ID_SERVICE_CONTINUE, MF_BYCOMMAND | MF_DISABLED); }; if (pItemData->m_dwStartType == SERVICE_AUTO_START) EnableMenuItem(hMenu, ID_SERVICE_AUTO, MF_BYCOMMAND | MF_DISABLED); else if (pItemData->m_dwStartType == SERVICE_DEMAND_START) EnableMenuItem(hMenu, ID_SERVICE_DEMAND, MF_BYCOMMAND | MF_DISABLED); else if (pItemData->m_dwStartType == SERVICE_DISABLED) EnableMenuItem(hMenu, ID_SERVICE_DISABLE, MF_BYCOMMAND | MF_DISABLED); return; }; BOOL StartTheService() { int nSelected; LVITEM lvi = { 0 }; PITEMDATA pItemData; SC_HANDLE hSCManager = NULL; SC_HANDLE hService; nSelected = SendMessage(g_hwndList, LVM_GETSELECTIONMARK, 0, 0); lvi.mask = LVIF_PARAM; lvi.iItem = nSelected; SendMessage(g_hwndList, LVM_GETITEM, 0, (LPARAM)&lvi); pItemData = (PITEMDATA)(lvi.lParam); hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); if (!hSCManager) return FALSE; hService = OpenService(hSCManager, pItemData->m_szServiceName, SERVICE_START); if (!hService) { CloseServiceHandle(hSCManager); return FALSE; }; if (!StartService(hService, 0, NULL)) { CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return FALSE; }; pItemData->m_dwCurrentState = SERVICE_RUNNING; lvi.mask = LVIF_TEXT; lvi.iItem = nSelected; lvi.iSubItem = 1; lvi.pszText = (LPTSTR)arrlpStrCurrentState[SERVICE_RUNNING]; SendMessage(g_hwndList, LVM_SETITEM, 0, (LPARAM)&lvi); CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return TRUE; }; BOOL ControlCurrentState(DWORD dwControl, DWORD dwNewCurrentState) { int nSelected; LVITEM lvi = { 0 }; PITEMDATA pItemData; SC_HANDLE hSCManager = NULL; SC_HANDLE hService; SERVICE_STATUS serviceStatus = { 0 }; nSelected = SendMessage(g_hwndList, LVM_GETSELECTIONMARK, 0, 0); lvi.mask = LVIF_PARAM; lvi.iItem = nSelected; SendMessage(g_hwndList, LVM_GETITEM, 0, (LPARAM)&lvi); pItemData = (PITEMDATA)(lvi.lParam); hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); if (!hSCManager) return FALSE; hService = OpenService(hSCManager, pItemData->m_szServiceName, SERVICE_STOP | SERVICE_PAUSE_CONTINUE); if (!hService) { CloseServiceHandle(hSCManager); return FALSE; }; if (!ControlService(hService, dwControl, &serviceStatus)) { CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return FALSE; }; pItemData->m_dwCurrentState = dwNewCurrentState; lvi.mask = LVIF_TEXT; lvi.iItem = nSelected; lvi.iSubItem = 1; lvi.pszText = (LPTSTR)arrlpStrCurrentState[dwNewCurrentState]; SendMessage(g_hwndList, LVM_SETITEM, 0, (LPARAM)&lvi); CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return TRUE; }; BOOL ChangeTheServiceConfig(DWORD dwStartType) { int nSelected; LVITEM lvi = { 0 }; PITEMDATA pItemData; SC_HANDLE hSCManager = NULL; SC_HANDLE hService; nSelected = SendMessage(g_hwndList, LVM_GETSELECTIONMARK, 0, 0); lvi.mask = LVIF_PARAM; lvi.iItem = nSelected; SendMessage(g_hwndList, LVM_GETITEM, 0, (LPARAM)&lvi); pItemData = (PITEMDATA)(lvi.lParam); hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); if (!hSCManager) return FALSE; hService = OpenService(hSCManager, pItemData->m_szServiceName, SERVICE_CHANGE_CONFIG); if (!hService) { CloseServiceHandle(hSCManager); return FALSE; }; if (!ChangeServiceConfig(hService, SERVICE_NO_CHANGE, dwStartType, SERVICE_NO_CHANGE, NULL, NULL, NULL, NULL, NULL, NULL, NULL)) { CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return FALSE; }; pItemData->m_dwStartType = dwStartType; lvi.mask = LVIF_TEXT; lvi.iItem = nSelected; lvi.iSubItem = 2; lvi.pszText = (LPTSTR)arrlpStrStartType[dwStartType]; SendMessage(g_hwndList, LVM_SETITEM, 0, (LPARAM)&lvi); CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return TRUE; };
INT_PTR CALLBACK DialogProcCreateService(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) { TCHAR szFile[MAX_PATH] = { 0 }; TCHAR szFileTitle[MAX_PATH] = { 0 }; OPENFILENAME ofn = { sizeof(OPENFILENAME) }; static HWND hwndCombo; int nIndex, nCount; BOOL bRet = FALSE; TCHAR szBinaryPathName[MAX_PATH] = { 0 }; TCHAR szServiceName[256] = { 0 }; TCHAR szDisplayName[256] = { 0 }; TCHAR szDescription[1024] = { 0 }; DWORD dwStartType; switch (uMsg) { case WM_INITDIALOG: { hwndCombo = GetDlgItem(hwndDlg, IDC_COMBO_STARTTYPE); nIndex = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)TEXT("自动启动")); SendMessage(hwndCombo, CB_SETITEMDATA, nIndex, 0x00000002); nIndex = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)TEXT("手动启动")); SendMessage(hwndCombo, CB_SETITEMDATA, nIndex, 0x00000003); nIndex = SendMessage(hwndCombo, CB_ADDSTRING, 0, (LPARAM)TEXT("禁用")); SendMessage(hwndCombo, CB_SETITEMDATA, nIndex, 0x00000004); SendMessage(hwndCombo, CB_SETCURSEL, 0, 0); SendMessage(GetDlgItem(hwndDlg, IDC_EDIT_SERVICENAME), EM_SETLIMITTEXT, _countof(szServiceName) - 1, 0); SendMessage(GetDlgItem(hwndDlg, IDC_EDIT_DISPLAYNAME), EM_SETLIMITTEXT, _countof(szDisplayName) - 1, 0); SendMessage(GetDlgItem(hwndDlg, IDC_EDIT_DESCRIPTION), EM_SETLIMITTEXT, _countof(szDescription) - 1, 0); return TRUE; }; case WM_COMMAND: { switch (LOWORD(wParam)) { case IDC_BTN_BROWSE: { ofn.hwndOwner = hwndDlg; ofn.lpstrFilter = TEXT("EXE文件(*.exe)\0*.exe\0所有文件(*.*)\0*.*\0"); ofn.lpstrFile = szFile; ofn.nMaxFile = _countof(szFile); ofn.lpstrFileTitle = szFileTitle; ofn.nMaxFileTitle = _countof(szFileTitle); ofn.lpstrTitle = TEXT("请选择要打开的文件"); ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; if (GetOpenFileName(&ofn)) { *(_tcsrchr(szFileTitle, TEXT('.'))) = TEXT('\0'); SetDlgItemText(hwndDlg, IDC_EDIT_BINARYPATHNAME, szFile); SetDlgItemText(hwndDlg, IDC_EDIT_SERVICENAME, szFileTitle); SetDlgItemText(hwndDlg, IDC_EDIT_DISPLAYNAME, szFileTitle); }; break; }; case IDOK: { nCount = GetDlgItemText(hwndDlg, IDC_EDIT_BINARYPATHNAME, szBinaryPathName, _countof(szBinaryPathName)); if (nCount < 5) { MessageBox(hwndDlg, TEXT("请输入服务的完整路径"), TEXT("错误提示"), MB_OK); return TRUE; }; nCount = GetDlgItemText(hwndDlg, IDC_EDIT_SERVICENAME, szServiceName, _countof(szServiceName)); if (nCount < 1) { MessageBox(hwndDlg, TEXT("请输入服务名称"), TEXT("错误提示"), MB_OK); return TRUE; }; nCount = GetDlgItemText(hwndDlg, IDC_EDIT_DISPLAYNAME, szDisplayName, _countof(szDisplayName)); if (nCount < 1) { MessageBox(hwndDlg, TEXT("请输入显示名称"), TEXT("错误提示"), MB_OK); return TRUE; }; GetDlgItemText(hwndDlg, IDC_EDIT_DESCRIPTION, szDescription, _countof(szDescription)); nIndex = SendMessage(hwndCombo, CB_GETCURSEL, 0, 0); dwStartType = SendMessage(hwndCombo, CB_GETITEMDATA, nIndex, 0); bRet = CreateAService(szBinaryPathName, szServiceName, szDisplayName, dwStartType, SERVICE_WIN32_OWN_PROCESS, szDescription); if (bRet) EndDialog(hwndDlg, 2); else EndDialog(hwndDlg, 1); break; }; case IDCANCEL: { EndDialog(hwndDlg, 0); break; }; }; return TRUE; }; }; return FALSE; }; BOOL CreateAService(LPCTSTR lpBinaryPathName, LPCTSTR lpServiceName, LPCTSTR lpDisplayName, DWORD dwStartType, DWORD dwServiceType, LPCTSTR lpDescription, DWORD dwDesiredAccess, DWORD dwErrorControl) { SC_HANDLE hSCManager = NULL; SC_HANDLE hService; SERVICE_DESCRIPTION serviceDescription = { 0 }; hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); if (!hSCManager) return FALSE; hService = CreateService(hSCManager, lpServiceName, lpDisplayName, dwDesiredAccess, dwServiceType, dwStartType, dwErrorControl, lpBinaryPathName, NULL, NULL, NULL, NULL, NULL); if (!hService) { CloseServiceHandle(hSCManager); return FALSE; }; if (!lpDescription) { serviceDescription.lpDescription = (LPTSTR)lpDescription; ChangeServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, &serviceDescription); }; g_bAscendingDisplayName = FALSE; GetServiceList(); LVFINDINFO lvfi = { 0 }; lvfi.flags = LVFI_STRING; lvfi.psz = lpDisplayName; int nIndex = SendMessage(g_hwndList, LVM_FINDITEM, -1, (LPARAM)&lvfi); LVITEM lvi = { 0 }; lvi.state = lvi.stateMask = LVIS_SELECTED; SendMessage(g_hwndList, LVM_SETITEMSTATE, nIndex, (LPARAM)&lvi); int nCount = SendMessage(g_hwndList, LVM_GETCOUNTPERPAGE, 0, 0); if (nIndex > nCount) { DWORD dw = SendMessage(g_hwndList, LVM_APPROXIMATEVIEWRECT, nIndex - 2, MAKELPARAM(-1, -1)); SendMessage(g_hwndList, LVM_SCROLL, 0, HIWORD(dw)); }; CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return TRUE; }; BOOL DeleteTheService() { int nSelected; LVITEM lvi = { 0 }; PITEMDATA pItemData; SC_HANDLE hSCManager = NULL; SC_HANDLE hService; nSelected = SendMessage(g_hwndList, LVM_GETSELECTIONMARK, 0, 0); lvi.mask = LVIF_PARAM; lvi.iItem = nSelected; SendMessage(g_hwndList, LVM_GETITEM, 0, (LPARAM)&lvi); pItemData = (PITEMDATA)(lvi.lParam); hSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT); if (!hSCManager) return FALSE; hService = OpenService(hSCManager, pItemData->m_szServiceName, DELETE); if (!hService) { CloseServiceHandle(hSCManager); return FALSE; }; if (!DeleteService(hService)) return FALSE; else { delete[]pItemData; SendMessage(g_hwndList, LVM_DELETEITEM, nSelected, 0); }; CloseServiceHandle(hService); CloseServiceHandle(hSCManager); return TRUE; };
|