Calling a batch file from a windows service

后端 未结 3 1255
孤街浪徒
孤街浪徒 2021-01-26 04:23

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

3条回答
  •  甜味超标
    2021-01-26 04:43

    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.

提交回复
热议问题