2016-09-05 17:04 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)

* contrib/hbwin/hbwin.ch
  * contrib/hbwin/hbwin.hbp
  * contrib/hbwin/hbwin.hbx
  + contrib/hbwin/win_svc_2.c
  * contrib/hbwin/win_svc.c -> contrib/hbwin/win_svc_1.c
    + add function win_ServiceRun( <cName>, [...] ) -> <lSuccess>
      to start a service by name
    + add WIN_SERVICE_CONTROL_* constants
    + add function win_ServiceControl( <cName>, <nControlCode> ) -> <lSuccess>
    + add 2nd parameter to win_ServiceDelete( <cName>, <lForce> ) -> <lSuccess>
      <lForce> will attempt to stop the service before deleting it.
    % separate service management functions and Harbour app-as-service
      support functions into separate sources
    ; build-tested only, function names not finalized.
    * set wapi_GetLastError() more consistently
    + win_ServiceInstall() add two new optional parameters:
      <cUsername> (5th) and <cPassword> (6th)
    ! win_ServiceInstall() do not convert an omitted
      <cDisplayName> (2nd) parameter to empty string, pass it
      to Windows as NULL instead
    % use HB_IT_EVALITEM instead of ( HB_IT_BLOCK | HB_IT_SYMBOL )
    ; Above patches come from 3.4 fork commits below:
      2015-09-03 18:52 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
      2015-09-02 19:54 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
      2015-09-02 18:43 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
      2015-04-03 11:43 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
      2014-01-29 02:15 UTC+0100 Viktor Szakáts (vszakats users.noreply.github.com)
This commit is contained in:
Viktor Szakats
2016-09-05 17:07:11 +02:00
parent 6a6f4d962d
commit d8c6c562f3
6 changed files with 352 additions and 123 deletions

View File

@@ -10,6 +10,35 @@
* Change, ! Fix, % Optimization, + Addition, - Removal, ; Comment
*/
2016-09-05 17:04 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
* contrib/hbwin/hbwin.ch
* contrib/hbwin/hbwin.hbp
* contrib/hbwin/hbwin.hbx
+ contrib/hbwin/win_svc_2.c
* contrib/hbwin/win_svc.c -> contrib/hbwin/win_svc_1.c
+ add function win_ServiceRun( <cName>, [...] ) -> <lSuccess>
to start a service by name
+ add WIN_SERVICE_CONTROL_* constants
+ add function win_ServiceControl( <cName>, <nControlCode> ) -> <lSuccess>
+ add 2nd parameter to win_ServiceDelete( <cName>, <lForce> ) -> <lSuccess>
<lForce> will attempt to stop the service before deleting it.
% separate service management functions and Harbour app-as-service
support functions into separate sources
; build-tested only, function names not finalized.
* set wapi_GetLastError() more consistently
+ win_ServiceInstall() add two new optional parameters:
<cUsername> (5th) and <cPassword> (6th)
! win_ServiceInstall() do not convert an omitted
<cDisplayName> (2nd) parameter to empty string, pass it
to Windows as NULL instead
% use HB_IT_EVALITEM instead of ( HB_IT_BLOCK | HB_IT_SYMBOL )
; Above patches come from 3.4 fork commits below:
2015-09-03 18:52 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
2015-09-02 19:54 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
2015-09-02 18:43 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
2015-04-03 11:43 UTC+0200 Viktor Szakats (vszakats users.noreply.github.com)
2014-01-29 02:15 UTC+0100 Viktor Szakáts (vszakats users.noreply.github.com)
2016-08-10 20:12 UTC+0300 Mindaugas Kavaliauskas (dbtopas/at/dbtopas.lt)
* contrib/rddads/ads.ch
+ 64bit server type constants included

View File

@@ -672,6 +672,17 @@
#define WIN_SERVICE_DISABLED 0x00000004
#define WIN_SERVICE_SYSTEM_START 0x00000001
/* win_serviceControl() control codes */
#define WIN_SERVICE_CONTROL_STOP 0x00000001
#define WIN_SERVICE_CONTROL_PAUSE 0x00000002
#define WIN_SERVICE_CONTROL_CONTINUE 0x00000003
#define WIN_SERVICE_CONTROL_INTERROGATE 0x00000004
#define WIN_SERVICE_CONTROL_PARAMCHANGE 0x00000006
#define WIN_SERVICE_CONTROL_NETBINDADD 0x00000007
#define WIN_SERVICE_CONTROL_NETBINDREMOVE 0x00000008
#define WIN_SERVICE_CONTROL_NETBINDENABLE 0x00000009
#define WIN_SERVICE_CONTROL_NETBINDDISABLE 0x0000000A
/* wapi_MessageBox()/wapi_MessageBeep() flags */
#define WIN_MB_OK 0x00000000
#define WIN_MB_OKCANCEL 0x00000001

View File

@@ -59,7 +59,8 @@ win_prn3.c
win_regc.c
win_rpc.c
win_shell.c
win_svc.c
win_svc_1.c
win_svc_2.c
oleauto.prg
axfunc.prg

View File

@@ -394,9 +394,11 @@ DYNAMIC win_regSetValueEx
DYNAMIC win_regWrite
DYNAMIC win_ReportEvent
DYNAMIC win_RunDetached
DYNAMIC win_serviceControl
DYNAMIC win_serviceDelete
DYNAMIC win_serviceGetStatus
DYNAMIC win_serviceInstall
DYNAMIC win_serviceRun
DYNAMIC win_serviceSetExitCode
DYNAMIC win_serviceSetStatus
DYNAMIC win_serviceStart

View File

@@ -1,7 +1,7 @@
/*
* Windows Service API
*
* Copyright 2010 Jose Luis Capel - <jlcapel at hotmail . com>
* Copyright 2010 Jose Luis Capel <jlcapel at hotmail . com>
*
* 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
@@ -52,7 +52,7 @@
#if ! defined( HB_OS_WIN_CE )
#if defined( __POCC__ ) || defined( __XCC__ )
# include <winsvc.h> /* it's disabled by WIN32_LEAN_AND_MEAN */
#include <winsvc.h> /* it's disabled by WIN32_LEAN_AND_MEAN */
#endif
static SERVICE_STATUS s_ServiceStatus;
@@ -76,7 +76,7 @@ static VOID WINAPI hbwin_SvcControlHandler( DWORD fdwControl )
return;
}
SetServiceStatus( s_hStatus, &s_ServiceStatus ); /* Report current status */
SetServiceStatus( s_hStatus, &s_ServiceStatus ); /* Report current status */
}
static VOID WINAPI hbwin_SvcMainFunction( DWORD dwArgc, LPTSTR * lpszArgv )
@@ -137,18 +137,22 @@ static VOID WINAPI hbwin_SvcMainFunction( DWORD dwArgc, LPTSTR * lpszArgv )
HB_FUNC( WIN_SERVICEGETSTATUS )
{
#if ! defined( HB_OS_WIN_CE )
hb_retnl( s_ServiceStatus.dwCurrentState );
hb_retnint( s_ServiceStatus.dwCurrentState );
#else
hb_retnl( 0 );
hb_retnint( 0 );
#endif
}
HB_FUNC( WIN_SERVICESETSTATUS )
{
#if ! defined( HB_OS_WIN_CE )
HB_BOOL bRetVal;
s_ServiceStatus.dwCurrentState = ( DWORD ) hb_parnl( 1 );
hb_retl( SetServiceStatus( s_hStatus, &s_ServiceStatus ) );
bRetVal = ( HB_BOOL ) SetServiceStatus( s_hStatus, &s_ServiceStatus );
hbwapi_SetLastError( GetLastError() );
hb_retl( bRetVal );
#else
hbwapi_SetLastError( ERROR_NOT_SUPPORTED );
hb_retl( HB_FALSE );
#endif
}
@@ -156,9 +160,13 @@ HB_FUNC( WIN_SERVICESETSTATUS )
HB_FUNC( WIN_SERVICESETEXITCODE )
{
#if ! defined( HB_OS_WIN_CE )
HB_BOOL bRetVal;
s_ServiceStatus.dwWin32ExitCode = ( DWORD ) hb_parnl( 1 );
hb_retl( SetServiceStatus( s_hStatus, &s_ServiceStatus ) );
bRetVal = ( HB_BOOL ) SetServiceStatus( s_hStatus, &s_ServiceStatus );
hbwapi_SetLastError( GetLastError() );
hb_retl( bRetVal );
#else
hbwapi_SetLastError( ERROR_NOT_SUPPORTED );
hb_retl( HB_FALSE );
#endif
}
@@ -166,122 +174,20 @@ HB_FUNC( WIN_SERVICESETEXITCODE )
HB_FUNC( WIN_SERVICESTOP )
{
#if ! defined( HB_OS_WIN_CE )
HB_BOOL bRetVal;
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 */
HB_ISNUM( 4 ) ? ( DWORD ) hb_parnl( 4 ) : 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
bRetVal = ( HB_BOOL ) SetServiceStatus( s_hStatus, &s_ServiceStatus );
hbwapi_SetLastError( GetLastError() );
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() );
#else
hbwapi_SetLastError( ERROR_NOT_SUPPORTED );
hb_retl( HB_FALSE );
#endif
hb_retl( bRetVal );
}
HB_FUNC( WIN_SERVICESTART )
{
HB_BOOL bRetVal = HB_FALSE;
HB_BOOL bRetVal;
#if ! defined( HB_OS_WIN_CE )
PHB_ITEM pEntryFunc;
@@ -296,7 +202,7 @@ HB_FUNC( WIN_SERVICESTART )
s_pHarbourEntryFunc = NULL;
}
pEntryFunc = hb_param( 2, HB_IT_BLOCK | HB_IT_SYMBOL );
pEntryFunc = hb_param( 2, HB_IT_EVALITEM );
if( pEntryFunc )
s_pHarbourEntryFunc = hb_itemNew( pEntryFunc );
@@ -307,11 +213,11 @@ HB_FUNC( WIN_SERVICESTART )
lpServiceTable[ 1 ].lpServiceName = NULL;
lpServiceTable[ 1 ].lpServiceProc = NULL;
if( StartServiceCtrlDispatcher( lpServiceTable ) )
bRetVal = HB_TRUE;
else
hbwapi_SetLastError( GetLastError() );
bRetVal = ( HB_BOOL ) StartServiceCtrlDispatcher( lpServiceTable );
hbwapi_SetLastError( GetLastError() );
#else
bRetVal = HB_FALSE;
hbwapi_SetLastError( ERROR_NOT_SUPPORTED );
#endif
hb_retl( bRetVal );
}

280
contrib/hbwin/win_svc_2.c Normal file
View File

@@ -0,0 +1,280 @@
/*
* Windows Service Management API
*
* Copyright 2010 Jose Luis Capel <jlcapel at hotmail . com>
*
* 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.txt. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307 USA (or visit the web site https://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"
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;
void * hAccountName;
void * hPassword;
LPCTSTR lpServiceName = HB_PARSTRDEF( 1, &hServiceName, NULL );
LPCTSTR lpDisplayName = HB_PARSTR( 2, &hDisplayName, NULL );
LPCTSTR lpAccountName = HB_PARSTR( 5, &hAccountName, NULL );
LPCTSTR lpPassword = HB_PARSTR( 6, &hPassword, 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 */
( DWORD ) hb_parnldef( 4, 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 */
lpAccountName, /* default: LocalSystem account */
lpPassword ); /* default: no password */
hbwapi_SetLastError( GetLastError() );
if( schSrv )
{
bRetVal = HB_TRUE;
CloseServiceHandle( schSrv );
}
hb_strfree( hServiceName );
hb_strfree( hDisplayName );
hb_strfree( hAccountName );
hb_strfree( hPassword );
CloseServiceHandle( schSCM );
}
else
hbwapi_SetLastError( GetLastError() );
}
hb_strfree( hPath );
#else
hbwapi_SetLastError( ERROR_NOT_SUPPORTED );
#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 )
{
if( hb_parl( 2 ) ) /* Check if service is up and stop it */
{
SERVICE_STATUS ssStatus;
if( ControlService( schSrv, SERVICE_CONTROL_STOP, &ssStatus ) )
{
while( ssStatus.dwCurrentState != SERVICE_STOPPED &&
QueryServiceStatus( schSrv, &ssStatus ) )
hb_idleSleep( 1.0 );
}
}
bRetVal = ( HB_BOOL ) DeleteService( schSrv );
hbwapi_SetLastError( GetLastError() );
CloseServiceHandle( schSrv );
}
else
hbwapi_SetLastError( GetLastError() );
hb_strfree( hServiceName );
CloseServiceHandle( schSCM );
}
else
hbwapi_SetLastError( GetLastError() );
#else
hbwapi_SetLastError( ERROR_NOT_SUPPORTED );
#endif
hb_retl( bRetVal );
}
HB_FUNC( WIN_SERVICECONTROL )
{
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 )
{
SERVICE_STATUS ssStatus;
memset( &ssStatus, 0, sizeof( ssStatus ) );
bRetVal = ( HB_BOOL ) ControlService( schSrv, ( DWORD ) hb_parnl( 2 ), &ssStatus );
hbwapi_SetLastError( GetLastError() );
CloseServiceHandle( schSrv );
}
else
hbwapi_SetLastError( GetLastError() );
hb_strfree( hServiceName );
CloseServiceHandle( schSCM );
}
else
hbwapi_SetLastError( GetLastError() );
#else
hbwapi_SetLastError( ERROR_NOT_SUPPORTED );
#endif
hb_retl( bRetVal );
}
HB_FUNC( WIN_SERVICERUN )
{
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 )
{
DWORD dwArgs, pos;
void ** hArgs;
LPCTSTR * lpArgs;
if( hb_pcount() >= 2 )
{
dwArgs = hb_pcount() - 1;
hArgs = ( void ** ) hb_xgrab( dwArgs * sizeof( void * ) );
lpArgs = ( LPCTSTR * ) hb_xgrab( dwArgs * sizeof( LPCTSTR ) );
for( pos = 0; pos < dwArgs; ++pos )
lpArgs[ pos ] = HB_PARSTRDEF( pos + 2, &hArgs[ pos ], NULL );
}
else
{
dwArgs = 0;
hArgs = NULL;
lpArgs = NULL;
}
bRetVal = ( HB_BOOL ) StartService( schSrv, dwArgs, lpArgs );
hbwapi_SetLastError( GetLastError() );
if( hArgs )
{
for( pos = 0; pos < dwArgs; ++pos )
hb_strfree( hArgs[ pos ] );
hb_xfree( hArgs );
hb_xfree( lpArgs );
}
CloseServiceHandle( schSrv );
}
else
hbwapi_SetLastError( GetLastError() );
hb_strfree( hServiceName );
CloseServiceHandle( schSCM );
}
else
hbwapi_SetLastError( GetLastError() );
#else
hbwapi_SetLastError( ERROR_NOT_SUPPORTED );
#endif
hb_retl( bRetVal );
}