📄 srvman32.cpp
字号:
/*____________________________________________________________________________*\ *
Copyright (c) 1997-2003 John Roy, Holger Zimmermann. All rights reserved.
These sources, libraries and applications are
FREE FOR COMMERCIAL AND NON-COMMERCIAL USE
as long as the following conditions are adhered to.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHORS OR ITS CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
OF THE POSSIBILITY OF SUCH DAMAGE.
*____________________________________________________________________________*|
*
* $Source: /cvsroot/pi3web/Pi3Web_200/Source/Win32App/SrvMan32.cpp,v $
* $Date: 2004/05/29 16:46:53 $
*
Description:
The execute function in this file sort of grew out of portortion
and needs to be split up into functions, its pretty unwieldy.
'CheckService' returns that the service is running for
paused services. This is mainly to stop an attempt at starting a
paused service. Paused services are not used or handled yet.
\*____________________________________________________________________________*/
//$SourceTop:$
#include <assert.h>
#include <windows.h>
#include "Pi2API.h"
#include "PIString.h"
#include "PIStrStr.h"
#include "DeQuote.h"
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
#define KEY_CONF_STARTTYPE "StartType"
#define KEY_CONF_CONFIGPATH "ConfigPath"
#define VALUE_EXISTSCOMMAND "ServiceExists"
#define VALUE_INSTALLCOMMAND "InstallService"
#define VALUE_DELETECOMMAND "DeleteService"
#define VALUE_STARTCOMMAND "StartService"
#define VALUE_STOPCOMMAND "StopService"
#define VALUE_CHECKCOMMAND "CheckService"
#define CONFIG_ERR(this, msg) \
{ PILog_addMessage( PIObject_getDB(this), \
PIObject_getConfigurationDB(this), PILOG_ERROR, (msg) ); }
// #define D { cerr << "File: " << __FILE__ << " Line: " << __LINE__ << endl; }
#define D
/*____________________________________________________________________________*\
*
Class:
Description:
\*____________________________________________________________________________*/
class Pi3ServiceManager
{
private:
const PIObject *pObject;
PIString sStartType;
PIString sConfigPath;
SC_HANDLE schSCManager;
int bOK;
public:
Pi3ServiceManager( const PIObject *pTheObject, int, const char *[] )
: pObject( pTheObject ), bOK( 0 ), schSCManager( 0 )
{
PIConfig *pConfigDB = PIObject_getConfigurationDB( pObject );
sStartType = PIConfig_lookupValue( pConfigDB, KEY_CONF_STARTTYPE,
0, 0 );
sConfigPath = (const char *)DeQuote(PIConfig_lookupValue(
pConfigDB, KEY_CONF_CONFIGPATH, 0, 0 ));
schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if ( !schSCManager )
{
CONFIG_ERR( pObject, "Unable to access service control \
manager" );
return;
};
bOK = 1;
};
~Pi3ServiceManager()
{
CloseServiceHandle( schSCManager );
};
inline int Execute( int iArgc, const char *ppArgv[] )
{
int iRet = PIAPI_COMPLETED;
if ( iArgc<3 )
{
CONFIG_ERR( pObject, "Missing command name from arguments" );
return PIAPI_ERROR;
};
assert( ppArgv && ppArgv[2] );
if ( !ppArgv || !ppArgv[2] )
{
CONFIG_ERR( pObject, "Internal error in argument list" );
return PIAPI_ERROR;
};
if ( iArgc<4 )
{
CONFIG_ERR( pObject, "Missing service name from arguments" );
return PIAPI_ERROR;
};
assert( ppArgv[3] );
if ( !ppArgv[3] )
{
CONFIG_ERR( pObject, "Internal error in argument list" );
return PIAPI_ERROR;
};
PIString sCommand = ppArgv[2];
PIString sServiceName = ppArgv[3];
if ( sCommand == VALUE_EXISTSCOMMAND )
{
SC_HANDLE schService = OpenService(
schSCManager,
TEXT(sServiceName),
SERVICE_QUERY_STATUS );
if ( schService )
{
cout << "Yes";
CloseServiceHandle( schService );
}
else if ( !schService &&
GetLastError()==ERROR_SERVICE_DOES_NOT_EXIST )
{
cout << "No";
}
else if ( !schService )
{
PIOStrStream os;
os << "Could not open service '" << sServiceName << "'; \
Win32Error #" << GetLastError() << ends;
CONFIG_ERR(pObject, os.str() );
iRet = PIAPI_ERROR;
};
}
else if ( sCommand == VALUE_INSTALLCOMMAND )
{
/* --- get binary path --- */
if ( iArgc<5 )
{
CONFIG_ERR(pObject,"Missing binary path from arguments" );
return PIAPI_ERROR;
};
assert( ppArgv[4] );
if ( !ppArgv[4] )
{
CONFIG_ERR( pObject, "Internal error in argument list" );
return PIAPI_ERROR;
};
PIString sBinaryPath = ppArgv[4];
/* --- get start type --- */
DWORD dwStartType = 0U - 1U;
if ( sStartType=="SERVICE_BOOT_START" )
{ dwStartType = SERVICE_BOOT_START; }
else if ( sStartType=="SERVICE_SYSTEM_START" )
{ dwStartType = SERVICE_SYSTEM_START; }
else if ( sStartType=="SERVICE_AUTO_START" )
{ dwStartType = SERVICE_AUTO_START; }
else if ( sStartType=="SERVICE_DEMAND_START" )
{ dwStartType = SERVICE_DEMAND_START; }
else if ( sStartType=="SERVICE_DISABLED" )
{ dwStartType = SERVICE_DISABLED; }
else
{
PIOStrStream os;
os << "Unknown service start type '"<<sStartType<< "'" << endl;
CONFIG_ERR( pObject, os.str() );
return PIAPI_ERROR;
};
SC_HANDLE schService = CreateService(
schSCManager,
TEXT(sServiceName),
sServiceName,
SERVICE_ALL_ACCESS | SERVICE_INTERACTIVE_PROCESS,
SERVICE_WIN32_OWN_PROCESS,
dwStartType,
SERVICE_ERROR_NORMAL,
sBinaryPath,
NULL,
NULL,
NULL,
NULL,
NULL );
if ( !schService )
{
PIOStrStream os;
os << "Could not install service with service name '\
" << sServiceName << "' and binary path '" << sBinaryPath << "'; \
Win32Error #" << GetLastError() << ends;
CONFIG_ERR(pObject, os.str() );
iRet = PIAPI_ERROR;
}
else
{
CloseServiceHandle( schService );
if ( sConfigPath.Len() )
{
HKEY hKey;
DWORD dwDisposition;
if ( RegCreateKeyEx(
HKEY_LOCAL_MACHINE, // handle of an open key
"SOFTWARE\\Pi3\\Pi3Svc", // address of subkey name
0, // reserved
NULL, // address of class string
REG_OPTION_NON_VOLATILE, // special options flag
KEY_SET_VALUE, // desired security access
NULL, // address of key security structure
&hKey, // address of buffer for opened handle
&dwDisposition // address of disposition value buffer
) == ERROR_SUCCESS )
{
if ( RegSetValueEx(
hKey, // handle of key to set value for
KEY_CONF_CONFIGPATH, // address of value to set
0, // reserved
REG_SZ, // flag for value type
(const unsigned char *)(const char *)sConfigPath,
// address of value data
sConfigPath.Len() // size of value data
) != ERROR_SUCCESS )
{
PIOStrStream os;
os << "Could not write registry value for service with name '\
" << sServiceName << "' and binary path '" << sBinaryPath << "'; \
Win32Error #" << GetLastError() << ends;
CONFIG_ERR(pObject, os.str() );
iRet = PIAPI_ERROR;
};
RegCloseKey(hKey);
}
else
{
PIOStrStream os;
os << "Could not create registry key for service with name '\
" << sServiceName << "' and binary path '" << sBinaryPath << "'; \
Win32Error #" << GetLastError() << ends;
CONFIG_ERR(pObject, os.str() );
iRet = PIAPI_ERROR;
}
};
};
}
else if ( sCommand == VALUE_DELETECOMMAND )
{
SC_HANDLE schService = OpenService(
schSCManager,
TEXT(sServiceName),
DELETE );
if ( !schService )
{
PIOStrStream os;
os << "Could not open service '" << sServiceName << "' for \
delete operation; Win32Error #" << GetLastError() << ends;
CONFIG_ERR(pObject, os.str() );
iRet = PIAPI_ERROR;
}
else if ( !DeleteService( schService ) )
{
PIOStrStream os;
os << "Could not delete service '" << sServiceName << "'; \
Win32Error #" << GetLastError() << ends;
CONFIG_ERR(pObject, os.str() );
iRet = PIAPI_ERROR;
};
if ( schService )
{
CloseServiceHandle( schService );
if ( RegDeleteKey( HKEY_LOCAL_MACHINE,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -