Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / C++

Taking Advantage of the Winlogon Notification Package

Rate me:
Please Sign up or sign in to vote.
5.00/5 (18 votes)
6 Jan 2001CPOL 340.5K   78   105
Taking advantage of the Winlogon Notification Package
<!-- Add the rest of your HTML here -->

Introduction

The Winlogon Notification Package is a DLL which exports functions that handle Winlogon.exe events. These event messages includes lock, unlock, logoff, logon, startup, shutdown, startscreensaver, stopscreensaver, and startshell. 

This article demonstrates how to use the Winlogon Notification Package as an alternative to NT Services. The main benefits for doing this is better handling of user activities. In addition, the Winlogon Notification Package will be very lightweight and requires much less code then its NT service equivalent. 

The Steps

Creating a Winlogon Notification package is very simple. Just create a DLL with specific functions to run during the Winlogon event messages. To let Winlogon.exe know about your DLL, simply add a few entries into the registry where appropriate. This method can be quite robust and versatile when combined with your services and applications.

Sample

This sample starts a WIN32 application before the user logon. Because the process is started by Winlogon, it is owned by the system account. Users may not end the process through 'End Task'. This is the exact way NT services behave. In this sample, the logoff notification will terminate the process. If the process needed to stay active, the EndProcessAtWinlogoff function should be removed. If we wanted the process to be owned by the user, we could use CreateProcessAsUser during a startup notification instead of a logon notification. 

Step 1.) - the dll

//sample.cpp

#include <windows.h>
#include <Winwlx.h>

PROCESS_INFORMATION g_pi;
TCHAR g_szPath[] = _T("c:\somepath\execut.exe \"arguments\"");

//This function safely terminates a process, allowing
//it to do cleanup (ie. DLL detach)
//It can be found at the Windows Developer's Journal
SafeTerminateProcess(HANDLE hProcess, UINT uExitCode);   

//Entrance function for the DLL
BOOL WINAPI LibMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
    switch (dwReason)
    {
        case DLL_PROCESS_ATTACH:
        {
	    DisableThreadLibraryCalls (hInstance);	
        }
        break;
    }
    return TRUE;
}

//Event handler for the Winlogon Logon event
VOID APIENTRY StartProcessAtWinLogon (PWLX_NOTIFICATION_INFO pInfo)
{
    STARTUPINFO si;
    si.cb = sizeof(STARTUPINFO); 
    si.lpReserved = NULL; 
    si.lpTitle = NULL; 
    si.lpDesktop = "WinSta0\\Default"; 
    si.dwX = si.dwY = si.dwXSize = si.dwYSize = 0L; 
    si.dwFlags = 0;; 
    si.wShowWindow = SW_SHOW; 
    si.lpReserved2 = NULL; 
    si.cbReserved2 = 0; 
				
    CreateProcess(NULL, g_szPath, NULL, NULL, FALSE, CREATE_NEW_CONSOLE,
                  NULL, NULL, &si, &g_pi);
}

//Event handler for the Winlogon Logoff event.
VOID APIENTRY StopProcessAtWinLogoff (PWLX_NOTIFICATION_INFO pInfo)
{
    //terminates the process
    SafeTerminateProcess(g_pi.hProcess, 0xDEADBEEF);  
}

//other event handlers
VOID APIENTRY YOUR_EVENT_HANDLERS (PWLX_NOTIFICATION_INFO pInfo)
{
    //code
}

...

Step 2.) - the exports

The program hasn't exported any functions yet. We need to create a .def file.

sample.def

EXPORTS
StartProcessAtWinLogon
StopProcessAtWinLogoff

 

Now add the following to your linkage options in VC6 and build.

/def: "sample.def"

If everything went well, the files sample.dll and sample.exp will be in your output folder. Move these to \%NTROOT%\system32

Step 3.) - the registry

Add the following values and keys to the registry. These values communicate to Winlogon.exe and let it know which procedures to run during an event notification. Add as few or as many notification events as needed.

HKEY_LOCAL_MACHINE
    \Software
        \Microsoft
            \Windows NT
                \CurrentVersion
                    \Winlogon
                        \Notify
                            \NameOfProject
                                \Asynchronous  REG_DWORD  0
                                \Dllname       REG_SZ     NameOfDll.dll
                                \Impersonate   REG_DWORD  0
                                \Logon         REG_SZ     StartProcessAtWinLogon
                                \Logoff        REG_SZ     StopProcessAtWinLogoff
                                \...           REG_SZ     NameOfFunction

That's it! Now restart and Winlogon.exe will launch your app.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Frontline Direct Inc., Adconion
United States United States
Tony Truong graduated from UCLA in Spring of 2001 and starting worked at Symantec Corporation as a Software Engineer. After a few years of developing various features for Norton SystemWorks, Tony moved to San Diego. He is currently writing database applications using ASP.NET and C# with the .NET Framework. Tony specializes in tara-byte databases with emphasis on high availability, optimization, and complex entity modeling.

Comments and Discussions

 
GeneralProblem with winlogon notification package Pin
14-Mar-02 8:32
suss14-Mar-02 8:32 
GeneralRe: Problem with winlogon notification package Pin
Tony Truong29-Mar-02 12:18
Tony Truong29-Mar-02 12:18 
GeneralRe: Problem with winlogon notification package Pin
Cristian Amarie17-Jan-03 6:53
Cristian Amarie17-Jan-03 6:53 
GeneralRe: Problem with winlogon notification package Pin
Eugene Lipunov27-Mar-03 20:10
Eugene Lipunov27-Mar-03 20:10 
QuestionWhy CRecordset-derived class using CString so widely? Pin
bugtwo4-Feb-02 0:15
bugtwo4-Feb-02 0:15 
AnswerRe: Why CRecordset-derived class using CString so widely? Pin
bugtwo4-Feb-02 0:19
bugtwo4-Feb-02 0:19 
GeneralRe: Why CRecordset-derived class using CString so widely? Pin
Matt Gullett4-Feb-02 0:57
Matt Gullett4-Feb-02 0:57 
GeneralRe: Why CRecordset-derived class using CString so widely? Pin
Matt Gullett4-Feb-02 1:02
Matt Gullett4-Feb-02 1:02 
GeneralRe: Why CRecordset-derived class using CString so widely? Pin
bugtwo4-Feb-02 17:53
bugtwo4-Feb-02 17:53 
GeneralProblem on Windows Xp : cannot catch events of smart-card .... Pin
vanta3-Feb-02 23:36
vanta3-Feb-02 23:36 
GeneralRe: Problem on Windows Xp : cannot catch events of smart-card .... Pin
atms28-May-03 21:20
atms28-May-03 21:20 
GeneralSetTimer in Startup Pin
20-Nov-01 7:30
suss20-Nov-01 7:30 
GeneralRe: SetTimer in Startup Pin
Blake Miller2-Mar-04 15:18
Blake Miller2-Mar-04 15:18 
GeneralRe: SetTimer in Startup Pin
Coder2k25-Jun-04 8:40
Coder2k25-Jun-04 8:40 
GeneralWhere is PWLX_NOTIFICATION_INFO defined Pin
21-Aug-01 6:06
suss21-Aug-01 6:06 
GeneralRe: Where is PWLX_NOTIFICATION_INFO defined Pin
12-Sep-01 14:07
suss12-Sep-01 14:07 
GeneralRe: Where is PWLX_NOTIFICATION_INFO defined Pin
27-Sep-01 22:09
suss27-Sep-01 22:09 
GeneralRe: Where is PWLX_NOTIFICATION_INFO defined Pin
Anonymous24-Mar-03 1:08
Anonymous24-Mar-03 1:08 
GeneralRe: Where is PWLX_NOTIFICATION_INFO defined Pin
Anonymous9-Aug-02 18:16
Anonymous9-Aug-02 18:16 
GeneralRe: Where is PWLX_NOTIFICATION_INFO defined Pin
ThatsAlok3-Jul-04 0:23
ThatsAlok3-Jul-04 0:23 
Generalnotification package as a component Pin
Amit Dey18-Jul-01 19:04
Amit Dey18-Jul-01 19:04 
GeneralRe: notification package as a component Pin
Blake Miller2-Mar-04 15:20
Blake Miller2-Mar-04 15:20 
Generalnotification package as a component Pin
Amit Dey18-Jul-01 19:04
Amit Dey18-Jul-01 19:04 
QuestionIs Notification Package a better option for replaceing default window logn? Pin
cathychou20-Jun-01 13:19
cathychou20-Jun-01 13:19 
QuestionIs it true???? Pin
31-May-01 8:18
suss31-May-01 8:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.