Q:

Calling a batch file from a windows service

I have a service that needs to call a batch when a new file is copied to a directory. I tried using CreateProcess, ShellExecute, ShellExecuteEx and even system. I tried calling the batch directly, then calling cmd.exe and passing /c batchfile as parameter... Nothing.
I set the service to run as administrator. I tried with other users. Nothing.
This is a Windows 2008 server.
Any ideas?
Thank you.

A:

CreateProcess, as shown in the (grossly simplified) example below, is a valid way to execute a batch file from a service.
STARTUPINFO si = { 0 };
PROCESS_INFORMATION pi = { 0 };
si.cb = sizeof(si);

if( !CreateProcessA( NULL,
                     "C:\\test.bat",
                     NULL,
                     NULL,
                     FALSE,
                     0,
                     NULL,
                     NULL,
                     &si,
                     &pi
                   ) )
{
    char msg[100];
    sprintf( msg, "CreateProcess() failed: %d", GetLastError() );
    OutputDebugStringA( msg );
}

Logging is key. If the batch file isn't being executed, CreateProcess() will tell you why.
You mention that the batch file is to be executed "when a new file is copied to a directory." Have you verified that the detection code is working properly? Is the code attempting to execute the batch file actually reached?
More context would definitely be helpful. Please post the relevant portions of the batch file and service code.

