简体   繁体   中英

how to start a program using a windows service?

I created a windows service in c++ using visual studios and now I want the service to run an exe file. The service is set to start every time the computer starts

i know i need to use code to locate the path of the exe like C:\\MyDirectory\\MyFile.exe but how to I actually run the file from the service?

i read about the process start method here but i am not sure how to use it

You can use createprocess function in your service to run an exe.

TCHAR* path = L"C:\\MyDirectory\\MyFile.exe";

STARTUPINFO info;
PROCESS_INFORMATION processInfo;

ZeroMemory( &info, sizeof(info) );
info.cb = sizeof(info);
ZeroMemory( &processInfo, sizeof(processInfo) );


if (CreateProcess(path, NULL, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
    ::WaitForSingleObject(processInfo.hProcess, INFINITE);
    CloseHandle(processInfo.hProcess);
    CloseHandle(processInfo.hThread);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM