libjmmcg  release_579_6_g8cffd
A C++ library containing an eclectic mix of useful, advanced components.
ServiceManipulation.cpp
Go to the documentation of this file.
1 /******************************************************************************
2 ** Copyright � 2002 by J.M.McGuiness, coder@hussar.me.uk
3 **
4 ** This library is free software; you can redistribute it and/or
5 ** modify it under the terms of the GNU Lesser General Public
6 ** License as published by the Free Software Foundation; either
7 ** version 2.1 of the License, or (at your option) any later version.
8 **
9 ** This library is distributed in the hope that it will be useful,
10 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ** Lesser General Public License for more details.
13 **
14 ** You should have received a copy of the GNU Lesser General Public
15 ** License along with this library; if not, write to the Free Software
16 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 
19 #include "stdafx.h"
20 
22 
23 #include "../../../core/info.hpp"
24 #include "../../../core/exception.hpp"
25 #include "../../../core/trace.hpp"
26 
27 /////////////////////////////////////////////////////////////////////////////
28 
29 using namespace libjmmcg;
30 using namespace NTUtils;
31 
32 /////////////////////////////////////////////////////////////////////////////
33 
35 
36 /////////////////////////////////////////////////////////////////////////////
37 
38 const TCHAR CloseServiceHandle_name[]=_T("CloseServiceHandle");
39 #ifdef UNICODE
40  const TCHAR OpenSCManager_name[]=_T("OpenSCManagerW");
41  const TCHAR OpenService_name[]=_T("OpenServiceW");
42  const TCHAR StartService_name[]=_T("StartServiceW");
43 #else
44  const TCHAR OpenSCManager_name[]=_T("OpenSCManagerA");
45  const TCHAR OpenService_name[]=_T("OpenServiceA");
46  const TCHAR StartService_name[]=_T("StartServiceA");
47 #endif // !UNICODE
48 const TCHAR ControlService_name[]=_T("ControlService");
49 
50 /////////////////////////////////////////////////////////////////////////////
51 
52 typedef WINADVAPI SC_HANDLE (WINAPI * const OpenSCManagerType)(LPCTSTR lpMachineName,LPCTSTR lpDatabaseName,DWORD dwDesiredAccess);
53 
54 /////////////////////////////////////////////////////////////////////////////
55 
56 ServiceManipulation::ServiceManipulation(const TCHAR * const machine) :
57  LoadLibraryWrapper(scm_lib_name),
58  pCloseServiceHandle(reinterpret_cast<CloseServiceHandleType>(::GetProcAddress(Handle(),CloseServiceHandle_name))),
59  pOpenService(reinterpret_cast<OpenServiceType>(::GetProcAddress(Handle(),OpenService_name))),
60  pControlService(reinterpret_cast<ControlServiceType>(::GetProcAddress(Handle(),ControlService_name))),
61  pStartService(reinterpret_cast<StartServiceType>(::GetProcAddress(Handle(),StartService_name))) {
62  assert(pCloseServiceHandle);
63  assert(pOpenService);
64  assert(pControlService);
65  assert(pStartService);
66  const OpenSCManagerType pOpenSCManager=reinterpret_cast<OpenSCManagerType>(::GetProcAddress(Handle(),OpenSCManager_name));
67  assert(pOpenSCManager);
68  scm=(*pOpenSCManager)(machine,NULL,SC_MANAGER_ALL_ACCESS);
69  if (!scm) {
70  throw exception_type(_T("Failed to open the Service Control Manager on the specified machine."),info::function(__LINE__,__PRETTY_FUNCTION__,typeid(*this),info::function::argument(_T("const TCHAR * const"),machine)),__REV_INFO__);
71  }
72  curr_service_name=NULL;
73 }
74 
75 ServiceManipulation::~ServiceManipulation(void) {
76  if (curr_service_name) {
77  (*pCloseServiceHandle)(curr_service);
78  JMMCG_TRACE(_T("ServiceManipulation::~ServiceManipulation(): Closing service: 0x")<<std::hex<<curr_service<<_T(", windows error code: ")<<NTUtils::win_exception::StrFromWinErr(::GetLastError()));
79  }
80  (*pCloseServiceHandle)(scm);
81  JMMCG_TRACE(_T("ServiceManipulation::~ServiceManipulation(): Closing service control manager: 0x")<<std::hex<<scm<<_T(", windows error code: ")<<NTUtils::win_exception::StrFromWinErr(::GetLastError()));
82 }
83 
84 void
85 ServiceManipulation::OpenService(const TCHAR * const service_name, const DWORD access) {
86  if (curr_service_name && _tcscmp(curr_service_name,service_name)) {
87  (*pCloseServiceHandle)(curr_service);
88  JMMCG_TRACE(_T("ServiceManipulation::OpenService(...): Closing service: 0x")<<std::hex<<curr_service<<_T(", windows error code: ")<<NTUtils::win_exception::StrFromWinErr(::GetLastError()));
89  curr_service_name=NULL;
90  }
91  if (!curr_service_name) {
92  curr_service=(*pOpenService)(scm,curr_service_name=service_name,access);
93  if (!curr_service) {
94  info::function info(__LINE__,__PRETTY_FUNCTION__,typeid(*this),info::function::argument(_T("const TCHAR * const"),service_name));
95  info.add_arg(_T("const DWORD"),access);
96  throw exception_type(_T("Failed to open the specified service with the specified access mask."),info,__REV_INFO__);
97  }
98  }
99  assert(curr_service_name && curr_service);
100 }