Reputation: 168
I have been tasked to create a Windows Service DLL to be hosted by SvcHost.exe using the g++ compiler. So far, I've set up the service, created a DLL with ServiceMain, added "ServiceDLL" into the SYSTEM registry, and added "(group name)" into the SOFTWARE registry.
I used the documentation found at Microsoft - Writing a ServiceMain Function and have followed every rabbit hole in the MSDN catalog. The ONE thing that I have not done is download Visual Studio.
Using other services as a comparison, it seems that my creation of a service is correct and the registry is correct. My code should be correct... so the only thing that I didn't follow the instruction are is the compilation method.
Disclaimer: I know that Microsoft has suggested not to use SvcHost.exe but I am not in charge of the project or it's design; I am just a peon.
Currently, I am doing the following to compile...
c:\> g++ -c svchostdemo.cpp
c:\> g++ -shared -o SvcHostDemo.dll svchostdemo.o
And I did the following to create the service...
c:\> sc create SvcHostDemo binpath= "%SystemRoot%\System32\svchost.exe -k demo" type= share
The following keys have been added/modified...
HKLM\SYSTEM\CurrentControlSet\services\SvcHostDemo\Parameters\ServiceDLL = %SystemRoot%\System32\SvcHostDemo.dll
HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Svchost\demo = SvcHostDemo
This is what I get when I start the service...
C:\Windows>sc start SvcHostDemo
SERVICE_NAME: SvcHostDemo
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 2 START_PENDING
(NOT_STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
WIN32_EXIT_CODE : 0 (0x0)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x7d0
PID : 6844
FLAGS :
This is what I get when I query the service...
C:\Windows>sc query SvcHostDemo
SERVICE_NAME: SvcHostDemo
TYPE : 20 WIN32_SHARE_PROCESS
STATE : 1 STOPPED
WIN32_EXIT_CODE : 193 (0xc1)
SERVICE_EXIT_CODE : 0 (0x0)
CHECKPOINT : 0x0
WAIT_HINT : 0x0
The DLL Code...
#include <windows.h>
#define SVCNAME TEXT("SvcHostDemo")
SERVICE_STATUS gSvcStatus;
SERVICE_STATUS_HANDLE gSvcStatusHandle;
HANDLE ghSvcStopEvent = NULL;
LPHANDLER_FUNCTION SvcCtrlHandler;
VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv);
VOID ReportSvcStatus( DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint);
//
// Purpose:
// Entry point for the service
//
// Parameters:
// dwArgc - Number of arguments in the lpszArgv array
// lpszArgv - Array of strings. The first string is the name of
// the service and subsequent strings are passed by the process
// that called the StartService function to start the service.
//
// Return value:
// None.
//
VOID WINAPI ServiceMain( DWORD dwArgc, LPTSTR *lpszArgv )
{
// Register the handler function for the service
gSvcStatusHandle = RegisterServiceCtrlHandler(
SVCNAME,
SvcCtrlHandler);
if( !gSvcStatusHandle )
{
MessageBox(NULL, "FAIL", "FAIL", MB_OK | MB_ICONQUESTION);
//SvcReportEvent(TEXT("RegisterServiceCtrlHandler"));
return;
}
MessageBox(NULL, "PASS", "PASS", MB_OK | MB_ICONQUESTION);
// These SERVICE_STATUS members remain as set here
gSvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
gSvcStatus.dwServiceSpecificExitCode = 0;
// Report initial status to the SCM
ReportSvcStatus( SERVICE_START_PENDING, NO_ERROR, 3000 );
// Perform service-specific initialization and work.
SvcInit( dwArgc, lpszArgv );
}
//
// Purpose:
// The service code
//
// Parameters:
// dwArgc - Number of arguments in the lpszArgv array
// lpszArgv - Array of strings. The first string is the name of
// the service and subsequent strings are passed by the process
// that called the StartService function to start the service.
//
// Return value:
// None
//
VOID SvcInit( DWORD dwArgc, LPTSTR *lpszArgv)
{
// TO_DO: Declare and set any required variables.
// Be sure to periodically call ReportSvcStatus() with
// SERVICE_START_PENDING. If initialization fails, call
// ReportSvcStatus with SERVICE_STOPPED.
// Create an event. The control handler function, SvcCtrlHandler,
// signals this event when it receives the stop control code.
ghSvcStopEvent = CreateEvent(
NULL, // default security attributes
TRUE, // manual reset event
FALSE, // not signaled
NULL); // no name
if ( ghSvcStopEvent == NULL)
{
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
return;
}
// Report running status when initialization is complete.
ReportSvcStatus( SERVICE_RUNNING, NO_ERROR, 0 );
// TO_DO: Perform work until service stops.
while(1)
{
// Check whether to stop the service.
WaitForSingleObject(ghSvcStopEvent, INFINITE);
ReportSvcStatus( SERVICE_STOPPED, NO_ERROR, 0 );
return;
}
}
//
// Purpose:
// Sets the current service status and reports it to the SCM.
//
// Parameters:
// dwCurrentState - The current state (see SERVICE_STATUS)
// dwWin32ExitCode - The system error code
// dwWaitHint - Estimated time for pending operation,
// in milliseconds
//
// Return value:
// None
//
VOID ReportSvcStatus( DWORD dwCurrentState,
DWORD dwWin32ExitCode,
DWORD dwWaitHint)
{
static DWORD dwCheckPoint = 1;
// Fill in the SERVICE_STATUS structure.
gSvcStatus.dwCurrentState = dwCurrentState;
gSvcStatus.dwWin32ExitCode = dwWin32ExitCode;
gSvcStatus.dwWaitHint = dwWaitHint;
if (dwCurrentState == SERVICE_START_PENDING)
gSvcStatus.dwControlsAccepted = 0;
else gSvcStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
if ( (dwCurrentState == SERVICE_RUNNING) ||
(dwCurrentState == SERVICE_STOPPED) )
gSvcStatus.dwCheckPoint = 0;
else gSvcStatus.dwCheckPoint = dwCheckPoint++;
// Report the status of the service to the SCM.
SetServiceStatus( gSvcStatusHandle, &gSvcStatus );
}
// TO COMPILE:
// c:\> g++ -c svchostdemo.cpp
// c:\> g++ -shared -o SvcHostDemo.dll svchostdemo.o
Upvotes: 0
Views: 1029