// ---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Main.h"
// ---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
String GetLastError1(DWORD errcode)
{
char *lpMsgBuf;
FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcode, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR) & lpMsgBuf, 0, NULL);
String ret = lpMsgBuf;
LocalFree(lpMsgBuf);
return ret;
}
// ---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
hSCM = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
CloseServiceHandle(hSCM);
}
// ---------------------------------------------------------------------------
bool TForm1::StartServiceEx(char *service_name)
{
SC_HANDLE hService = OpenService(hSCM, service_name, SERVICE_ALL_ACCESS);
SERVICE_STATUS status;
if (!QueryServiceStatus(hService, &status))
{
DWORD errcode = GetLastError();
CloseServiceHandle(hService);
throw Exception(GetLastError1(errcode));
}
if (status.dwCurrentState == SERVICE_STOPPED)
{
if (!StartService(hService, NULL, NULL))
{
DWORD errcode = GetLastError();
CloseServiceHandle(hService);
throw Exception(GetLastError1(errcode));
}
}
CloseServiceHandle(hService);
return true;
}
bool TForm1::StopServiceEx(char *service_name)
{
SC_HANDLE hService = OpenService(hSCM, service_name, SERVICE_ALL_ACCESS);
SERVICE_STATUS status;
if (!QueryServiceStatus(hService, &status))
{
DWORD errcode = GetLastError();
CloseServiceHandle(hService);
throw Exception(GetLastError1(errcode));
}
if (status.dwCurrentState != SERVICE_STOPPED)
{
if (!ControlService(hService, SERVICE_CONTROL_STOP, &status))
{
DWORD errcode = GetLastError();
CloseServiceHandle(hService);
throw Exception(GetLastError1(errcode));
}
}
CloseServiceHandle(hService);
return true;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::actRefreshExecute(TObject *Sender)
{
actRefresh->Enabled = false;
lv1->Items->BeginUpdate();
lv1->Clear();
LPENUM_SERVICE_STATUS lpServices = NULL;
DWORD nSize = 0;
DWORD n;
DWORD nResumeHandle = 0;
lpServices = (LPENUM_SERVICE_STATUS)LocalAlloc(LPTR, 64 * 1024); // 注意分配足够的空间
EnumServicesStatus(hSCM, SERVICE_WIN32, SERVICE_STATE_ALL, (LPENUM_SERVICE_STATUS)lpServices, 64 * 1024, &nSize, &n, &nResumeHandle);
LPQUERY_SERVICE_CONFIG ServicesInfo = NULL;
for (unsigned int i = 0; i < n; i++)
{
DWORD nResumeHandle = 0;
SC_HANDLE hService = OpenService(hSCM, lpServices[i].lpServiceName, SERVICE_ALL_ACCESS);
ServicesInfo = (LPQUERY_SERVICE_CONFIG)LocalAlloc(LPTR, 64 * 1024); // 注意分配足够的空间
QueryServiceConfig(hService, ServicesInfo, 8 * 1024, &nResumeHandle); // 枚举各个服务信息
String start_type;
if (2 == ServicesInfo->dwStartType)
start_type = "自动";
if (3 == ServicesInfo->dwStartType)
start_type = "手动";
if (4 == ServicesInfo->dwStartType)
start_type = "禁用";
String state;
if (lpServices[i].ServiceStatus.dwCurrentState != SERVICE_STOPPED)
state = "已启动";
TListItem *item;
item = lv1->Items->Add();
item->Caption = i + 1;
item->SubItems->Add(lpServices[i].lpServiceName);
item->SubItems->Add(ServicesInfo->lpDisplayName);
item->SubItems->Add(state);
item->SubItems->Add(start_type);
// 获取服务描述
LPSERVICE_DESCRIPTION lpqscBuf2;
DWORD dwNeeded = 0;
if (QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, NULL, 0, &dwNeeded) == false && GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
lpqscBuf2 = (LPSERVICE_DESCRIPTION)LocalAlloc(LPTR, dwNeeded);
if (QueryServiceConfig2(hService, SERVICE_CONFIG_DESCRIPTION, (BYTE*)lpqscBuf2, dwNeeded, &dwNeeded))
{
item->SubItems->Add(lpqscBuf2->lpDescription);
}
LocalFree(lpqscBuf2);
}
item->SubItems->Add(ServicesInfo->lpBinaryPathName);
LocalFree(ServicesInfo);
::CloseServiceHandle(hService);
}
LocalFree(lpServices);
lv1->Items->EndUpdate();
actRefresh->Enabled = true;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::actStartExecute(TObject *Sender)
{
try
{
StartServiceEx(AnsiString(lv1->ItemFocused->SubItems->Strings[0]).c_str());
}
catch(Exception & e)
{
Application->MessageBox(e.Message.c_str(), L"操作失败", MB_OK + MB_ICONWARNING);
return;
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::actStopExecute(TObject *Sender)
{
try
{
StopServiceEx(AnsiString(lv1->ItemFocused->SubItems->Strings[0]).c_str());
}
catch(Exception & e)
{
Application->MessageBox(e.Message.c_str(), L"操作失败", MB_OK + MB_ICONWARNING);
return;
}
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::actForbiddenExecute(TObject *Sender)
{
SC_LOCK sclLock;
sclLock = LockServiceDatabase(hSCM);
if (sclLock == NULL)
{
if (GetLastError() != ERROR_SERVICE_DATABASE_LOCKED)
{
Application->MessageBox(L"锁定服务数据库失败!", L"提示", MB_OK + MB_ICONWARNING);
}
}
AnsiString service_name = AnsiString(lv1->ItemFocused->SubItems->Strings[0]);
SC_HANDLE hService = OpenService(hSCM, service_name.c_str(), SERVICE_ALL_ACCESS);
if (!ChangeServiceConfig(hService, // handle of service
SERVICE_NO_CHANGE, // service type: no change
SERVICE_DISABLED, // 这里做了更改
SERVICE_NO_CHANGE, // error control: no change
NULL, // binary path: no change
NULL, // load order group: no change
NULL, // tag ID: no change
NULL, // dependencies: no change
NULL, // account name: no change
NULL, // password: no change
NULL)) // displayname
{
Application->MessageBox(L"设置启动类型失败!", L"提示", MB_OK + MB_ICONWARNING);
}
UnlockServiceDatabase(sclLock);
CloseServiceHandle(hService);
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::actManulExecute(TObject *Sender)
{
SC_LOCK sclLock;
sclLock = LockServiceDatabase(hSCM);
if (sclLock == NULL)
{
if (GetLastError() != ERROR_SERVICE_DATABASE_LOCKED)
{
Application->MessageBox(L"锁定服务数据库失败!", L"提示", MB_OK + MB_ICONWARNING);
}
}
AnsiString service_name = AnsiString(lv1->ItemFocused->SubItems->Strings[0]);
SC_HANDLE hService = OpenService(hSCM, service_name.c_str(), SERVICE_ALL_ACCESS);
if (!ChangeServiceConfig(hService, // handle of service
SERVICE_NO_CHANGE, // service type: no change
SERVICE_DEMAND_START, // 这里做了更改
SERVICE_NO_CHANGE, // error control: no change
NULL, // binary path: no change
NULL, // load order group: no change
NULL, // tag ID: no change
NULL, // dependencies: no change
NULL, // account name: no change
NULL, // password: no change
NULL)) // displayname
{
Application->MessageBox(L"设置启动类型失败!", L"提示", MB_OK + MB_ICONWARNING);
}
UnlockServiceDatabase(sclLock);
CloseServiceHandle(hService);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::actAutoExecute(TObject *Sender)
{
SC_LOCK sclLock;
sclLock = LockServiceDatabase(hSCM);
if (sclLock == NULL)
{
if (GetLastError() != ERROR_SERVICE_DATABASE_LOCKED)
{
Application->MessageBox(L"锁定服务数据库失败!", L"提示", MB_OK + MB_ICONWARNING);
}
}
AnsiString service_name = AnsiString(lv1->ItemFo
- 1
- 2
前往页