WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Run Winbatch as a Service

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Passing Parameters to a WinBatch Service


Question:

I have successfully created a windows 2000 service as per some of the examples provided. However, when I try to pass command-line parameters to that service they are not visible to the program. If I rename the .exs program to .exe, the parameters are visible but then svcSetAccept returns -1 indicating that is not running as a service.

Is there any way to pass parameters to the .exs service or do I need to use a .ini file or registry entries?

Answer:

How are you installing the service? Are you running a seperate WinBatch script to install the service? If so, when you look at the properties of the service (once it's installed) do the command line parameters look okay?

How are you capturing the command line parameters in the service script? The DebugTrace function and IntControl 1006 (returns the un-parsed WinBatch command line), should be very helpful in debugging your problem.

Add both functions to your script. Recompile as a .EXS file. Install the service. Then start it. Post the resulting trace file here.

User Reply:

I am creating the service using sc from the resource kit. Once created, you can see the parameters on the service properties page under "Path to executable":

C:\PS\css\cssTux.exs -PS_HOME=d:\ps84 -Appservers=CIS -Schedulers= -delay=2

Here is part of the trace for the .exs file:
************************************************************

*** Debug Initialized ***

==============================
Tue 4/20/2004 9:37:02 AM
WinBatch 32 2004A
WIL DLL 4.4add
C:\PS\css\cssTux.exs
Windows platform: NT, version: 5.0, build: 2195 (Service Pack 4)
==============================

rc=IntControl(1006, 0, 0, 0, 0)
(0) VALUE=> ""

logIt(logDir,strcat("Command line: ",rc))
(0) CALLING UDF (logit)
I created another service using sc that uses a .exe version of the program. Here's it's service properties "Path to executable":
C:\PS\css\cssTux.exe -PS_HOME=d:\ps84 -Appservers=CIS -Schedulers= -delay=2
Here is the trace for the .exe file:
************************************************************

*** Debug Initialized ***

==============================
Tue 4/20/2004 9:43:38 AM
WinBatch 32 2004A
WIL DLL 4.4add
C:\PS\css\cssTux.exe
Windows platform: NT, version: 5.0, build: 2195 (Service Pack 4)
==============================

rc=IntControl(1006, 0, 0, 0, 0)
(0) VALUE=> "-PS_HOME=d:\ps84 -Appservers=CIS -Schedulers= -delay=2"

logIt(logDir,strcat("Command line: ",rc))
(0) CALLING UDF (logit) 

Answer:

Quite often services do take their configuration information directly from the registry. IntControl(1009,...) was implemented based on an enhancement request that I made just for that purpose. Once your service knows the actual service name associated with itself [the actual registry key name], it can look up in the registry under its service database entry in the registry and find any values that it needs to take as configuration parameters.

I don't know if I've ever tried to have a service-mode compiled script accept command line parameters, but there is an option in the service configuration to enter "Start parameters". Are you entering the parameters through this field in the service properties?

User Reply:

The "start parameters" on the properties page in Win2K only apply to starting the service from that dialog. They disappear once the dialog is closed.

Answer:

Services get their startup parameters out of the registry. as services generally do not have command lines.

I did some digging around and found something that might be of use to you. This comes from a service-mode script that I wrote last year, and the script needed to be able to accept command line parameters when the service was automatically started.

; Get our command line. We are dealing with a native service program here, and the
; only way that we get command line parameters passed in to us when we get automatically
; started up at system boot time is if they are included as part of the binary file
; specification that is part of the service definition. We must call GetCommandLineA()
; in KERNEL32.DLL in order to obtain the full command line since a WinBatch script in
; native service mode only receives [in Param1 through Param9] the parameters that are
; manually supplied when a service is manually started. Another key thing to remember
; here is that the binary file specification must be a short 8.3 name since we cannot
; tolerate any long file names that include whitespace in them. We will parse the
; command line and find the instance # assigned to the current service.

CmdLine = ''

TempStrAddr = DllCall('KERNEL32.DLL',long:'GetCommandLineA')

TempByte = IntControl(32,TempStrAddr,'BYTE',0,0)

while(TempByte != 0)
CmdLine = StrCat(CmdLine,Num2Char(TempByte))
TempStrAddr = TempStrAddr + 1
TempByte = IntControl(32,TempStrAddr,'BYTE',0,0)
endwhile

TempPos = StrIndexNc(CmdLine,'/#=',0,@FWDSCAN)

if (TempPos == 0)
TempMsg = StrCat('Error! Run Mode = "',RunMode,'", should be "5". Aborting...')
Message(Title01,TempMsg)
IntControl(1000,ERR_INSTANCE_UNDEFINED,0,0,0)
exit
endif

SvcInstanceNum = Int(StrSub(CmdLine,TempPos+3,2))
SvcInstanceStr = StrFixLeft(SvcInstanceNum,'0',2)
This code was used before IntControl(1009,...) was available. In effect, the binary/executable file for the service was specified with some command line parameters following it. A Win32 API function call is made to retrieve the full command line that was used to start the service, and then the command line parameters were parsed out and used to figure out which service instance this was. The IntControl(1009,....) call now makes this code unnecessary as you can directly get the service's registry key name and from that you can directly interrogate the registry for configuration parameters that are specific to this particular instance of the service.
Article ID:   W16675
File Created: 2005:02:18:12:21:50
Last Updated: 2005:02:18:12:21:50