Windows Service - finding path

Windows Service - finding path

Postby TimStone » Fri Jul 22, 2022 11:05 pm

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

Re: Windows Service - finding path

Postby cnavarro » Fri Jul 22, 2022 11:48 pm

What do you get when you run
Code: Select all  Expand view
FWLOG hb_Arg(0)
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Windows Service - finding path

Postby TimStone » Fri Jul 22, 2022 11:55 pm

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

Re: Windows Service - finding path

Postby Jimmy » Sat Jul 23, 2022 12:16 am

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
greeting,
Jimmy
User avatar
Jimmy
 
Posts: 1725
Joined: Thu Sep 05, 2019 5:32 am
Location: Hamburg, Germany

Re: Windows Service - finding path

Postby cnavarro » Sat Jul 23, 2022 4:11 am

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

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


 
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6541
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Windows Service - finding path

Postby TimStone » Sat Jul 23, 2022 6:00 am

Thanks

I will try both on Sunday ... have to travel all day tomorrow.
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: 2943
Joined: Fri Oct 07, 2005 1:45 pm
Location: Trabuco Canyon, CA USA

Re: Windows Service - finding path

Postby TimStone » Sun Jul 24, 2022 3:16 am

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.
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: 2943
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: Google [Bot] and 50 guests