Create a windows service

Create a windows service

Postby leandro » Mon Apr 10, 2023 3:50 pm

Buenos días para todos,

Tim buenos días como estas?, estuve consultando el foro como crear un servicio de windows y todo los caminos conducen hacia ti, jejejejeje.
De casualidad me puedes indicar el camino a seguir para lograr crear el servicio, y si de casualidad tienes un ejemplo te lo agradecería, de antemano gracias.

Good morning Tim, how are you? I was consulting the forum on how to create a windows service and all roads lead to you, hehehehehe.
By chance can you tell me the way to follow to create the service, and if by chance you have an example I would appreciate it, thanks in advance.
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1669
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Create a windows service

Postby TimStone » Mon Apr 10, 2023 9:28 pm

Here is the structure that I use. All commands go in the area noted /*. */
Code: Select all  Expand view
/*
                                                PRG:                      MLS12us.exe   Auto Updating Service
                                                Purpose:              Background running routine to handle updates and submissions2/15/2022 4:51:32 PM
                                               
                                                Updated:             11/26/2022 5:07:44 PM
*/
                                         
 
#INCLUDE "Fivewin.CH"
#INCLUDE "ini.CH"
#INCLUDE "TDATA.CH"
#INCLUDE "common.CH"
#INCLUDE "mail.ch"
#include "fileio.ch"
#include "hbwin.ch"
#include "hbmzip.ch"
#include "ads.ch"
#INCLUDE "constant.ch"
#INCLUDE "hbClass.ch"
#include "hbcurl.ch"
 
#DEFINE INTERNET_FLAG_PASSIVE    0x08000000
#define  HKEY_LOCAL_MACHINE      2147483650
EXTERN ADSKeyCount, ADSKeyNo, AdsGetRelKeyPos, AdsSetRelKeyPos
 
#if ! defined( __HBSCRIPT__HBSHELL )
#define _SERVICE_NAME "MLS12_Auto_Updater"
 
PROCEDURE Main( cMode )
 
   LOCAL nError
   LOCAL cMsg
 
   hb_default( @cMode, "S" ) // NOTE: Must be the default action
 
   SWITCH Upper( cMode )
   CASE "I"
 
      IF win_serviceInstall( _SERVICE_NAME, "MLS Auto Updater Service" )
         ? "Service has been successfully installed"
      ELSE
         nError := wapi_GetLastError()
         cMsg := Space( 128 )
         wapi_FormatMessage( ,,,, @cMsg )
         ? "Error installing service: " + hb_ntos( nError ) + " " + cMsg
      ENDIF
      EXIT
 
   CASE "U"
 
      IF win_serviceDelete( _SERVICE_NAME )
         ? "Service has been deleted"
      ELSE
         nError := wapi_GetLastError()
         cMsg := Space( 128 )
         wapi_FormatMessage( ,,,, @cMsg )
         ? "Error deleting service: " + hb_ntos( nError ) + " " + cMsg
      ENDIF
      EXIT
 
   CASE "S"
 
      // NOTE: Used when starting up as service.  Do not invoke the executable manually with this option
 
      IF win_serviceStart( _SERVICE_NAME, @SrvMain() )
         ? "Service has started OK"
      ELSE
         nError := wapi_GetLastError()
         cMsg := Space( 128 )
         wapi_FormatMessage( ,,,, @cMsg )
         ? "Service has had some problems: " + hb_ntos( nError ) + " " + cMsg
      ENDIF
      EXIT
 
   ENDSWITCH
 
RETURN
 
 
PROCEDURE SrvMain( cParam1, cParam2 )
 
                LOCAL n := 0
                LOCAL cParam
                LOCAL CurDir  := CURDIR(), CurDri  := DISKNAME()
                LOCAL owIcon, oIni, meSavStr, nHandle, nServer := 7
                PRIVATE oWnd, oTray, oIcon, oIcon1, oTmr
 
                PRIVATE  cPath := hb_DirBase() + "DATA\"// CurDri + ":\" + CurDir + "\Data\"
                PRIVATE  cDnPath := hb_DirBase() + "
MLS\"
                PRIVATE cBakPath := CurDri + "
:\" + CurDir
                PRIVATE cValue, cStart := Time( )            
                PRIVATE cInstVer, dLstBkUp, cGetSN, eSavStr, lCFXActive, dCFLstDte,;
                                dvChkDte, cvChkVer, lvOKToDl, cvOnSrvr, cvAuthTo, cvChkBul, dCLLstDte,;
                                sCurDte, sWkCxDte, sWkClDte, cBackupPath, ldForce, lKukui, oDbLog
                PRIVATE mCLinkAC, mCLinkFt, mCLinkPw, mCLinkUn, oDcf, lCloudBackup := .f.
 
                PUBLIC fhnd := hb_FCreate( hb_FNameExtSet( hb_ProgName(), "
.out" ), FC_NORMAL, FO_DENYNONE + FO_WRITE )
                PUBLIC cHome := cPath
                PUBLIC iDLL, lfHndl
                PUBLIC nConnect := AdsIsServerLoaded( cPath ), lCFXloyalty := .f.
 
                FWrite( fhnd, TIME() + "
Startup" + hb_eol() )
 
                REQUEST DBFCDX
rddRegister( "
DBFCDX", 1 )
                rddsetdefault( "
DBFCDX" )
 
                SET DELETED ON
                SET EXCLUSIVE OFF
                SET DEFAULT TO ( cPath )
                FWrite( fhnd, TIME() + "
RDD Initialized" + hb_eol() )
                FWrite( fhnd, TIME() + "
Path: " + cPath + hb_eol() )
 
                // Load the DLL
                iDLL := LoadLibrary( "
wininet.dll" )
 
               FWrite( fhnd, TIME() + "
wininet.dll is loaded " + hb_eol() )
 
               DO WHILE  .t. //  win_serviceGetStatus() == WIN_SERVICE_RUNNING
 
FWrite( fhnd, TIME() + "
Work in progress " + hb_eol() )
 
/* This is where you add in your operations.  You can call Functions from here
Remember, a service can have NO screen interaction or prompts.  You will find
Many FWH calls won’t be useable in a service.  */
 
FWrite( fhnd, TIME() + "
Cycle Completed" + hb_eol() )
               
                hb_idleSleep( 10000 )   // 3 hours
 
ENDDO
 
//      Close the log file
                FWrite( fhnd, TIME() + "
Exiting..." + hb_eol() )
                Close( fhnd )
 
// Close the library
FreeLibrary( iDLL )
 
RETURN
 


Remember, you DO NOT use any screen calls or operations ( or at least not in my experience ).
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2944
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Create a windows service

Postby leandro » Tue Apr 11, 2023 6:52 pm

Tim, muchas gracias por la respuesta, nuestro amigo Cristóbal tambien nos envió el mismo código, pero el problema es que no funciona con xharbour, solo con harbour :(
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1669
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Create a windows service

Postby TimStone » Tue Apr 11, 2023 7:48 pm

In the past I used xHarbour Builder ( many years ago ). I have not used the current xHarbour.

Perhaps you could install Harbour and build your service with it. Several functions in here are necessary for my build but you might not need them. For example, ADS functions.
Tim Stone
http://www.MasterLinkSoftware.com
http://www.autoshopwriter.com
timstone@masterlinksoftware.com
Using: FWH 23.10 with Harbour 3.2.0 / Microsoft Visual Studio Community 2022-24 32/64 bit
User avatar
TimStone
 
Posts: 2944
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 53 guests