EDN Admin
Well-known member
Hello I have created an ATL service . Below is my Code
<div style="color:Black;background-color:White
<pre><span style="color:Green // SDMService.cpp : Implementation of WinMain
#include <span style="color:#a31515 "stdafx.h"
<span style="color:Green //#include "resource.h"
#include <span style="color:#a31515 "SDMService_i.h"
<span style="color:Green //#include <stdio.h>
<span style="color:Green // Here is the timer class object
ActivationTimer timer;
<span style="color:Blue void
MessageDisplay(<span style="color:Blue void
);
<span style="color:Blue void
mainjob(<span style="color:Blue void
);
<span style="color:Green // Use enum to maintain the service status
[v1_enum] <span style="color:Green // specifies IDL v1_enum attribute categories = IDL , usage : enums
<span style="color:Blue enum
CServiceStatus
{
run = 0,
pause = 1,
stop = 2,
shutmedown = 3
};
<span style="color:Blue class
CSDMServiceModule : <span style="color:Blue public
CAtlServiceModuleT <CSDMServiceModule, IDS_SERVICENAME >
{
<span style="color:Blue public
:
DECLARE_LIBID(LIBID_SDMServiceLib)
<span style="color:Blue private
:
<span style="color:Green // this flag in millisecond,tells us how often is checked for task
DWORD m_iPollTime;
HANDLE m_ThreadHandle;
HANDLE m_hServerStopEvent;
<span style="color:Blue bool
connDone;
CServiceStatus m_ServiceStatus;
<span style="color:Blue bool
stopNow;
<span style="color:Blue public
:
~CSDMServiceModule() <span style="color:Blue throw
()
{
<span style="color:Blue if
(m_ThreadHandle!=NULL)
CloseHandle(m_ThreadHandle);
<span style="color:Blue if
(m_hServerStopEvent!=NULL)
CloseHandle(m_hServerStopEvent);
}
CSDMServiceModule() <span style="color:Blue throw
() :m_iPollTime(2000),m_ThreadHandle(NULL),m_hServerStopEvent(NULL),stopNow(<span style="color:Blue false
)
{
m_ServiceStatus = CServiceStatus::run;
<span style="color:Green //m_dwTimeOut = 60000; // one minute for timeout
}
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_SDMSERVICE, <span style="color:#a31515 "{E00B6E93-A758-433B-AA99-48612EB949E5}"
)
HRESULT InitializeSecurity() <span style="color:Blue throw
()
{
<span style="color:Green // TODO : Call CoInitializeSecurity and provide the appropriate security settings for
<span style="color:Green // your service
<span style="color:Green // Suggested - PKT Level Authentication,
<span style="color:Green // Impersonation Level of RPC_C_IMP_LEVEL_IDENTIFY
<span style="color:Green // and an appropiate Non NULL Security Descriptor.
<span style="color:Blue return
S_OK;
}
<span style="color:Green // Implemented by me from here
<span style="color:Blue void
OnPause() <span style="color:Blue throw
()
{
m_ServiceStatus = CServiceStatus:
ause;
__super::OnPause();
<span style="color:Blue if
(m_hServerStopEvent!=NULL)
SetEvent(m_hServerStopEvent);
}
<span style="color:Blue void
OnStop() <span style="color:Blue throw
()
{
__super::OnStop();
m_ServiceStatus = CServiceStatus::stop;
<span style="color:Blue if
(m_hServerStopEvent!=NULL)
{
stopNow = <span style="color:Blue true
; <span style="color:Green // be sure to kill the thread & process before it nicely ends
<span style="color:Green // otherwise our pointer will release while the instance has been killed already,a common bug
SetEvent(m_hServerStopEvent);
DWORD tc = GetTickCount();
<span style="color:Blue while
(m_hServerStopEvent!=NULL || GetTickCount()-tc>20000) <span style="color:Green // max 20 sec.
Sleep(10); <span style="color:Green // 10 millisec
}
}
<span style="color:Blue void
OnContinue() <span style="color:Blue throw
()
{
__super::OnContinue();
m_ServiceStatus = CServiceStatus::run;
<span style="color:Blue if
(m_hServerStopEvent!=NULL)
SetEvent(m_hServerStopEvent);
}
<span style="color:Blue void
OnShutDown() <span style="color:Blue throw
()
{
<span style="color:Green //__super::OnShutDown();
m_ServiceStatus = CServiceStatus:
ause;
<span style="color:Blue if
( m_hServerStopEvent != NULL)
SetEvent(m_hServerStopEvent);
}
HRESULT RegisterAppId(<span style="color:Blue bool
bService = <span style="color:Blue false
) <span style="color:Blue throw
()
{
HRESULT hr = S_OK;
<span style="color:Green // we extended our service description
<span style="color:Green // on w2k or any higher it is user friendly
BOOL res = __super::RegisterAppId(bService);
<span style="color:Blue if
(bService)
{
<span style="color:Green //DebugBreak();
<span style="color:Blue if
(IsInstalled())
{
SC_HANDLE hSCM = ::OpenSCManagerW(NULL,NULL,SERVICE_CHANGE_CONFIG);
SC_HANDLE hService = NULL;
<span style="color:Blue if
(hSCM==NULL)
hr = AtlHresultFromLastError();
<span style="color:Blue else
{
hService = ::OpenServiceW(hSCM,m_szServiceName,SERVICE_CHANGE_CONFIG);
<span style="color:Blue if
(hService!=NULL)
{
<span style="color:Blue const
<span style="color:Blue int
m_szServiceNameLen = 4096;
WCHAR m_szServiceDescription[m_szServiceNameLen]={0};
LoadStringW(_AtlBaseModule.GetModuleInstance(),IDS_SERVICENAME,m_szServiceDescription,m_szServiceNameLen);
SERVICE_DESCRIPTION sdbuf = {m_szServiceDescription};
res = ChangeServiceConfig2W(hService,SERVICE_CONFIG_DESCRIPTION,&sdbuf);
::CloseServiceHandle(hService);
}
<span style="color:Blue else
hr = AtlHresultFromLastError();
::CloseServiceHandle(hSCM);
}
}
}
<span style="color:Blue return
hr;
}
HRESULT PreMessageLoop(<span style="color:Blue int
nShowCmd) <span style="color:Blue throw
()
{
HRESULT hr = __super:
reMessageLoop(nShowCmd);
<span style="color:Green // if we dont have any COM classes Register class object returns S_FALSE
<span style="color:Green // this cause the process to terminate
<span style="color:Green // we dont want this so we written S_OK in this case
<span style="color:Blue if
(hr==S_FALSE) hr = S_OK;
<span style="color:Blue if
(m_bService==TRUE && hr == S_OK)
m_ThreadHandle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)mainjob,(<span style="color:Blue void
*)<span style="color:Blue this
,0,0);
}
};
CSDMServiceModule _AtlModule;
<span style="color:Blue extern
<span style="color:#a31515 "C"
<span style="color:Blue int
WINAPI _tWinMain(HINSTANCE <span style="color:Green /*hInstance*/
, HINSTANCE <span style="color:Green /*hPrevInstance*/
,
LPTSTR <span style="color:Green /*lpCmdLine*/
, <span style="color:Blue int
nShowCmd)
{
_AtlModule.LogEvent(L<span style="color:#a31515 "This is SDM Service : in Main function"
);
<span style="color:Blue return
_AtlModule.WinMain(nShowCmd);
<span style="color:Green //MessageDisplay();
}
<span style="color:Blue void
MessageDisplay(<span style="color:Blue void
)
{
<span style="color:Green // ::MessageBox(0,L"Test service",L"SDMService",MB_OK);
_AtlModule.LogEvent(L<span style="color:#a31515 "This is in function MessageDisplay of SDMService"
);
}
<span style="color:Blue void
mainjob(<span style="color:Blue void
)
{
timer.AddTask(5000,MessageDisplay);
}
[/code]
This is code.<br/> <br/> Please check the above code and tell me whether i have done any mistake.<br/> <br/> When i debug the service it works but not when i register and start the service from the control panel.<br/> <br/> Is i required to create any thread or Event? if so please tell me How and where to create thread or event.<br/> <br/> Please help
<
Logic is my Greatest Asset !
View the full article
<div style="color:Black;background-color:White
<pre><span style="color:Green // SDMService.cpp : Implementation of WinMain
#include <span style="color:#a31515 "stdafx.h"
<span style="color:Green //#include "resource.h"
#include <span style="color:#a31515 "SDMService_i.h"
<span style="color:Green //#include <stdio.h>
<span style="color:Green // Here is the timer class object
ActivationTimer timer;
<span style="color:Blue void
MessageDisplay(<span style="color:Blue void
);
<span style="color:Blue void
mainjob(<span style="color:Blue void
);
<span style="color:Green // Use enum to maintain the service status
[v1_enum] <span style="color:Green // specifies IDL v1_enum attribute categories = IDL , usage : enums
<span style="color:Blue enum
CServiceStatus
{
run = 0,
pause = 1,
stop = 2,
shutmedown = 3
};
<span style="color:Blue class
CSDMServiceModule : <span style="color:Blue public
CAtlServiceModuleT <CSDMServiceModule, IDS_SERVICENAME >
{
<span style="color:Blue public
:
DECLARE_LIBID(LIBID_SDMServiceLib)
<span style="color:Blue private
:
<span style="color:Green // this flag in millisecond,tells us how often is checked for task
DWORD m_iPollTime;
HANDLE m_ThreadHandle;
HANDLE m_hServerStopEvent;
<span style="color:Blue bool
connDone;
CServiceStatus m_ServiceStatus;
<span style="color:Blue bool
stopNow;
<span style="color:Blue public
:
~CSDMServiceModule() <span style="color:Blue throw
()
{
<span style="color:Blue if
(m_ThreadHandle!=NULL)
CloseHandle(m_ThreadHandle);
<span style="color:Blue if
(m_hServerStopEvent!=NULL)
CloseHandle(m_hServerStopEvent);
}
CSDMServiceModule() <span style="color:Blue throw
() :m_iPollTime(2000),m_ThreadHandle(NULL),m_hServerStopEvent(NULL),stopNow(<span style="color:Blue false
)
{
m_ServiceStatus = CServiceStatus::run;
<span style="color:Green //m_dwTimeOut = 60000; // one minute for timeout
}
DECLARE_REGISTRY_APPID_RESOURCEID(IDR_SDMSERVICE, <span style="color:#a31515 "{E00B6E93-A758-433B-AA99-48612EB949E5}"
)
HRESULT InitializeSecurity() <span style="color:Blue throw
()
{
<span style="color:Green // TODO : Call CoInitializeSecurity and provide the appropriate security settings for
<span style="color:Green // your service
<span style="color:Green // Suggested - PKT Level Authentication,
<span style="color:Green // Impersonation Level of RPC_C_IMP_LEVEL_IDENTIFY
<span style="color:Green // and an appropiate Non NULL Security Descriptor.
<span style="color:Blue return
S_OK;
}
<span style="color:Green // Implemented by me from here
<span style="color:Blue void
OnPause() <span style="color:Blue throw
()
{
m_ServiceStatus = CServiceStatus:

__super::OnPause();
<span style="color:Blue if
(m_hServerStopEvent!=NULL)
SetEvent(m_hServerStopEvent);
}
<span style="color:Blue void
OnStop() <span style="color:Blue throw
()
{
__super::OnStop();
m_ServiceStatus = CServiceStatus::stop;
<span style="color:Blue if
(m_hServerStopEvent!=NULL)
{
stopNow = <span style="color:Blue true
; <span style="color:Green // be sure to kill the thread & process before it nicely ends
<span style="color:Green // otherwise our pointer will release while the instance has been killed already,a common bug
SetEvent(m_hServerStopEvent);
DWORD tc = GetTickCount();
<span style="color:Blue while
(m_hServerStopEvent!=NULL || GetTickCount()-tc>20000) <span style="color:Green // max 20 sec.
Sleep(10); <span style="color:Green // 10 millisec
}
}
<span style="color:Blue void
OnContinue() <span style="color:Blue throw
()
{
__super::OnContinue();
m_ServiceStatus = CServiceStatus::run;
<span style="color:Blue if
(m_hServerStopEvent!=NULL)
SetEvent(m_hServerStopEvent);
}
<span style="color:Blue void
OnShutDown() <span style="color:Blue throw
()
{
<span style="color:Green //__super::OnShutDown();
m_ServiceStatus = CServiceStatus:

<span style="color:Blue if
( m_hServerStopEvent != NULL)
SetEvent(m_hServerStopEvent);
}
HRESULT RegisterAppId(<span style="color:Blue bool
bService = <span style="color:Blue false
) <span style="color:Blue throw
()
{
HRESULT hr = S_OK;
<span style="color:Green // we extended our service description
<span style="color:Green // on w2k or any higher it is user friendly
BOOL res = __super::RegisterAppId(bService);
<span style="color:Blue if
(bService)
{
<span style="color:Green //DebugBreak();
<span style="color:Blue if
(IsInstalled())
{
SC_HANDLE hSCM = ::OpenSCManagerW(NULL,NULL,SERVICE_CHANGE_CONFIG);
SC_HANDLE hService = NULL;
<span style="color:Blue if
(hSCM==NULL)
hr = AtlHresultFromLastError();
<span style="color:Blue else
{
hService = ::OpenServiceW(hSCM,m_szServiceName,SERVICE_CHANGE_CONFIG);
<span style="color:Blue if
(hService!=NULL)
{
<span style="color:Blue const
<span style="color:Blue int
m_szServiceNameLen = 4096;
WCHAR m_szServiceDescription[m_szServiceNameLen]={0};
LoadStringW(_AtlBaseModule.GetModuleInstance(),IDS_SERVICENAME,m_szServiceDescription,m_szServiceNameLen);
SERVICE_DESCRIPTION sdbuf = {m_szServiceDescription};
res = ChangeServiceConfig2W(hService,SERVICE_CONFIG_DESCRIPTION,&sdbuf);
::CloseServiceHandle(hService);
}
<span style="color:Blue else
hr = AtlHresultFromLastError();
::CloseServiceHandle(hSCM);
}
}
}
<span style="color:Blue return
hr;
}
HRESULT PreMessageLoop(<span style="color:Blue int
nShowCmd) <span style="color:Blue throw
()
{
HRESULT hr = __super:

<span style="color:Green // if we dont have any COM classes Register class object returns S_FALSE
<span style="color:Green // this cause the process to terminate
<span style="color:Green // we dont want this so we written S_OK in this case
<span style="color:Blue if
(hr==S_FALSE) hr = S_OK;
<span style="color:Blue if
(m_bService==TRUE && hr == S_OK)
m_ThreadHandle = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)mainjob,(<span style="color:Blue void
*)<span style="color:Blue this
,0,0);
}
};
CSDMServiceModule _AtlModule;
<span style="color:Blue extern
<span style="color:#a31515 "C"
<span style="color:Blue int
WINAPI _tWinMain(HINSTANCE <span style="color:Green /*hInstance*/
, HINSTANCE <span style="color:Green /*hPrevInstance*/
,
LPTSTR <span style="color:Green /*lpCmdLine*/
, <span style="color:Blue int
nShowCmd)
{
_AtlModule.LogEvent(L<span style="color:#a31515 "This is SDM Service : in Main function"
);
<span style="color:Blue return
_AtlModule.WinMain(nShowCmd);
<span style="color:Green //MessageDisplay();
}
<span style="color:Blue void
MessageDisplay(<span style="color:Blue void
)
{
<span style="color:Green // ::MessageBox(0,L"Test service",L"SDMService",MB_OK);
_AtlModule.LogEvent(L<span style="color:#a31515 "This is in function MessageDisplay of SDMService"
);
}
<span style="color:Blue void
mainjob(<span style="color:Blue void
)
{
timer.AddTask(5000,MessageDisplay);
}
[/code]
This is code.<br/> <br/> Please check the above code and tell me whether i have done any mistake.<br/> <br/> When i debug the service it works but not when i register and start the service from the control panel.<br/> <br/> Is i required to create any thread or Event? if so please tell me How and where to create thread or event.<br/> <br/> Please help
<
Logic is my Greatest Asset !
View the full article