ServicePage.cpp
上传用户:shouhua
上传日期:2014-12-06
资源大小:5685k
文件大小:17k
- // ServicePage.cpp : implementation file
- //
- #include "stdafx.h"
- #include "AntiEvilTools.h"
- #include "ServicePage.h"
- #include "ModulVer.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- char **pattern;
- int pattern_count;
- int nohideservice;
- int ischange;
- CModuleVersion *GoTo;
- MyList *Gogogo;
- DWORD CreateServer_StartType = SERVICE_AUTO_START;
- //CHiddenReg ChkReg;
- /////////////////////////////////////////////////////////////////////////////
- // CServicePage property page
- IMPLEMENT_DYNCREATE(CServicePage, CPropertyPage)
- CServicePage::CServicePage() : CPropertyPage(CServicePage::IDD)
- {
- //{{AFX_DATA_INIT(CServicePage)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- }
- CServicePage::~CServicePage()
- {
- }
- void CServicePage::DoDataExchange(CDataExchange* pDX)
- {
- CPropertyPage::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CServicePage)
- DDX_Control(pDX, IDC_LIST_SERVICE, m_list);
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CServicePage, CPropertyPage)
- //{{AFX_MSG_MAP(CServicePage)
- ON_NOTIFY(NM_RCLICK, IDC_LIST_SERVICE, OnRclickProcessList)
- ON_COMMAND(ID_MENU_REF, OnMenuRef)
- ON_COMMAND(ID_SERVICE_START, OnServiceStart)
- ON_COMMAND(ID_GooleService, OnGooleService)
- ON_COMMAND(ID_SERVICE_DELETE, OnServiceDelete)
- ON_COMMAND(ID_SERVICE_STOP, OnServiceStop)
- ON_COMMAND(ID_SERVICE_AUTO, OnServiceAuto)
- ON_COMMAND(ID_SERVICE_MANAGER, OnServiceManager)
- ON_COMMAND(ID_SERVICE_DISABLE, OnServiceDisable)
- ON_COMMAND(ID_ServiceProperties, OnServiceProperties)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CServicePage message handlers
- BOOL CServicePage::OnInitDialog()
- {
- CDialog::OnInitDialog();
- CWinApp *pApp = AfxGetApp();
- CHeaderCtrl* pHeader = NULL;
- pHeader=m_list.GetHeaderCtrl();
- if(pHeader==NULL)
- return -1;
- //VERIFY(m_list.m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd));
- m_pImage = new CImageList();
- ASSERT(m_pImage != NULL); // serious allocation failure checking
- m_list.SetImageList(m_pImage, LVSIL_SMALL);
- m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
- Gogogo->InitSystemImageLists(m_list.m_hWnd);
- m_list.InsertColumn(0,"服务名称",LVCFMT_LEFT,100,-1);
- m_list.InsertColumn(1,"详细名称",LVCFMT_LEFT,200,-1);
- m_list.InsertColumn(2,"服务状态",LVCFMT_LEFT,60,-1);
- m_list.InsertColumn(3,"启动类型",LVCFMT_LEFT,60,-1);
- m_list.InsertColumn(4,"程序路径",LVCFMT_LEFT,240,-1);
- m_list.InsertColumn(5,"详细描述",LVCFMT_LEFT,300,-1);
- OnView();
- return TRUE;
- }
- void CServicePage::OnView()
- {
- m_list.DeleteAllItems();
- UpdateData(false);
- LPQUERY_SERVICE_CONFIG ServicesInfo = NULL;
- LPENUM_SERVICE_STATUS lpServices = NULL;
- DWORD nSize = 0;
- DWORD n;
- DWORD nResumeHandle = 0;
- DWORD dwServiceType = SERVICE_WIN32;
- SC_HANDLE schSCManager = NULL;
- BOOL Flag = FALSE;
- char szTemp[MAX_PATH];
- DWORD dwType=REG_SZ;
- char szFilePath[MAX_PATH];
- DWORD dwBufLen=MAX_PATH;
- if((schSCManager=OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS))==NULL)
- {
- return;
- }
- lpServices = (LPENUM_SERVICE_STATUS) LocalAlloc(LPTR, 64 * 1024); // Allocate Ram
- EnumServicesStatus(schSCManager,
- dwServiceType,
- SERVICE_STATE_ALL,
- (LPENUM_SERVICE_STATUS)lpServices,
- 64 * 1024,
- &nSize,
- &n,
- &nResumeHandle);
- int nitem = 0;
- for (unsigned long i = 0; i < n; i++)
- {
- SC_HANDLE service = NULL;
- DWORD nResumeHandle = 0;
- service=OpenService(schSCManager,lpServices[i].lpServiceName, SERVICE_ALL_ACCESS);
- ServicesInfo = (LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 64 * 1024); // Allocate Ram
- QueryServiceConfig(service, ServicesInfo, 64 * 1024, &nResumeHandle);
- nitem=m_list.InsertItem(i, lpServices[i].lpServiceName);
- m_list.SetItemText(i, 1, lpServices[i].lpDisplayName);
- if ( lpServices[i].ServiceStatus.dwCurrentState == SERVICE_RUNNING)//SERVICE_STOPPED)
- {
- m_list.SetItemText(i, 2, "已启动");
- }
- else
- {
- m_list.SetItemText(i,2,"已停止");
- }
- CString stemp;
- LPQUERY_SERVICE_CONFIG lpServerConfig;
- LPSERVICE_DESCRIPTION lpServerDesc;
- lpServerConfig=(LPQUERY_SERVICE_CONFIG) LocalAlloc(LPTR, 4096);
- if(QueryServiceConfig(service,lpServerConfig,4096,&nResumeHandle))
- {
- switch(lpServerConfig->dwStartType)
- {
- case SERVICE_AUTO_START:
- stemp="自动";
- break;
- case SERVICE_BOOT_START:
- stemp="Boot";
- break;
- case SERVICE_DEMAND_START:
- stemp="手动";
- break;
- case SERVICE_DISABLED:
- stemp="禁用";
- break;
- case SERVICE_SYSTEM_START:
- stemp="系统";
- break;
- }
- m_list.SetItemText(i,3,stemp);
- stemp.Format("%s",lpServerConfig->lpBinaryPathName);
- memset(szTemp, 0, MAX_PATH);
- GoTo->ParseString(stemp, szTemp);
- CString strTemp;
- strTemp.Format("%s", szTemp);
- strTemp.MakeLower();
- if(strTemp.Find("svchost"))
- {
- HKEY hKEY;
- CString subKey="SYSTEM\CurrentControlSet\Services\";
- subKey += lpServices[i].lpServiceName;
- subKey += "\Parameters\";
- long lRet = ::RegOpenKeyEx(HKEY_LOCAL_MACHINE, subKey, 0, KEY_READ, &hKEY);
- if( lRet != ERROR_SUCCESS )
- goto GoTo1;
- lRet = ::RegQueryValueEx(hKEY, "ServiceDll", NULL, &dwType, (LPBYTE)szFilePath, &dwBufLen);
- if( (lRet != ERROR_SUCCESS) || (dwBufLen > MAX_PATH) )
- {
- ::RegCloseKey(hKEY);
- goto GoTo1;
- }
- memset(szTemp, 0, MAX_PATH);
- GoTo->ParseString(szFilePath, szTemp);
- GoTo1:
- strTemp.Format("%s", szTemp);
- memset(szTemp, 0, MAX_PATH);
- ::ExpandEnvironmentStrings(strTemp, szTemp, MAX_PATH);
- m_list.SetItemText(i,4,szTemp);
- }
- //MessageBox(szTemp);
- m_list.SetItem(i, 0, LVIF_TEXT | LVIF_IMAGE,lpServices[i].lpServiceName ,
- Gogogo->GetFileIcon(szTemp), 0, 0, 0);
- }
- LocalFree(lpServerConfig);
- lpServerDesc=(LPSERVICE_DESCRIPTION ) LocalAlloc( LPTR, 4096);
- if (QueryServiceConfig2(service, SERVICE_CONFIG_DESCRIPTION, (LPBYTE)lpServerDesc,
- 4096, &nResumeHandle) )
- {
- stemp.Format("%s",lpServerDesc->lpDescription);
- m_list.SetItemText(i,5,stemp);
- }
- LocalFree(lpServerDesc);
- UpdateData(false);
- }
- }
- void CServicePage::OnRclickProcessList(NMHDR* pNMHDR, LRESULT* pResult)
- {
- iSelected = m_list.GetNextItem(-1, LVNI_SELECTED);
- POINT point;
- ::GetCursorPos(&point);
- CMenu menu;
- menu.LoadMenu(IDR_MENU_SERVICE);
- menu.GetSubMenu(0)->TrackPopupMenu(TPM_LEFTALIGN, point.x, point.y, this);
- *pResult = 0;
- }
- void CServicePage::OnMenuRef()
- {
- // TODO: Add your command handler code here
- OnView();
- }
- void CServicePage::OnServiceStart()
- {
- // TODO: Add your command handler code here
- EnablePriv();
- char ServerName[100]=" ";
- int x=m_list.GetSelectionMark();
- m_list.GetItemText(x,0,ServerName,100);
- SC_HANDLE scm;
- SC_HANDLE service;
- SERVICE_STATUS status;
- if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
- {
- //GoTo->PrintLog("打开服务管理器错误n");
- return;
- }
- service=OpenService(scm,ServerName,SERVICE_ALL_ACCESS);
- if(!service)
- {
- //GoTo->PrintLog("打开服务错误!n");
- CloseServiceHandle(scm);
- return;
- }
- BOOL isSuccess=QueryServiceStatus(service,&status);
- if (!isSuccess)
- {
- //GoTo->PrintLog("查询服务错误!n");
- CloseServiceHandle(service );
- CloseServiceHandle(scm);
- return;
- }
- if ( status.dwCurrentState==SERVICE_STOPPED )
- {
- isSuccess=::StartService(service,NULL,NULL);
- if (!isSuccess)
- {
- MessageBox("启动服务失败!", "错误", MB_OK|MB_ICONERROR);
- }
- else
- {
- m_list.SetItemText(x, 2, "已启动");
- MessageBox("启动服务成功!", "提示", MB_OK|MB_ICONINFORMATION);
- }
- }
- else
- {
- MessageBox("服务已经启动!", "提示", MB_OK|MB_ICONINFORMATION);
- }
- CloseServiceHandle(service );
- CloseServiceHandle(scm);
- }
- BOOL CServicePage::EnablePriv()
- {
- HANDLE hToken;
- if ( OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken) )
- {
- TOKEN_PRIVILEGES tkp;
-
- LookupPrivilegeValue( NULL,SE_DEBUG_NAME,&tkp.Privileges[0].Luid );//修改进程权限
- tkp.PrivilegeCount=1;
- tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
- AdjustTokenPrivileges( hToken,FALSE,&tkp,sizeof tkp,NULL,NULL );//通知系统修改进程权限
-
- }
- return( (GetLastError()==ERROR_SUCCESS) );
- }
- void CServicePage::OnGooleService()
- {
- // TODO: Add your command handler code here
- char GoogleName[100]=" ";
- int name=m_list.GetSelectionMark();
- m_list.GetItemText(name,0,GoogleName,100);
- CString FullName = "http://www.google.cn/search?q="+CString(GoogleName)+"服务";
- ShellExecute(m_hWnd, NULL, FullName, NULL, NULL, SW_HIDE);
- }
- void CServicePage::OnServiceDelete()
- {
- // TODO: Add your command handler code here
- EnablePriv();
- char ServerName[100]=" ";
- int x=m_list.GetSelectionMark();
- m_list.GetItemText(x,0,ServerName,100);
- SC_HANDLE scm;
- SC_HANDLE service;
- SERVICE_STATUS status;
- if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
- {
- return;
- }
- service=OpenService(scm,ServerName,SERVICE_ALL_ACCESS|DELETE);
-
- if(!service)
- {
- CloseServiceHandle(scm);
- return;
- }
- BOOL isSuccess=QueryServiceStatus(service,&status);
- if (!isSuccess)
- {
- CloseServiceHandle(service );
- CloseServiceHandle(scm);
- return;
- }
- if ( status.dwCurrentState!=SERVICE_STOPPED )
- {
- isSuccess=ControlService(service,SERVICE_CONTROL_STOP,&status);
- if (!isSuccess )
- {
- CloseServiceHandle(service );
- CloseServiceHandle(scm);
- return;
- }
- }
- isSuccess=::DeleteService(service);
- if (!isSuccess)
- {
- MessageBox("删除服务失败!", "错误", MB_OK|MB_ICONERROR);
- }
- else
- {
-
- HKEY hKey;
- CString regpath = "SYSTEM\CurrentControlSet\Services\";
- regpath+= ServerName;
- if(ERROR_SUCCESS == RegOpenKeyEx(HKEY_LOCAL_MACHINE, regpath, 0, KEY_ALL_ACCESS, &hKey) )
- {
- if(ERROR_SUCCESS != RegDeleteKey(hKey, regpath) )
- {
- RegCloseKey(hKey); //随便删除下注册表里残留的东东
- // return;
- }
- }
- else
- {
- return ;
- }
- m_list.DeleteItem(x);
- MessageBox("删除服务成功!", "提示", MB_OK|MB_ICONINFORMATION);
- }
- CloseServiceHandle(service );
- CloseServiceHandle(scm);
- }
- void CServicePage::OnServiceStop()
- {
- // TODO: Add your command handler code here
- EnablePriv();
- char ServerName[100]=" ";
- int x=m_list.GetSelectionMark();
- m_list.GetItemText(x,0,ServerName,100);
- SC_HANDLE scm;
- SC_HANDLE service;
- SERVICE_STATUS status;
- if((scm=OpenSCManager(NULL,NULL,SC_MANAGER_CREATE_SERVICE))==NULL)
- {
- return;
- }
- service=OpenService(scm,ServerName,SERVICE_ALL_ACCESS);
-
- if(!service)
- {
- CloseServiceHandle(scm);
- return;
- }
- BOOL isSuccess=QueryServiceStatus(service,&status);
- if (!isSuccess)
- {
- CloseServiceHandle(service);
- CloseServiceHandle(scm);
- return;
- }
- if ( status.dwCurrentState!=SERVICE_STOPPED )
- {
- isSuccess=ControlService(service,SERVICE_CONTROL_STOP,&status);
- if (!isSuccess )
- {
- MessageBox("停止服务失败!", "错误", MB_OK|MB_ICONERROR);
- }
- else
- {
- m_list.SetItemText(x, 2, "已停止");
- MessageBox("停止服务成功!", "提示", MB_OK|MB_ICONINFORMATION);
- }
- }
- CloseServiceHandle(service);
- CloseServiceHandle(scm);
- }
- void CServicePage::OnServiceAuto()
- {
- // TODO: Add your command handler code here
- EnablePriv();
- char ServerName[100]=" ";
- int x=m_list.GetSelectionMark();
- m_list.GetItemText(x,0,ServerName,100);
- SC_HANDLE scm;
- SC_HANDLE service;
- if((scm=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS))==NULL)
- {
- return;
- }
- service=OpenService(scm,ServerName,SERVICE_ALL_ACCESS);
- if(!service)
- {
- CloseServiceHandle(scm);
- return;
- }
- SC_LOCK sclLock;
- DWORD dwStartType;
- sclLock = LockServiceDatabase(scm);
- if (sclLock == NULL)
- {
- DWORD dwErr = GetLastError();
- if (dwErr == ERROR_SERVICE_DATABASE_LOCKED)
- {
- CloseServiceHandle(scm);
- return;
- }
- if(dwErr == ERROR_INVALID_HANDLE)
- {
- CloseServiceHandle(scm);
- return;
- }
- if(dwErr == ERROR_ACCESS_DENIED)
- {
- CloseServiceHandle(scm);
- return;
- }
- else
- {
- return;
- }
- }
- dwStartType = SERVICE_AUTO_START;
- if (! ChangeServiceConfig(
- service, // handle of service
- SERVICE_NO_CHANGE, // service type: no change
- dwStartType, // change service start type
- 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
- {
- }
- else
- {
- m_list.SetItemText(x, 3, "自动");
- }
- UnlockServiceDatabase(sclLock);
- CloseServiceHandle(service);
- CloseServiceHandle(scm);
- }
- void CServicePage::OnServiceManager()
- {
- // TODO: Add your command handler code here
- EnablePriv();
- char ServerName[MAX_PATH]=" ";
- int x=m_list.GetSelectionMark();
- m_list.GetItemText(x,0,ServerName,MAX_PATH);
- SC_HANDLE scm;
- SC_HANDLE service;
- if((scm=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS))==NULL)
- {
- return;
- }
- service=OpenService(scm, ServerName, SERVICE_ALL_ACCESS);
- if(!service)
- {
- if(ERROR_SERVICE_DOES_NOT_EXIST == GetLastError())
- {
- MessageBox("指定服务不存在!n", "错误", MB_OK|MB_ICONERROR);
- CloseServiceHandle(scm);
- return;
- }
- }
- SC_LOCK sclLock;
- DWORD dwStartType;
- sclLock = LockServiceDatabase(scm);
- if (sclLock == NULL)
- {
- DWORD dwErr = GetLastError();
- if (dwErr == ERROR_SERVICE_DATABASE_LOCKED)
- {
- CloseServiceHandle(scm);
- return;
- }
- if(dwErr == ERROR_INVALID_HANDLE)
- {
- CloseServiceHandle(scm);
- return;
- }
- if(dwErr == ERROR_ACCESS_DENIED)
- {
- CloseServiceHandle(scm);
- return;
- }
- else
- {
- return;
- }
-
- }
- dwStartType = SERVICE_DEMAND_START;
- if (! ChangeServiceConfig(
- service, // handle of service
- SERVICE_NO_CHANGE, // service type: no change
- dwStartType, // change service start type
- 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
- {
- }
- else
- {
- m_list.SetItemText(x, 3, "手动");
- }
- UnlockServiceDatabase(sclLock);
- CloseServiceHandle(service);
- CloseServiceHandle(scm);
- }
- void CServicePage::OnServiceDisable()
- {
- // TODO: Add your command handler code here
- EnablePriv();
- char ServerName[100]=" ";
- int x=m_list.GetSelectionMark();
- m_list.GetItemText(x,0,ServerName,100);
- SC_HANDLE scm;
- SC_HANDLE service;
- if((scm=OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS))==NULL)
- {
- return;
- }
- service=OpenService(scm,ServerName,SERVICE_ALL_ACCESS);
- if(!service)
- {
- CloseServiceHandle(scm);
- return;
- }
- SC_LOCK sclLock;
- DWORD dwStartType;
- sclLock = LockServiceDatabase(scm);
- if (sclLock == NULL)
- {
- DWORD dwErr = GetLastError();
- if (dwErr == ERROR_SERVICE_DATABASE_LOCKED)
- {
- CloseServiceHandle(scm);
- return;
- }
- if(dwErr == ERROR_INVALID_HANDLE)
- {
- CloseServiceHandle(scm);
- return;
- }
- if(dwErr == ERROR_ACCESS_DENIED)
- {
- CloseServiceHandle(scm);
- return;
- }
- else
- {
- return;
- }
- }
- dwStartType = SERVICE_DISABLED;
- if (! ChangeServiceConfig(
- service, // handle of service
- SERVICE_NO_CHANGE, // service type: no change
- dwStartType, // change service start type
- 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
- {
- MessageBox("修改配置错误!n", "错误", MB_OK|MB_ICONERROR);
- }
- else
- {
- m_list.SetItemText(x, 3, "禁用");
- }
- UnlockServiceDatabase(sclLock);
- CloseServiceHandle(service);
- CloseServiceHandle(scm);
- }
- void CServicePage::OnServiceProperties()
- {
- // TODO: Add your command handler code here
- EnablePriv();
- char AppPath[100]=" ";
- char Path[MAX_PATH];
- int x=m_list.GetSelectionMark();
- m_list.GetItemText(x,4,AppPath,100);
- SHELLEXECUTEINFO sei;
- memset(Path, 0, MAX_PATH);
- GoTo->ParseString(AppPath, Path);
- ZeroMemory(&sei,sizeof(sei));
- sei.cbSize = sizeof(sei);
- sei.lpFile = Path;
- sei.lpVerb = _T("properties");
- sei.fMask = SEE_MASK_INVOKEIDLIST;
- sei.nShow = SW_SHOWNORMAL;
- ShellExecuteEx(&sei);
- }