Visual Basic Code of the Week (COTW)
Issue #125
Online Version at (paid subscribers only)
All content and source code is Copyright (c) 2000 by C&D Programming Corp. No part of this issue can be reprinted or distributed in any manner without express written permission of C&D Programming Corp.

Issue topic: Running your application across logins (like a Windows Service)

IMPORTANT! - Publication Change

Starting with the last issue the full Code of the Week issues will only be published every other week. All current subscribers which have purchased an annual subscription will still receive all 52 issues they paid for, it will just take two years to get them instead of one year. In the weeks between publication of Code of the Week we will usually send out shorter issues with more a tip type content (similar to the Ziff Davis VBTips ezine).

If you have any question about this publication change, please notify us at

Extra CASH!

If you have any tips to contribute, email us at Be sure to include instructions and source code. For each tip received which gets published we will pay you $10 to $25 US Dollars.


In this Issue

In this issue we discuss how to run your application as a Windows Service (not an NT service).

If you have any questions about using this module, let us know at


This module shows how to make your application run as a service. The main portion of these routines use the RegisterServiceProcess function to tell Windows that your application should stay active across Windows logins. It will also keep your application from showing up in the Ctrl-Alt-Del task listing. To use this module just call ServiceInstall at the start up of your application and ServiceUninstall at the end.

Microsoft describes this function as follows:

The RegisterServiceProcess Function

Applications started by the system using the RunServices and RunServicesOnce keys will close when the user selects Close all programs and log on as a different user from the Shutdown dialog box. By calling the RegisterServiceProcess function, a Win32-based application can prevent itself or any other Win32-based application from being closed when the user logs off. Win32-based applications registered in this manner close only when the system is shut down.

The application should provide for different users logging on at different times during its execution. The application can distinguish between a user logging off and the system shutting down by examining the lParam parameter of the WM_QUERYENDSESSION and WM_ENDSESSION messages. If the user shuts down the system, lParam is NULL. If the user logs off, lParam is set to ENDSESSION_LOGOFF.


Public Sub ServiceInstall()

Install the running application as a service. An error will be raised if this is unsuccessful.

Public Sub ServiceUninstall()

Uninstall the running application as a service. An error will be raised if this is unsuccessful.

Sample Usage

This sample shows how to use the ServiceInstall and ServiceUninstall routine.

Public Sub Main()
End Sub

Public frm_Unload(Cancel as Integer)
End Sub

Source Code

To see the source code for this issue you must be a subscriber to Code of the Week. If you are a subscriber the source code is available at the following address:

This document is available on the web

Paid subscribers can view this issue in HTML format. There is no additional source or information in the HTML formatted document. It just looks a little better since we have included some HTML formatting. Just point your browser to link at the top of this document.

Other links

Contact Information

C&D Programming Corp.
PO Box 20128
Floral Park, NY 11002-0128
Phone or Fax: (212) 504-7945