📄 control.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/EnhPi3/Control.cpp,v $
* $Date: 2004/07/04 19:32:22 $
*
Description:
Functions to control execution of Pi3Web server on windows
platforms.
Most functions here return 1 for true/success, 0 for false and -1
for errors. This is true unless otherwise stated.
\*____________________________________________________________________________*/
/* $SourceTop:$ */
#include <assert.h>
#include <windows.h>
#include <stdio.h>
#include <regstr.h>
#include "MiscUtil.h"
#include "SwapStr.h"
#include "Control.h"
/*____________________________________________________________________________*\
*
Description:
\*____________________________________________________________________________*/
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
SC_HANDLE Internal_openServiceManager()
{
SC_HANDLE schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
return schSCManager;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
void Internal_closeServiceManager( SC_HANDLE schSCManager )
{
CloseServiceHandle( schSCManager );
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
Return code: 1=service started, 0=service not running, -1=error
\*____________________________________________________________________________*/
int Internal_trackServiceState( HANDLE schService )
{
SERVICE_STATUS ssStatus;
/*
** Get service status
*/
if ( !QueryServiceStatus( schService, &ssStatus ) )
{
DWORD dwError = ::GetLastError();
if ( dwError==ERROR_SERVICE_NOT_ACTIVE )
return 0;
else
return -1;
};
/*
** Check if service is clearly running or not running
*/
switch( ssStatus.dwCurrentState )
{
case SERVICE_RUNNING: return 1;
case SERVICE_STOPPED: return 0;
default:;
};
/*
** Track service as it tries to start or stop
*/
DWORD dwOldCheckPoint = ssStatus.dwCheckPoint;
for(;;)
{
/*
** Service is running
*/
Sleep( ssStatus.dwWaitHint );
if ( !QueryServiceStatus( schService, &ssStatus ) )
{
DWORD dwError = ::GetLastError();
if ( dwError==ERROR_SERVICE_NOT_ACTIVE )
return 0;
else
return -1; /* error - unknown */
};
/*
** Check if service is clearly running or not running
*/
switch( ssStatus.dwCurrentState )
{
case SERVICE_RUNNING: return 1;
case SERVICE_STOPPED: return 0;
default:;
};
/*
** Check if service has stalled
*/
if ( dwOldCheckPoint >= ssStatus.dwCheckPoint )
{ return -1; }; /* error - stalled */
dwOldCheckPoint = ssStatus.dwCheckPoint;
};
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int CTRL_isServiceInstalled( const char *pServiceName )
{
assert( pServiceName );
SC_HANDLE scH = Internal_openServiceManager();
if ( !scH )
{ return -1; };
HANDLE schService = OpenService(
scH, pServiceName, SERVICE_QUERY_STATUS );
int iRet = 1;
if ( schService )
{
CloseServiceHandle( schService );
}
else
{
iRet = GetLastError()==ERROR_SERVICE_DOES_NOT_EXIST ? 0 : -1;
}
Internal_closeServiceManager( scH );
return iRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int CTRL_installService( const char *pServiceName, const char *pPath )
{
assert( pServiceName && pPath );
SC_HANDLE scH = Internal_openServiceManager();
if ( !scH )
{ return -1; };
DWORD dwStartType = SERVICE_AUTO_START;
HANDLE schService = CreateService(
scH,
pServiceName,
pServiceName,
SERVICE_ALL_ACCESS | SERVICE_INTERACTIVE_PROCESS,
SERVICE_WIN32_OWN_PROCESS,
dwStartType,
SERVICE_ERROR_NORMAL,
pPath,
NULL,
NULL,
NULL,
NULL,
NULL );
Internal_closeServiceManager( scH );
if ( !schService )
{ return -1; };
CloseServiceHandle( schService );
return 1;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int CTRL_unInstallService( const char *pServiceName )
{
assert( pServiceName );
SC_HANDLE scH = Internal_openServiceManager();
if ( !scH )
{ return -1; };
HANDLE schService = OpenService(
scH, pServiceName, DELETE );
Internal_closeServiceManager( scH );
if ( !schService )
{ return -1; };
int iRet = DeleteService( schService );
CloseServiceHandle( schService );
return iRet ? 1 : -1;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int CTRL_isServiceRunning( const char *pServiceName )
{
assert( pServiceName );
SC_HANDLE scH = Internal_openServiceManager();
if ( !scH )
{ return -1; };
HANDLE schService = OpenService(
scH, pServiceName, SERVICE_QUERY_STATUS );
Internal_closeServiceManager( scH );
if ( !schService )
{ return -1; };
int iRet = Internal_trackServiceState( schService );
CloseServiceHandle( schService );
return iRet;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
int CTRL_startService( const char *pServiceName )
{
assert( pServiceName );
SC_HANDLE scH = Internal_openServiceManager();
if ( !scH )
{ return -1; };
HANDLE schService = ::OpenService(
scH, pServiceName, SERVICE_START | SERVICE_QUERY_STATUS );
Internal_closeServiceManager( scH );
if ( !schService )
{ return -1; };
/*
** Try to start service
*/
if ( !::StartService( schService, 0, NULL ) )
{ goto start_service_fail; };
/*
** Track service while it starts
*/
if ( Internal_trackServiceState( schService )!=1 )
{ goto start_service_fail; };
/*
** OK!
*/
::CloseServiceHandle( schService );
return 1;
start_service_fail:
::CloseServiceHandle( schService );
return -1;
}
/*____________________________________________________________________________*\
*
Function:
Synopsis:
Description:
\*____________________________________________________________________________*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -