Using a Simple MAPI in API that Runs as a Windows NT Service

Introduction

As you should know, a Win NT service is an application that does not have a user interface and can run (if configured) automatically every time the workstation/server boots. This is an advantage for some applications that need to be executed without user intervention (the user is not required to log in and execute an application) and they're running permanently in an infinite cycle, waiting for an event to do one or more tasks.

An example of this is the application that I'm developing. In general, its main goals are:

Send via e-mail some documents every time they are modified, to their destination registered on a MSSQL database.

The e-mail account must be configured. This means that you have to log on to Win NT and configure the e-mail on Control Panel + Mail.

The Service has to run with the user-configured mail account, as explained in Point 1. To do this, go to Control Panel + Service + <double-click on the service you want to change> and change the Log On As from System Account to This Account: and input the user name, password, and then confirm the password.

The user has to be a member of the \Administrators group on the machine where the service is running. Check the MSDN article for more information.

Notes

To be honest, I don't know if it is possible to debug a service (maybe debugging attaching to a process), so to simply debug the application, just uncomment the lines below on the code and between EfaLogOnMail and EfaLogOffMail add your code to be executed. These lines are just before the code needed for the service definition.

// COMMENT THE FOLLOWING LINES TO RUN AS A WINNT
// SERVICE //
EfaLogOnMail ();
EfaLogOffMail ();
return (0);
// COMMENT THE FOLLOWING LINES TO RUN AS A WINNT
// SERVICE //

Downloads

Comments

Debugging a service

Try putting a DebugBreak() command in the DEBUG build of the service. When the breakpoint is reached, click on the Cancel button of the message to debug the code; the debugger will attach to the service

Debugging services as they startup

I just came across some words in "Inside Windows 2000" (Solomon, Russinovich) that may help those that want to debug services as they startup, rather than after they have already started.

In the Registry under HLKM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options, you can add keys that alter the functionality of CreateProcess and allow you to override the execution of a specific image file and in its place have a debugger run.

There is an example key in the Registry called "Your Image File Name Here..." that shows you the basic format for entering the required information.

With this functionality you can have services debugged as they startup as opposed to after they have already started. Or you can use this to play evil tricks on your friends' computers while they are not looking =) Your choice.

For more information on this topic see "Inside Windows 2000" in Chapter 6 "Processes, Threads, and Jobs" on page 309.

Top White Papers and Webcasts

Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT
The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility.
Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …