Page 1 of 1

Windows Service - finding path

PostPosted: Fri Jul 22, 2022 11:05 pm
by TimStone
I have a built a Windows Service program. It runs fine with one exception.

The service .exe resides in a folder on the server.
When the service is started, it knows the location of that file, and works correctly.

I need to write to a log file, so I set a path. I use. curdrv() and curdir() plus "\data\" to create the path.
So it should come back with F:\MLS12\Data\
However, with the service executing, it uses the following path:
Path: C:\WINDOWS\system32\Data\

How can I detect, within a windows service, the proper path ? If I hardcode the path, it works fine, but I can't do that because installs will be different at various locations.

Thanks.

Re: Windows Service - finding path

PostPosted: Fri Jul 22, 2022 11:48 pm
by cnavarro
What do you get when you run
Code: Select all  Expand view
FWLOG hb_Arg(0)

Re: Windows Service - finding path

PostPosted: Fri Jul 22, 2022 11:55 pm
by TimStone
I showed what the service is doing. Although the app is in a different drive and directory, once it runs it is doing it in c:\windows\system32. That is what the app is getting as s response. It is reporting that in the output log

I need it to know where the data files are kept which is a subdirectory of where the .exe resides.


Sent from my iPhone using Tapatalk

Re: Windows Service - finding path

PostPosted: Sat Jul 23, 2022 12:16 am
by Jimmy
hi,

what about
hb_DirBase() ➜ cDirName
returns the application's executable base directory (program statrtup directory).

https://github.com/Petewg/harbour-core/wiki/hb_D

Re: Windows Service - finding path

PostPosted: Sat Jul 23, 2022 4:11 am
by cnavarro
Tim, try with this
Code: Select all  Expand view

#include "fivewin.ch"

Function Main()

   local oWnd

   DEFINE WINDOW oWnd TITLE "Test"  FROM 10, 10 TO 410, 710 PIXEL

   ACTIVATE WINDOW oWnd ;
      ON INIT ( MsgInfo( cFilePath( PathRunService( "AnyDesk" ) ) ) )

Return nil

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

Function PathRunService( tcName, lTerminate )

   local oLocator
   local oWMI
   local oProcesses
   local oProcess
   local cPath        := ""
   
   DEFAULT lTerminate := .F.
   
   oLocator   := CREATEOBJECT( "WBEMScripting.SWBEMLocator" )
   oWMI       := oLocator:ConnectServer()
   oWMI:Security_:ImpersonationLevel := 3
   oProcesses = oWMI:ExecQuery([SELECT PathName FROM Win32_Service WHERE Name = '] + tcName + ['])

   if oProcesses:Count > 0
    for EACH oProcess in oProcesses
         cPath := oProcess:PathName
        if lTerminate
            oProcess:Terminate(0)
        endif
    Next
   endif

Return cPath

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


 

Re: Windows Service - finding path

PostPosted: Sat Jul 23, 2022 6:00 am
by TimStone
Thanks

I will try both on Sunday ... have to travel all day tomorrow.

Re: Windows Service - finding path

PostPosted: Sun Jul 24, 2022 3:16 am
by TimStone
I would like to thank both of you for your helpful responses.

The easier solution, using hb_DirBase() actually does exactly what I need. The service, when installed by the Installer software, knows where the file resides. That is in the same Root folder where the data directory is installed. Since the service calls for the DirBase(), once it receives that info, it knows the relevant path to the data folder where the log file is located, and also the relevant data files are that the service application needs to process "in the background".

It is actually nice to be able to build a service with Harbour/FWH, and have it run in the background regardless of who logs into the server. This is my auto backup, and auto program updater, utility. This will work nicely now.