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.

urifAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
simply :

        system("script.bat");

??
0
evilrixSenior Software Engineer (Avast)Commented:
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
0
evilrixSenior Software Engineer (Avast)Commented:
^^^ obviously xxx.bat should be substituted with the name of your script file.
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

evilrixSenior Software Engineer (Avast)Commented:
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

0
urifAuthor Commented:
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.
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
Infinity08Commented:
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.
0
urifAuthor Commented:
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
0
Infinity08Commented:
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
0
urifAuthor Commented:
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.
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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?
0
urifAuthor Commented:
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...

0
evilrixSenior Software Engineer (Avast)Commented:
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.
0
urifAuthor Commented:
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...
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
evilrixSenior Software Engineer (Avast)Commented:
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.
0
evilrixSenior Software Engineer (Avast)Commented:
Also, I wonder if this article might help you diagnose the issue...
http://www.windowsitpro.com/Article/ArticleID/22888/22888.html
0
urifAuthor Commented:
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...
0
evilrixSenior Software Engineer (Avast)Commented:
I also discovered this, what seems to apply to your problem.
http://www.experts-exchange.com/Programming/Languages/Scripting/Q_22524877.html
0
urifAuthor Commented:
you mean opening a copy of itself in another thread?
i miss fork() in windows....
0
urifAuthor Commented:
thanks, i saw that post when i googled for possible answers....
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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?
0
evilrixSenior Software Engineer (Avast)Commented:
>> you mean opening a copy of itself in another thread?
No I mean run itself as a new process but with a command line switch that tells it to run the batch file and terminate... so it runs as a normal exe and not as a service. If the service is currently an exe this is a viable solution (although a bit of a kludge).
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
urifAuthor Commented:
hmmm, i am running it as administrator...
0
urifAuthor Commented:
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
0
evilrixSenior Software Engineer (Avast)Commented:
>> 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 :)
0
evilrixSenior Software Engineer (Avast)Commented:
Hi urif,

How did you get on with this?

-Rx.
0
urifAuthor Commented:
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
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.