Skip to content

Run As Service

A C helper that calls RegisterServiceProcess to make the current process count as a Windows service, resolved dynamically from kernel32.

Published Sep 4, 1995

RegisterServiceProcess is the one-call trick that tells Windows "this process is now a service" - it flips the process's session type so Task Manager lists it under services and SCM can track it. This snippet shows the defensive way to call it: resolve the function pointer with LoadLibrary/GetProcAddress at runtime (it only exists on NT 4+), then invoke it with your service status handle (0 here for a test) and the service name. Useful when you are writing a lightweight daemon or want service-like semantics without implementing the full SvcMain control dispatcher.

#include <windows.h>
#include <stdio.h>

typedef DWORD (WINAPI* RegisterServiceProcess)(DWORD,DWORD);

void RunAsService(int xService)
{
	RegisterServiceProcess pReg = 0;
	HMODULE hmod = LoadLibrary("kernel32.dll");
	if(hmod)
	{
		pReg = (RegisterServiceProcess)GetProcAddress(hmod, "RegisterServiceProcess");
		if(pReg)pReg(0,xService);
		FreeLibrary(hmod);
	}
}

int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
	return 0;
}

The dynamic lookup matters: linking against RegisterServiceProcess directly breaks builds targeting older platforms where the export does not exist. For a real service you would pass the actual SERVICE_STATUS_HANDLE from your SvcMain instead of 0, and still implement the control handler - this call alone only changes how the system classifies the process.

An unhandled error has occurred. Reload 🗙