Another C program example starting a Windows service programmatically with Win32 library

 

// For WinXp as a target

#define _WIN32_WINNT 0x0501

#include <Windows.h>

#include <stdio.h>

#include <tchar.h>

 

#define SCHED_CLASS TEXT("SAGEWINDOWCLASS")

#define SCHED_TITLE TEXT("SYSTEM AGENT COM WINDOW")

#define SCHED_SERVICE_APP_NAME TEXT("mstask.exe")

#define SCHED_SERVICE_NAME TEXT("Schedule")

 

int main(int argc, char **argv)

{

STARTUPINFO sui;

PROCESS_INFORMATION pi;

TCHAR szApp[MAX_PATH];

SERVICE_STATUS SvcStatus;

SC_HANDLE hSC = NULL;

SC_HANDLE hSchSvc = NULL;

LPTSTR pszPath;

OSVERSIONINFO osver;

DWORD dwRet;

BOOL fRet;

 

osver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);

ZeroMemory(&sui, sizeof(sui));

sui.cb = sizeof (STARTUPINFO);

 

// Determine which version of OS you are running.

GetVersionEx(&osver);

 

if (osver.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)

{

// If Windows 95, check to see if Windows 95 version of Task Scheduler is running.

HWND hwnd = FindWindow(SCHED_CLASS, SCHED_TITLE);

if (hwnd != NULL)

{

// It is already running.

printf("Task Scheduler is already running.\n");

return 0;

}

 

// Execute the task scheduler process.

dwRet = SearchPath(NULL,

SCHED_SERVICE_APP_NAME,

NULL,

MAX_PATH,

szApp,

&pszPath);

 

if (dwRet == 0)

{ return GetLastError(); }

else

printf("SearchPath() is OK.\n");

 

fRet = CreateProcess(szApp,

NULL,

NULL,

NULL,

FALSE,

CREATE_NEW_CONSOLE | CREATE_NEW_PROCESS_GROUP,

NULL,

NULL,

&sui,

&pi);

 

if (fRet == 0)

{

return GetLastError();

}

else

printf("CreateProcess() is OK.\n");

 

CloseHandle(pi.hProcess);

CloseHandle(pi.hThread);

return S_OK;

}

else

{

// If not Windows 95, check to see if Windows NT version of Task Scheduler is running.

hSC = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);

if (hSC == NULL)

{

return GetLastError();

}

else

printf("OpenSCManager() is OK.\n");

 

hSchSvc = OpenService(hSC, SCHED_SERVICE_NAME, SERVICE_START | SERVICE_QUERY_STATUS);

 

CloseServiceHandle(hSC);

if (hSchSvc == NULL)

{

return GetLastError();

}

else

printf("OpenService() is OK.\n");

 

if (QueryServiceStatus(hSchSvc, &SvcStatus) == FALSE)

{

CloseServiceHandle(hSchSvc);

return GetLastError();

}

else

printf("QueryServiceStatus() is OK.\n");

 

if (SvcStatus.dwCurrentState == SERVICE_RUNNING)

{

// The service is already running.

CloseServiceHandle(hSchSvc);

printf("Task Scheduler is already running.\n");

return 0;

}

if (StartService(hSchSvc, 0, NULL) == FALSE)

{

CloseServiceHandle(hSchSvc);

printf("Could not start Task Scheduler.\n");

return GetLastError();

}

CloseServiceHandle(hSchSvc);

printf("Task Scheduler has been started.\n");

return S_OK;

}

}

 

Output example:  (when the Task Scheduler is not running yet):

 

OpenSCManager() is OK.

OpenService() is OK.

QueryServiceStatus() is OK.

Task Scheduler has been started.

Press any key to continue . . .

 

 

Output example: (when the Task Scheduler is already running):

 

OpenSCManager() is OK.

OpenService() is OK.

QueryServiceStatus() is OK.

Task Scheduler is already running.

Press any key to continue . . .

 

 

Verify that the service has been started.

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Target platform: none, just for learning and fun

Header file: Standard and Windows

Additional library: Windows Platform SDK

Additional project setting: Set project to be compiled as C. Click Project menu->your_project_name Properties->Configuration Properties->C/C++->Advanced->Compiled As: Compiled as C Code (/TC)

Other info: non-CLR or unmanaged. Need to add Advapi32.lib (Advapi32.dll) to the project. Click the Project menu->Select the your_project_name Properties... sub menu->Expand the Configuration Properties folder on the left pane->Expand the Linker subfolder->Select the Input subfolder->Select the Additional Dependencies field on the right pane->Click the ... at the end of the field->Type in 'Advapi32.lib' in the empty pane->Click the OK button->Click the OK button second time to close the project Properties dialog.

To do: Another example on how to start the Windows service programmatically using C code of the Win32 library

To show: The Windows process C Win32 functions usage for Windows system programming

 

 

C and C++ Programming Resources | C & C++ Code Example Index