Link to home
Start Free TrialLog in
Avatar of urif
urif

asked on

running a batch file from a windows service

i have a service that needs to run once in a while a batch file.
i tried winexec(), shellexecute(), shellexecuteex(), system() and createprocess(). they all return success but the batch file never gets executed.
yes, the service runs as local and it's set to SERVICE_INTERACTIVE_PROCESS.
and yes, the account is administrator

question: how to run a simple batch file from a service.

thanks
code is appreciated.

Avatar of Infinity08
Infinity08
Flag of Belgium image

simply :

        system("script.bat");

??
I suspect the problem is that a .bat file is not actually an executable but; rather, a batch file executed by the shell. Try passing the following to WinExec():

cmd.exe /c xxx.bat
^^^ obviously xxx.bat should be substituted with the name of your script file.
Ok, I finally got a chance to try this myself and I can confirm the following will open a bat file when just run from the command line.
This is the batch file
^^^^^^^^^^^^^^^^^^^^^^
@echo off
echo "hello world"
pause
 
This is the C++ code
^^^^^^^^^^^^^^^^^^^^^^
#include <windows.h>
 
int main()
{
	WinExec("c:\\temp\\test.bat", 0);
	return 0;
}

Open in new window

Avatar of urif
urif

ASKER

ok, thanks for the answers, but as i stated, i tried all of them before, it's not a nomal command line, it's a windows service (as stated in the question)
i tried all of the variations, including cmd /c and they do NOT work.
thanks for trying.

by the way, my same code ran from a  normal command line program will work with no problems, ONLY when it's ran from a service it won't run.
>> by the way, my same code ran from a  normal command line program will work with no problems, ONLY when it's ran from a service it won't run

Ah, well you didn't state that before :)

>> i stated, i tried all of them before, it's not a nomal command line

I had no way to test this running as a service so I could only suggest. Sorry I was unable to provide a working solution.

-Rx.
I think the problem might be that your service is not allowed to execute processes.

Got to Start > Run, and type services.msc, then click OK. This will open service manager.
Find your service in the list, then right-click on it and choose Properties.
On the Log On tab, check that "Allow service to interact with desktop" is checked. If not, check it, and restart the service.
Avatar of urif

ASKER

thanks inifinity08, but this is what i wrote in my question:

"yes, the service runs as local and it's set to SERVICE_INTERACTIVE_PROCESS."

that means that it starts with Allow service to interact with desktop" already activated
I seem to have missed that ... Sorry.

But allow me to continue this train of thought a bit further. Can you check this registry key :

        HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows

for the value of NoInteractiveServices ? If it has a value other than 0, then SERVICE_INTERACTIVE_PROCESS has no effect.

At the risk of giving you information that you already know, here's what MSDN has to say about interactive services :

        http://msdn2.microsoft.com/en-us/library/ms683502(VS.85).aspx
Avatar of urif

ASKER

thanks. i did in fact check the registry as well, the value is set correctly so no problem there.
i am puzzled everything is set so i don't understand why i can't run a simple batch file.
>> i don't understand why i can't run a simple batch file.
JOOI: Does it work if you run an executable rather than a batch file? If so could you not just re-write the batch file as an exe?
Avatar of urif

ASKER

it does work with an exe, however the batch file gets written dynamically by another application and i don't have access to the source of that application.
basically a new ever bat file is thrown every 40 min or so and the services comes alone, picks it and handles it.
only there is no "handles it" at the moment...

What about if your service runs and exe that runs the batch file? A little messy but it might resolve your immediate problem and give you some breathing space to figure out how to do it properly -- if, in fact, it is possible.
Avatar of urif

ASKER

sounds like a good idea and that is they way is being handle at the moment. however since the services needs to be installed remotely on several systems i was asked to find a solution that can bypass that exe.
so, it is a good workaround but i need to find a way to run that batch file.

how hard can it be?

i've been reading all over different forums and people seem to encounter this problem quite often, and so far no solutions. i've seen this reported on win2k, winXP and 2003.
if it was an MS thing i'll leave it alone as something that cannot be done, but i've also seen people posting in those forum threads that they encounter no problems running a batch file from a service


go figure....

i must be missing something...
>> how hard can it be?
I guess that very much depends on how easy it is to find the answer :) I did some searching on MSDN but could find nothing that I'd consider useful.

>>  must be missing something...
You and I both my friend.
Incidentally, there is not reason why your service can't be the same exe that runs the batch file. It can invoke itself with a specific command line param just to execute the batch file in a separate process -- if you see what I mean.
Also, I wonder if this article might help you diagnose the issue...
http://www.windowsitpro.com/Article/ArticleID/22888/22888.html
Avatar of urif

ASKER

ok, this will sound like an OS war thing, but i come from the UNIX world, i coded a lot there. in UNIX (Linux) we have daemons, pretty much what MS calls a service. now, a daemon can do anything a normal program does within the its user account, so if i run a daemon with my user account it could pretty much do anything i can do. including running programs, opening documents, etc.
everything is well documented, all the config files are pure ascii, and no complications.
MS however, everything is so closely guarded, config files are formatted, older versions of those formats are not valid on new versions of the same program... the registry is a bad idea, it gets corrupted and puff! everything is gone...

anyway, this is my frustration speaking...
Avatar of urif

ASKER

you mean opening a copy of itself in another thread?
i miss fork() in windows....
Avatar of urif

ASKER

thanks, i saw that post when i googled for possible answers....
>> but i come from the UNIX world
Me too :)

>> anyway, this is my frustration speaking...
Go for it, it sometimes helps to vent.

The problem with Windows (if you want to call it that) compared to Unix is that granting privileges to users is a whole lot more complex because, by and large, Windows allows finer granular control. Access to resources is controlled by ACLs (Access Control Lists) and what you can do with them is controlled by the users privileges. I presume your service is running under the LocalSystem account? The default set of privileges can be fund here: -

http://msdn2.microsoft.com/en-us/library/ms684190.aspx

It's probably one of these not being set properly that is the problem. Did you try running the service as a more privileged user (as suggested in the EE link above) to see if that resolved the issue?
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of urif

ASKER

hmmm, i am running it as administrator...
Avatar of urif

ASKER

ok, i'll give it a try... running the exe as another process... i'll keep you posted. it'll be tomorrow tho

thanks for the help
>> thanks for the help
You're welcome -- if you can call it that! To be honest I have no idea so I am just bouncing ideas now :)
Hi urif,

How did you get on with this?

-Rx.
Avatar of urif

ASKER

i still haven't figured it out, however this one was a good "close to" solutions so the points go to you.
thanks so much for the help