Article ID: 197820
Article Last Modified on 8/25/2005
BOOL CMyService::OnInit()
{
// Read the registry parameters.
// Try opening the registry key:
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet
// \Services\<AppName>\Parameters
HKEY hkey;
char szKey[1024];
strcpy(szKey, "SYSTEM\\CurrentControlSet\\Services\\");
strcat(szKey, m_szServiceName);
strcat(szKey, "\\Parameters");
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
szKey,
0,
KEY_QUERY_VALUE,
&hkey) == ERROR_SUCCESS)
{
// Yes we are installed.
DWORD dwType = 0;
DWORD dwSize = sizeof(m_iStartParam);
RegQueryValueEx(hkey,
"Start",
NULL,
&dwType,
(BYTE*)&m_iStartParam,
&dwSize);
dwSize = sizeof(m_iIncParam);
RegQueryValueEx(hkey,
"Inc",
NULL,
&dwType,
(BYTE*)&m_iIncParam,
&dwSize);
RegCloseKey(hkey);
}
// Set the initial state.
m_iState = m_iStartParam;
return TRUE;
}
void CNTService::Run()
{
DebugMsg("Entering CNTService::Run()");
while (m_bIsRunning) {
DebugMsg("Sleeping...");
Sleep(5000);
}
// Nothing more to do.
DebugMsg("Leaving CNTService::Run()");
}
The OnInit method is where the service initializes itself and does all the
preparation work for preparing to run. For our purposes, this is the most
important method because this is where you would add code to initialize the
MAPI subsystem and logon to a session.
BOOL CMyService::OnInit()
{
// Read the registry parameters.
// Try opening the registry key:
// HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet
// \Services\<AppName>\Parameters
HKEY hkey;
char szKey[1024];
strcpy(szKey, "SYSTEM\\CurrentControlSet\\Services\\");
strcat(szKey, m_szServiceName);
strcat(szKey, "\\Parameters");
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,
szKey,
0,
KEY_QUERY_VALUE,
&hkey) == ERROR_SUCCESS)
{
// Yes we are installed.
DWORD dwType = 0;
DWORD dwSize = sizeof(m_iStartParam);
RegQueryValueEx(hkey,
"Start",
NULL,
&dwType,
(BYTE*)&m_iStartParam,
&dwSize);
dwSize = sizeof(m_iIncParam);
RegQueryValueEx(hkey,
"Inc",
NULL,
&dwType,
(BYTE*)&m_iIncParam,
&dwSize);
RegCloseKey(hkey);
}
StartMAPI ( &m_pSession );
// Set the initial state.
m_iState = m_iStartParam;
return TRUE;
}
HRESULT CNTService::StartMAPI ( LPMAPISESSION *pSess )
{
// Declare and initialize any variables used by MAPI.
HRESULT hRes = S_OK;
MAPIINIT_0 pMapiInit = { MAPI_INIT_VERSION, MAPI_NT_SERVICE };
ULONG ulFlags = 0L;
char pProfile[64];
LPMAPISESSION pSvcSess = NULL;
// Initialize MAPI.
hRes = MAPIInitialize ( &pMapiInit );
// If MAPIInitialize succeeds, create a session object.
if ( !FAILED ( hRes ) )
{
ulFlags = MAPI_NO_MAIL |
MAPI_NEW_SESSION |
MAPI_EXPLICIT_PROFILE |
MAPI_NT_SERVICE ;
hRes = MAPILogonEx ( 0L, pProfile, NULL, ulFlags, &pSvcSess);
}
if ( !FAILED ( hRes ) )
{
*pSess = pSvcSess;
}
return hRes;
}
Because neither StartMAPI() nor m_pSession are already defined in the
CNTService class, you need to add this method and property to the class
definition so the compiler knows to expect them up front. Also, this
Windows NT Service needs to be aware of all of the special data types that
MAPI supports. To do this add the following lines of code to the class
definition found in the accompanying header file:
HRESULT StartMAPI ( LPMAPISESSION * ); LPMAPISESSION m_pSession;Add the following line of code to the same header file in the file scope section at the top where the other include statement is found:
#include <mapix.h>There are a few things in this MAPI code that are not necessary for normal desktop MAPI clients to do but are necessary for MAPI clients that run in a Windows NT Service. The first major difference is the call to the MAPIInitialize() function. You pass in a MAPIINIT_0 structure that lets the MAPI subsystem know that MAPI is going to execute in a Windows NT Service. This occurs in the OnInit because this is where all initialization should occur. It would be confusing to move this code to the Run method because by the time your service is in the Run method, it appears that the service has passed all initialization tests. Receiving an initialization error in the Run method would be misleading to the user.
Windows NT Services are expected to run unattended.
Keywords: kbhowto kbmsg kbcode KB197820