Files
harbour-core/harbour/contrib/hbwin/win_svc.c
Viktor Szakats f145ef4c80 2010-06-18 11:58 UTC+0200 Viktor Szakats (harbour.01 syenar.hu)
* include/hbdefs.h
    ! Fixed one intentional typo I put in to test legacy types.

  * src/compiler/hbmain.c
  * src/compiler/genhrb.c
  * src/compiler/gencc.c
  * src/compiler/hbcmplib.c
  * include/hbcomp.h
  * include/hbcompdf.h
    * HB_ULONG -> HB_SIZE, where I could find out from warnings.
    + Added parameter names to some function declarations where they
      were missing.
    ; Przemek, please check me. I feel I will mess this up if continuing
      beyond this point, so I'd appreciate if you could take a look at
      HB_ULONG to HB_SIZE conversion from an intrinsic perspective,
      there seem to be places where it needs further tweaks to get us
      full Win64 support. [ I've also intentionally left this area
      more or less intact when doing the previous pass of conversion. ]
    ; There are places where simple 'int' is used, while it should be
      HB_SIZE (or maybe size_t? in some places).
    ; Also, Win64 conversion will need some more work f.e. in filesys.c
      where WinAPI file I/O functions can't accept a 64-bit value.
      First I will report these and if they seem complicated I'll
      revert to HB_SIZE = HB_ULONG for Win64 in the being.

  * src/compiler/hbgenerr.c
    * Formatting.

  * src/pp/ppcore.c
  * src/vm/strapi.c
  * src/vm/debug.c
  * src/vm/itemapi.c
  * src/vm/cmdarg.c
  * src/vm/set.c
  * src/debug/dbgentry.c
  * src/common/hbgete.c
  * src/common/hbstr.c
  * src/common/strwild.c
  * src/nortl/nortl.c
  * src/rtl/lennum.c
  * src/rtl/strmatch.c
  * src/rtl/gtstd/gtstd.c
  * src/rtl/hbstrfmt.c
  * src/rtl/transfrm.c
  * src/rtl/gtcgi/gtcgi.c
  * src/rtl/direct.c
  * src/rtl/filesys.c
  * src/rtl/console.c
  * src/rtl/hbgtcore.c
  * src/rtl/cdpapi.c
  * src/rtl/mlcfunc.c
  * src/rtl/itemseri.c
  * src/rtl/gtpca/gtpca.c
  * src/rtl/trace.c
  * src/rtl/samples.c
  * src/rtl/gete.c
  * src/rdd/workarea.c
  * src/rdd/hsx/hsx.c
  * src/rdd/hbsix/sxsem.c
  * src/rdd/hbsix/sxfname.c
  * contrib/hbct/tab.c
  * contrib/xhb/dbf2txt.c
  * contrib/xhb/datesxhb.c
  * contrib/xhb/dbgfxc.c
  * contrib/hbmzip/hbmzip.c
  * contrib/hbnf/fttext.c
  * contrib/hbnetio/netiocli.c
  * contrib/hbpgsql/postgres.c
  * contrib/hbclipsm/num.c
  * contrib/rddads/ads1.c
  * contrib/sddsqlt3/sddsqlt3.c
  * contrib/hbfimage/fi_wrp.c
  * contrib/sddodbc/sddodbc.c
  * contrib/sddoci/sddoci.c
  * contrib/hbwin/win_svc.c
  * contrib/hbwin/win_prn2.c
    * '( HB_SIZE ) strlen(' -> 'strlen('
2010-06-18 10:03:44 +00:00

315 lines
9.4 KiB
C

/*
* $Id$
*/
/*
* Harbour Project source code:
* Windows Service API
*
* Copyright 2010 Jose Luis Capel - <jlcapel at hotmail . com>
* www - http://harbour-project.org
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this software; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
*
* As a special exception, the Harbour Project gives permission for
* additional uses of the text contained in its release of Harbour.
*
* The exception is that, if you link the Harbour libraries with other
* files to produce an executable, this does not by itself cause the
* resulting executable to be covered by the GNU General Public License.
* Your use of that executable is in no way restricted on account of
* linking the Harbour library code into it.
*
* This exception does not however invalidate any other reasons why
* the executable file might be covered by the GNU General Public License.
*
* This exception applies only to the code released by the Harbour
* Project under the name Harbour. If you copy code from other
* Harbour Project or Free Software Foundation releases into a copy of
* Harbour, as the General Public License permits, the exception does
* not apply to the code that you add in this way. To avoid misleading
* anyone as to the status of such modified files, you must delete
* this exception notice from them.
*
* If you write modifications of your own for Harbour, it is your choice
* whether to permit this exception to apply to your modifications.
* If you do not wish that, delete this exception notice.
*
*/
#include "hbwapi.h"
#include "hbvm.h"
#if ! defined( HB_OS_WIN_CE )
#if defined( __POCC__ ) || defined( __XCC__ )
# include <winsvc.h> /* it's disabled by WIN32_LEAN_AND_MEAN */
#endif
static SERVICE_STATUS s_ServiceStatus;
static SERVICE_STATUS_HANDLE s_hStatus;
static char s_szHarbourEntryFunc[ HB_SYMBOL_NAME_LEN + 1 ];
static TCHAR s_lpServiceName[ 256 ];
/* Control handler function */
static VOID WINAPI hbwin_SvcControlHandler( DWORD fdwControl )
{
switch( fdwControl )
{
case SERVICE_CONTROL_STOP:
s_ServiceStatus.dwWin32ExitCode = 0;
s_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
return;
case SERVICE_CONTROL_SHUTDOWN:
s_ServiceStatus.dwWin32ExitCode = 0;
s_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
return;
}
SetServiceStatus( s_hStatus, &s_ServiceStatus ); /* Report current status */
}
static VOID WINAPI hbwin_SvcMainFunction( DWORD dwArgc, LPTSTR * lpszArgv )
{
s_ServiceStatus.dwServiceType = SERVICE_WIN32;
s_ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
s_ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
s_ServiceStatus.dwWin32ExitCode = 0;
s_ServiceStatus.dwServiceSpecificExitCode = 0;
s_ServiceStatus.dwCheckPoint = 0;
s_ServiceStatus.dwWaitHint = 0;
s_hStatus = RegisterServiceCtrlHandler( s_lpServiceName, ( LPHANDLER_FUNCTION ) hbwin_SvcControlHandler );
if( s_hStatus != ( SERVICE_STATUS_HANDLE ) 0 )
{
PHB_DYNS pDynSym = hb_dynsymFindName( s_szHarbourEntryFunc );
if( pDynSym )
{
if( hb_vmRequestReenter() )
{
DWORD i;
int iArgCount = 0;
/* We report the running status to SCM. */
s_ServiceStatus.dwCurrentState = SERVICE_RUNNING;
SetServiceStatus( s_hStatus, &s_ServiceStatus );
hb_vmPushSymbol( hb_dynsymSymbol( pDynSym ) );
hb_vmPushNil();
for( i = 1; i < dwArgc; ++i )
{
char * pszArg = HB_TCHAR_CONVFROM( lpszArgv[ i ] );
if( ! hb_cmdargIsInternal( pszArg, NULL ) )
{
hb_vmPushString( pszArg, strlen( pszArg ) );
++iArgCount;
}
HB_TCHAR_FREE( pszArg );
}
hb_vmProc( ( HB_USHORT ) iArgCount );
hb_vmRequestRestore();
}
}
else
{
HB_TRACE( HB_TR_DEBUG, ("Harbour service entry function not found") );
}
}
else
{
HB_TRACE( HB_TR_DEBUG, ("Error registering service") );
}
}
#endif
HB_FUNC( WIN_SERVICEGETSTATUS )
{
#if ! defined( HB_OS_WIN_CE )
hb_retnl( s_ServiceStatus.dwCurrentState );
#else
hb_retnl( 0 );
#endif
}
HB_FUNC( WIN_SERVICESETSTATUS )
{
#if ! defined( HB_OS_WIN_CE )
s_ServiceStatus.dwCurrentState = ( DWORD ) hb_parnl( 1 );
hb_retl( SetServiceStatus( s_hStatus, &s_ServiceStatus ) );
#else
hb_retl( HB_FALSE );
#endif
}
HB_FUNC( WIN_SERVICESETEXITCODE )
{
#if ! defined( HB_OS_WIN_CE )
s_ServiceStatus.dwWin32ExitCode = ( DWORD ) hb_parnl( 1 );
hb_retl( SetServiceStatus( s_hStatus, &s_ServiceStatus ) );
#else
hb_retl( HB_FALSE );
#endif
}
HB_FUNC( WIN_SERVICESTOP )
{
#if ! defined( HB_OS_WIN_CE )
s_ServiceStatus.dwCurrentState = SERVICE_STOPPED;
SetServiceStatus( s_hStatus, &s_ServiceStatus );
#endif
}
HB_FUNC( WIN_SERVICEINSTALL )
{
HB_BOOL bRetVal = HB_FALSE;
#if ! defined( HB_OS_WIN_CE )
void * hPath;
LPCTSTR lpPath = HB_PARSTR( 3, &hPath, NULL );
TCHAR lpPathBuffer[ MAX_PATH ];
if( lpPath == NULL )
{
if( GetModuleFileName( NULL, lpPathBuffer, HB_SIZEOFARRAY( lpPathBuffer ) ) )
lpPath = lpPathBuffer;
else
hbwapi_SetLastError( GetLastError() );
}
if( lpPath )
{
SC_HANDLE schSCM = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( schSCM )
{
SC_HANDLE schSrv;
void * hServiceName;
void * hDisplayName;
LPCTSTR lpServiceName = HB_PARSTRDEF( 1, &hServiceName, NULL );
LPCTSTR lpDisplayName = HB_PARSTRDEF( 2, &hDisplayName, NULL );
schSrv = CreateService( schSCM, /* SCM database */
lpServiceName, /* name of service */
lpDisplayName, /* service name to display */
SERVICE_ALL_ACCESS, /* desired access */
SERVICE_WIN32_OWN_PROCESS, /* service type */
SERVICE_DEMAND_START, /* start type */
SERVICE_ERROR_NORMAL, /* error control type */
lpPath, /* path to service's binary */
NULL, /* no load ordering group */
NULL, /* no tag identifier */
NULL, /* no dependencies */
NULL, /* LocalSystem account */
NULL ); /* no password */
if( schSrv )
{
bRetVal = HB_TRUE;
CloseServiceHandle( schSrv );
}
else
hbwapi_SetLastError( GetLastError() );
hb_strfree( hServiceName );
hb_strfree( hDisplayName );
CloseServiceHandle( schSCM );
}
else
hbwapi_SetLastError( GetLastError() );
}
hb_strfree( hPath );
#endif
hb_retl( bRetVal );
}
HB_FUNC( WIN_SERVICEDELETE )
{
HB_BOOL bRetVal = HB_FALSE;
#if ! defined( HB_OS_WIN_CE )
SC_HANDLE schSCM = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if( schSCM )
{
void * hServiceName;
SC_HANDLE schSrv = OpenService( schSCM,
HB_PARSTRDEF( 1, &hServiceName, NULL ),
SERVICE_ALL_ACCESS );
if( schSrv )
{
/* TODO: check if service is up and then stop it */
bRetVal = ( HB_BOOL ) DeleteService( schSrv );
CloseServiceHandle( schSrv );
}
else
hbwapi_SetLastError( GetLastError() );
hb_strfree( hServiceName );
CloseServiceHandle( schSCM );
}
else
hbwapi_SetLastError( GetLastError() );
#endif
hb_retl( bRetVal );
}
HB_FUNC( WIN_SERVICESTART )
{
HB_BOOL bRetVal = HB_FALSE;
#if ! defined( HB_OS_WIN_CE )
SERVICE_TABLE_ENTRY lpServiceTable[ 2 ];
HB_TCHAR_COPYTO( s_lpServiceName, hb_parcx( 1 ), HB_SIZEOFARRAY( s_lpServiceName ) - 1 );
hb_strncpy( s_szHarbourEntryFunc, hb_parcx( 2 ), HB_SIZEOFARRAY( s_szHarbourEntryFunc ) - 1 );
lpServiceTable[ 0 ].lpServiceName = s_lpServiceName;
lpServiceTable[ 0 ].lpServiceProc = ( LPSERVICE_MAIN_FUNCTION ) hbwin_SvcMainFunction;
lpServiceTable[ 1 ].lpServiceName = NULL;
lpServiceTable[ 1 ].lpServiceProc = NULL;
if( StartServiceCtrlDispatcher( lpServiceTable ) )
bRetVal = HB_TRUE;
else
hbwapi_SetLastError( GetLastError() );
#endif
hb_retl( bRetVal );
}