Can anyone guide me on this step wise please,how to install this service

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I have the following files to make a service and have no idea how to create,install and run the service.Which file to add to a solution and which not

Memservice.c
// The first Windows Service application<br/>
#include <windows.h><br/>
#include <stdio.h><br/>
<br/>
// Make sure that the path is correct - that the folder exists<br/>
#define SLEEP_TIME 5000<br/>
#define LOGFILE "C:\MyServices\memstatus.txt"<br/>
<br/>
// Replace with wide-chars<br/>
int WriteToLog(char* str)<br/>
{<br/>
FILE* log;<br/>
log = fopen(LOGFILE, "a+");<br/>
if (log == NULL)<br/>
return -1;<br/>
fprintf(log, "%sn", str);<br/>
fclose(log);<br/>
return 0;<br/>
}<br/>
<br/>
<br/>
SERVICE_STATUS ServiceStatus;
<br/>
SERVICE_STATUS_HANDLE hStatus; <br/>
<br/>
void ServiceMain(int argc, char** argv); <br/>
void ControlHandler(DWORD request); <br/>
int InitService();<br/>
<br/>
void main() <br/>
{ <br/>
SERVICE_TABLE_ENTRY ServiceTable[2];<br/>
ServiceTable[0].lpServiceName = "MemoryStatus";<br/>
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;<br/>
<br/>
ServiceTable[1].lpServiceName = NULL;<br/>
ServiceTable[1].lpServiceProc = NULL;<br/>
// Start the control dispatcher thread for our service<br/>
StartServiceCtrlDispatcher(ServiceTable); <br/>
}<br/>
<br/>
// Service initialization<br/>
int InitService() <br/>
{ <br/>
int result;<br/>
result = WriteToLog("Monitoring started.");<br/>
return(result); <br/>
}<br/>
<br/>
void ServiceMain(int argc, char** argv) <br/>
{ <br/>
int error; <br/>
MEMORYSTATUS memory;<br/>
char buffer[16];<br/>
int result;<br/>
<br/>
ServiceStatus.dwServiceType = SERVICE_WIN32; <br/>
ServiceStatus.dwCurrentState = SERVICE_START_PENDING; <br/>
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;<br/>
ServiceStatus.dwWin32ExitCode = 0; <br/>
ServiceStatus.dwServiceSpecificExitCode = 0; <br/>
ServiceStatus.dwCheckPoint = 0; <br/>
ServiceStatus.dwWaitHint = 0; <br/>
<br/>
hStatus = RegisterServiceCtrlHandler(<br/>
"MemoryStatus", <br/>
(LPHANDLER_FUNCTION)ControlHandler); <br/>
if (hStatus == (SERVICE_STATUS_HANDLE)0) <br/>
{ <br/>
// Registering Control Handler failed<br/>
return; <br/>
} <br/>
// Initialize Service <br/>
error = InitService(); <br/>
if (error) <br/>
{<br/>
// Initialization failed<br/>
ServiceStatus.dwCurrentState = SERVICE_STOPPED; <br/>
ServiceStatus.dwWin32ExitCode = -1; <br/>
SetServiceStatus(hStatus, &ServiceStatus); <br/>
return; <br/>
} <br/>
<br/>
// We report the running status to SCM. <br/>
ServiceStatus.dwCurrentState = SERVICE_RUNNING; <br/>
SetServiceStatus (hStatus, &ServiceStatus);<br/>
<br/>
// The worker loop of a service<br/>
while (ServiceStatus.dwCurrentState == SERVICE_RUNNING)<br/>
{<br/>
GlobalMemoryStatus(&memory);<br/>
sprintf(buffer, "%d", memory.dwAvailPhys);<br/>
result = WriteToLog(buffer);<br/>
if (result)<br/>
{<br/>
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
<br/>
ServiceStatus.dwWin32ExitCode = -1;
<br/>
SetServiceStatus(hStatus, &ServiceStatus);<br/>
return;<br/>
}<br/>
Sleep(SLEEP_TIME);<br/>
}<br/>
return; <br/>
}<br/>
<br/>
void ControlHandler(DWORD request) <br/>
{ <br/>
switch(request) <br/>
{ <br/>
case SERVICE_CONTROL_STOP: <br/>
WriteToLog("Monitoring stopped.");<br/>
<br/>
ServiceStatus.dwWin32ExitCode = 0;
<br/>
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
<br/>
SetServiceStatus (hStatus, &ServiceStatus);<br/>
return; <br/>
<br/>
case SERVICE_CONTROL_SHUTDOWN: <br/>
WriteToLog("Monitoring stopped.");<br/>
<br/>
ServiceStatus.dwWin32ExitCode = 0;
<br/>
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
<br/>
SetServiceStatus (hStatus, &ServiceStatus);<br/>
return; <br/>
<br/>
default:<br/>
break;<br/>
} <br/>
<br/>
// Report current status<br/>
SetServiceStatus (hStatus, &ServiceStatus);<br/>
return; <br/>
}<br/>
<br/>
<br/>
InstallService.txt//Whether i should be a .txt file or .c file,it should be added to solution or not where to keep it???
#include <windows.h><br/>
<br/>
#include <stdio.h><br/>
<br/>
<br/>
<br/>
int main()<br/>
<br/>
{<br/>
<br/>
SC_HANDLE schSCManager, schService;<br/>
<br/>
// The service executable location, just dummy here else make sure the executable is there :o)...<br/>
<br/>
LPCTSTR lpszBinaryPathName = "%SystemRoot%\system\mytestservice.exe";<br/>
<br/>
// Service display name...<br/>
<br/>
LPCTSTR lpszDisplayName = "My_Sample_Service";<br/>
<br/>
// Registry Subkey<br/>
<br/>
LPCTSTR lpszServiceName = "MySampleSrv";<br/>
<br/>
// Open a handle to the SC Manager database...<br/>
<br/>
schSCManager = OpenSCManager(<br/>
<br/>
NULL, // local machine<br/>
<br/>
NULL, // SERVICES_ACTIVE_DATABASE database is opened by default<br/>
<br/>
SC_MANAGER_ALL_ACCESS); // full access rights<br/>
<br/>
<br/>
<br/>
if (NULL == schSCManager)<br/>
<br/>
printf("OpenSCManager() failed, error: %d.n", GetLastError());<br/>
<br/>
else<br/>
<br/>
printf("OpenSCManager() looks OK.n");<br/>
<br/>
<br/>
<br/>
// Create/install service...<br/>
<br/>
schService = CreateService(<br/>
<br/>
schSCManager, // SCManager database<br/>
<br/>
lpszServiceName, // name of service<br/>
<br/>
lpszDisplayName, // service name to display<br/>
<br/>
SERVICE_ALL_ACCESS, // desired access<br/>
<br/>
SERVICE_WIN32_OWN_PROCESS, // service type<br/>
<br/>
SERVICE_DEMAND_START, // start type<br/>
<br/>
SERVICE_ERROR_NORMAL, // error control type<br/>
<br/>
lpszBinaryPathName, // services binary<br/>
<br/>
NULL, // no load ordering group<br/>
<br/>
NULL, // no tag identifier<br/>
<br/>
NULL, // no dependencies<br/>
<br/>
NULL, // LocalSystem account<br/>
<br/>
NULL); // no password<br/>
<br/>
<br/>
<br/>
if (schService == NULL)<br/>
<br/>
{<br/>
<br/>
printf("CreateService() failed.n");<br/>
<br/>
return FALSE;<br/>
<br/>
}<br/>
<br/>
else<br/>
<br/>
{<br/>
<br/>
printf("CreateService() for %s looks OK.n", lpszServiceName);<br/>
<br/>
if (CloseServiceHandle(schService) == 0)<br/>
<br/>
printf("CloseServiceHandle() failed, error: %d.n", GetLastError());<br/>
<br/>
else<br/>
<br/>
printf("CloseServiceHandle() is OK.n");<br/>
<br/>
<br/>
<br/>
return 0;<br/>
<br/>
}<br/>
<br/>
}
Interactive_services.txt// Whether i should be a .txt file or .c file,it should be added to solution or not where to keep it???
#include <windows.h><br/>
<br/>
<br/>
<br/>
STARTUPINFO si;<br/>
<br/>
PROCESS_INFORMATION ProcessInformation;<br/>
<br/>
<br/>
<br/>
si.cb = sizeof(STARTUPINFO);<br/>
<br/>
si.lpReserved = NULL;<br/>
<br/>
si.lpTitle = NULL;<br/>
<br/>
si.lpDesktop = "WinSta0\Default";<br/>
<br/>
si.dwX = si.dwY = si.dwXSize = si.dwYSize = 0L;<br/>
<br/>
si.dwFlags = 0;<br/>
<br/>
si.wShowWindow = SW_SHOW;<br/>
<br/>
si.lpReserved2 = NULL;<br/>
<br/>
si.cbReserved2 = 0;<br/>
<br/>
if (CreateProcess(NULL, lpszCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &ProcessInformation))<br/>
<br/>
{<br/>
<br/>
CloseHandle(ProcessInformation.hProcess);<br/>
<br/>
CloseHandle(ProcessInformation.hThread);<br/>
<br/>
}<br/>

Can anyone guide me on this step wise please
<hr class="sig Tarun

View the full article
 
Back
Top