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>
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.