user2295419
user2295419

Reputation: 29

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

Upvotes: 2

Views: 4253

Answers (1)

shivakumar
shivakumar

Reputation: 3397

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);
}

Upvotes: 1

Related Questions