#1 2020-12-17 21:38:22

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

Error 6 - The handle is invalid when trying to start services

I've created a console program which is a HTTP server - OK
I've created a daemon program that uses the same classes from console program - OK
I can run the daemon using /console and it works - OK

However, if I try to /start the daemon, it "starts" but I got an error on log:

20201217 21340931  !  +    ServDaemonCore.TDaemonService(026fb3e8).Stop
20201217 21340931  !  -    00.000.000
20201217 21340933  ! EXC   EOleException 80040e4d ("Especificação de autorização inválida") [] at 4fc65e  stack trace API 46ae31 404de8 77a171b4 77a03b36 4fc65e 407977 4e34b4 70b9ac 712bb1 713082 4d3c99 4d3601 760d1946 75f5fa29 779f75f4 779f75c4 

It is in Portuguese... but debugging the original error is "Error 6 - The handle is invalid when trying to start services".

Running on Windows PRO; user is Admin; it's not antivirus.

Any tips?

Offline

#2 2020-12-17 21:44:36

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

Re: Error 6 - The handle is invalid when trying to start services

Here you can see the original error, debugging mORMot:
error.jpg

Offline

#3 2020-12-17 22:02:38

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Error 6 - The handle is invalid when trying to start services

Was the service at least installed?

The error here is indicative of its state, it is a GetLastError.
But the root cause is not this.
Use the debugger to find below when it was raised.

Offline

#4 2020-12-18 01:44:49

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

Re: Error 6 - The handle is invalid when trying to start services

Yes, I can install without any problem.
If I use "/start", it seems that it's started (I can see on Windows Services that status was changed) doing nothing, as it was a fake start.

Debuggint I can see below that:
1. FHandle=0
2. backupError=5 — I'm not sure why 'OpenSCManager(''%'',''%'') for [%]' wasn't register on the log file, as I'm using "*", though.

constructor TServiceController.CreateOpenService(const TargetComputer,
  DataBaseName, Name: String; DesiredAccess: DWORD);
var backupError: cardinal;
begin
  inherited Create;
  StringToUTF8(Name,FName);
  FSCHandle := OpenSCManager(pointer(TargetComputer), pointer(DatabaseName),
    GENERIC_READ);
  if FSCHandle = 0 then begin
    backupError := GetLastError;
    ServiceLog.Add.Log(sllLastError,'OpenSCManager(''%'',''%'') for [%]',
      [TargetComputer,DatabaseName,FName]);
    SetLastError(backupError);
    Exit;
  end;
  FHandle := OpenService(FSCHandle, pointer(Name), DesiredAccess);
  if FHandle=0 then begin
    backupError := GetLastError;
    ServiceLog.Add.Log(sllLastError,'OpenService("%")',[Name]);
    SetLastError(backupError);
  end;
end;

After that, returning to the TSynDaemon.CommandLine() the function Show() is going to be executed:

          cStart:
            Show(ctrl.Start([]));

Than, the last error changes for "6", after execution of StartService():

function StartService(hService: SC_HANDLE; dwNumServiceArgs: DWORD;
  lpServiceArgVectors: Pointer): BOOL; stdcall; external advapi32
  name 'StartService'+{$ifdef UNICODE}'W'{$else}'A'{$endif};

Offline

#5 2020-12-18 04:12:14

Chaa
Member
Registered: 2011-03-26
Posts: 244

Re: Error 6 - The handle is invalid when trying to start services

Your OpenSCManager failed with ERROR_ACCESS_DENIED.
I think you must use "Run as Administrator".

Offline

#6 2020-12-18 08:16:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Error 6 - The handle is invalid when trying to start services

So /install works but /start fails?

If this is the case, then the service may be half-installed.
IT happens when a service installation or execution was broken, due e.g. to the executable still running when uninstalling.
Unstall it, restart the PC, ensure it is actually uninstalled, then reinstall it.

Offline

#7 2020-12-18 12:40:49

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

Re: Error 6 - The handle is invalid when trying to start services

Chaa wrote:

Your OpenSCManager failed with ERROR_ACCESS_DENIED.
I think you must use "Run as Administrator".

I use from the beginning...

ab wrote:

So /install works but /start fails?

If this is the case, then the service may be half-installed.
IT happens when a service installation or execution was broken, due e.g. to the executable still running when uninstalling.
Unstall it, restart the PC, ensure it is actually uninstalled, then reinstall it.

Yes, /install works but /start fails.
I did what you said and I got the same error.
On log, I have 2 Stop() calls when I tried to execute /install for the first time, after reboot.

Offline

#8 2020-12-21 12:11:03

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

Re: Error 6 - The handle is invalid when trying to start services

Guys, please, any more ideias?

Offline

#9 2020-12-21 12:40:33

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 209

Re: Error 6 - The handle is invalid when trying to start services

Can you post a minimal working example?

Offline

#10 2020-12-21 13:41:29

mdbs99
Member
From: Rio de Janeiro, Brazil
Registered: 2018-01-20
Posts: 132
Website

Re: Error 6 - The handle is invalid when trying to start services

Original code is complex (and I cannot post here). I will make an simple example without any access do DB just to know if this issue is something related to privilege to install—even though I'm using Admin, but the machine is not mine. It's part of a network with its own rules, antivirus, AD, etc.

If it works, it means that has something wrong in original code... however, it works perfectly on console mode.

Offline

#11 2020-12-21 13:55:32

pvn0
Member
From: Slovenia
Registered: 2018-02-12
Posts: 209

Re: Error 6 - The handle is invalid when trying to start services

I'm not asking you to post your code, just create a fresh dummy service that has no logic code and see if it starts on your system.

Offline

#12 2020-12-21 17:03:42

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,182
Website

Re: Error 6 - The handle is invalid when trying to start services

There may be some troubles about Windows service execution.
For instance, execution user rights, unmounted folder, and so on...
The service execution context is pretty specific, and don't match the app execution itself.

Try to move the service into another folder.

Offline

#13 2020-12-21 17:22:39

macfly
Member
From: Brasil
Registered: 2016-08-20
Posts: 374

Re: Error 6 - The handle is invalid when trying to start services

I don't use this feature on Windows, but I use Daemon on Linux.

I had problems, for obvious reasons, when using relative paths in the code (settings files), por example path of db.

The forked application could not find the files, because it was running in another context.

Offline

Board footer

Powered by FluxBB