FWH as a Service

FWH as a Service

Postby TimStone » Tue Dec 01, 2015 8:48 pm

I will try again.

Is anyone using a FWH / xHarbour application as a service ? I created one, and the application will run properly if started manually. I also can install it as a service. However, when I try to start it as a service, I get a timeout error saying it did not respond. There is plenty of time for it to respond prior to the message.

So, I've asked two different ways. Maybe this time I can get a response. IS ANYONE RUNNING A FW APP AS A SERViCE ? If so, can you provide a sample code so I can compare and see what might be wrong with what I'm doing. If I can get this working, it resolves many issues.

Thank You !
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: 2909
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: FWH as a Service

Postby Antonio Linares » Wed Dec 02, 2015 8:06 am

Tim,

Could you explain how have you installed it as a service ?

So I can test it here, thanks
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41410
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: FWH as a Service

Postby TimStone » Wed Dec 02, 2015 4:51 pm

Antonio,

I am using Actual Installer for my full application installation. It works well. You can direct it to use the SC command native to Windows ( which you will find in the system folder ). Using that tool you can install / register a service. Then, using the windows Services tool you can see it and start it.

You can use this link to review how to use the SC command. You can then use it from the WIndows Command Prompt to actually install a service.

https://technet.microsoft.com/en-us/library/bb490995.aspx

I use the Create command. Here is a sample:

sc create "TestService" binPath= "C:\TestDir>\YourApp.exe" DisplayName= "TestService" start= auto

Please note the = is attached to the parameter but must be followed by a space before stating the value.



I have seen several people refer to using apps as Services so I thought sure some would respond here.

Tim
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: 2909
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: FWH as a Service

Postby Antonio Linares » Wed Dec 02, 2015 7:05 pm

Tim,

thanks,

Why do you need your app as a service ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41410
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: FWH as a Service

Postby TimStone » Wed Dec 02, 2015 8:39 pm

Antonio,

The app is my automatic update service, plus it does a daily unattended backup of all data files, and submits data to 3rd party vendors who contract with my client. It resides on the server which may be an actual WinServer computer, or just a regular Windows machine that shares data with the connected computers.

Previously I have installed the app so it will begin in the Startup folder. Reboot, and it starts back up. It is not a problem on a regular windows OS. However, when a Microsoft Server system reboots ( as in an Windows update ), programs in the Startup folder do not run until a user actually logs on to the computer. In some cases, this doesn't happen for weeks. However, a service will start on reboot and does not wait for a login.

So, it is causing problems doing it in the Startup folder in some installations.

The program itself has no problem doing the work when it's running.

Tim
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: 2909
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: FWH as a Service

Postby Antonio Linares » Wed Dec 02, 2015 11:08 pm

Tim,

I have just tried:

sc create "FiveTech" binPath= "C:\fwh\samples\tutor01.exe" DisplayName= "TestService" start= auto
[SC] CreateService SUCCESS

and opened the task manager. The service looked as stopped, then right clicked and selected start but it says
it is unable to start service.

What to do to get tutor01.exe working ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41410
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: FWH as a Service

Postby TimStone » Wed Dec 02, 2015 11:23 pm

Antonio,

That is my question. The exact error message says that it timed out waiting for the service to start ( respond ). Apparently when we start a service, it needs to issue a response to signal it has started.

There are several people who have posted on this forum about using applications as a service. Surely someone has information that will help us understand what we need to do. I'm sure many people could use this capability in their own installations.

Tim
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: 2909
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: FWH as a Service

Postby Antonio Linares » Thu Dec 03, 2015 9:37 am

Tim,

It seems as we have to do several calls to Windows API functions:

Here there is a C language example that we need to find how to implement in FWH:
http://stackoverflow.com/questions/8972416/how-to-correctly-use-setservicestatus-to-tell-windows-my-service-is-stopping

We have to call:

StartServiceCtrlDispatcher()
RegisterServiceCtrlHandlerEx()
SetServiceStatus()

and we have to provide several callbacks
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41410
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: FWH as a Service

Postby TimStone » Thu Dec 03, 2015 4:46 pm

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: 2909
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: FWH as a Service

Postby Antonio Linares » Thu Dec 03, 2015 4:59 pm

Tim,

I have a created a service.c file this way. Its not finished yet, the idea is to test it
and see how far we get:

service.c
Code: Select all  Expand view
#include <windows.h>
#include <hbapi.h>

#define MY_SVC_NAME "My service"
#define THE_PROG "\"C:\\Program Files\\My software\\bin\\The Prog.exe\""
#define SLEEP_TIME 300000

static SERVICE_STATUS ServiceStatus;
static SERVICE_STATUS_HANDLE hStatus;

void WINAPI ServiceMain(DWORD argc, LPSTR argv);
void WINAPI ControlHandler(DWORD request);
void InitService();
   
static int cont_running = 1;

DWORD WINAPI ServiceHandlerProc(DWORD ControlCode, DWORD a, void *b, void *c)
{
  switch (ControlCode) {
  case SERVICE_CONTROL_STOP :
    cont_running = 0;
    ServiceStatus.dwCheckPoint=0;
    ServiceStatus.dwWin32ExitCode = 0;
    ServiceStatus.dwCurrentState  = SERVICE_STOP_PENDING;
    ServiceStatus.dwWaitHint =2000;
    SetServiceStatus (hStatus, &ServiceStatus);
    Sleep(1000);
    ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
    SetServiceStatus (hStatus, &ServiceStatus);
    break;
  case SERVICE_CONTROL_SHUTDOWN :
    cont_running = 0;
    ServiceStatus.dwCheckPoint=0;
    ServiceStatus.dwWin32ExitCode = 0;
    ServiceStatus.dwCurrentState  = SERVICE_STOP_PENDING;
    ServiceStatus.dwWaitHint =2000;
    SetServiceStatus (hStatus, &ServiceStatus);
    Sleep(1000);
    ServiceStatus.dwCurrentState  = SERVICE_STOPPED;
    SetServiceStatus (hStatus, &ServiceStatus);
    break;
  }

  return 0;

}

static void InitService( void )
{
  cont_running=1;
  do {
    // system(THE_PROG);
    Sleep(SLEEP_TIME);
  } while (cont_running);
}

void WINAPI ServiceMain( DWORD argc, LPSTR argv )
{
  SERVICE_STATUS_HANDLE hServiceStatus;

  ServiceStatus.dwServiceType        = SERVICE_WIN32;
  ServiceStatus.dwCurrentState       = SERVICE_START_PENDING;
  ServiceStatus.dwControlsAccepted   = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
  ServiceStatus.dwWin32ExitCode      = 0;
  ServiceStatus.dwServiceSpecificExitCode = 0;
  ServiceStatus.dwCheckPoint         = 0;
  ServiceStatus.dwWaitHint           = 0;

  hServiceStatus = RegisterServiceCtrlHandlerEx( MY_SVC_NAME, ServiceHandlerProc, 0 );

  Sleep( 1000 );

  ServiceStatus.dwCheckPoint = 0;
  ServiceStatus.dwWaitHint = 0;
  ServiceStatus.dwCurrentState = SERVICE_RUNNING;
  SetServiceStatus( hServiceStatus, &ServiceStatus );

  // InitService();

  MessageBeep( -1 );
  MessageBeep( -1 );
  MessageBeep( -1 );

  return;
}

HB_FUNC( STARTSERVICECTRLDISPATCHER ) // cServiceName --> nError
{
   SERVICE_TABLE_ENTRY ServiceStartTable[ 2 ];
 
   ServiceStartTable[ 0 ].lpServiceName = MY_SVC_NAME;
   ServiceStartTable[ 0 ].lpServiceProc = ( LPSERVICE_MAIN_FUNCTION ) ServiceMain;
   ServiceStartTable[ 1 ].lpServiceName = NULL;
   ServiceStartTable[ 1 ].lpServiceProc = NULL;

   StartServiceCtrlDispatcher( ServiceStartTable );

   hb_retni( GetLastError() );
}


To test it, I use this example:
Code: Select all  Expand view
#include "FiveWin.ch"

//----------------------------------------------------------------------------//

function Main()

   StartServiceCtrlDispatcher()

   MsgInfo( "ok" )

return nil


At least we can start the service now, but it does nothing yet, not even beeps (see the calls to MessageBeep() from C code)
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41410
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: FWH as a Service

Postby Antonio Linares » Thu Dec 03, 2015 5:05 pm

Tim,

I did not remember that Harbour provides an API for services, so we don't need to implement them.

Those functions are included in Harbour hbwin.lib so you can directly use them.

Have you tried them ?
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41410
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: FWH as a Service

Postby TimStone » Thu Dec 03, 2015 5:17 pm

No I haven't. Sadly Harbour documentation is severely lacking. Also, I had built that .exe with xHarbour.

I had understood that using the sc.exe command would allow almost any .exe file to run as a service. Once installed, it was supposed to be able to run. The threads I found on this site were about installing the services, so that is why I had not paid much attention to them.
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: 2909
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: FWH as a Service

Postby Antonio Linares » Thu Dec 03, 2015 6:07 pm

Tim,

We must use those Harbour functions or it will not run
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41410
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: FWH as a Service

Postby TimStone » Thu Dec 03, 2015 6:39 pm

OK ... I can create a build with Harbour and MSVC ... but I need to know the functions. Did you create a sample that works ?
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: 2909
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: FWH as a Service

Postby Gale FORd » Thu Dec 03, 2015 9:22 pm

Check out this sample. Does not use FW but may help.
https://groups.google.com/d/msg/harbour-users/MplGdJQEZlY/qETsKCzQLgAJ
Gale FORd
 
Posts: 663
Joined: Mon Dec 05, 2005 11:22 pm
Location: Houston

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: No registered users and 41 guests