I have written a windows service that needs to perform certain functions only if a User is logged in. The problem I am running into is that I get an error if the computer is in the process of being shut down or restarted. At all other times, the service runs just fine.
I need to either detect a logon/logoff event OR check somehow as to whether there is currently a valid logged on user. The closest I have found is ManagementEvent Watcher class, but I am not able to determine the appropiate event class to query for the Audit logon/logoff events. Basically I just need to not try and open the ad-watch if the user has logged off (Event Id 538). Of course, if there is an easier check, please let me know of that! (WindowsIdentit y.CurrentIdenti ty() does not work because it returns the system account, which is the context that the service is running under).
I do appreciate any assistance on this. Thank you! 7 8194
Aarti wrote: I have written a windows service that needs to perform certain functions only if a User is logged in. The problem I am running into is that I get an error if the computer is in the process of being shut down or restarted. At all other times, the service runs just fine.
I need to either detect a logon/logoff event OR check somehow as to whether there is currently a valid logged on user. The closest I have found is ManagementEvent Watcher class, but I am not able to determine the appropiate event class to query for the Audit logon/logoff events. Basically I just need to not try and open the ad-watch if the user has logged off (Event Id 538). Of course, if there is an easier check, please let me know of that! (WindowsIdentit y.CurrentIdenti ty() does not work because it returns the system account, which is the context that the service is running under).
I do appreciate any assistance on this. Thank you!
I hope I understood you correctly.
As far as I understand your problem is in the startup and shutdown processes. I think there is an option/property/something else?
that tells windows to warn the service when windows is about to shutdown. So when the service gets this message you can shut it
down manually to prevent the error that occurs from occuring. I'm sorry for being so vague, but I really dont know much about
services. I only read somewhere (cant find where) that there is this option, as I am implementing a service for my application in
the near future.
One other thing that I had in mind. Instead of checking if someone is logged on, you could test if some vital system service is
running, something that would be running only if the user was logged on. Not the best solution, just an idea.
Good luck.
Nick Z.
If you want the service to be notified when computer is shut down or restarted,
(1) Set CanShutDown property to true.
(2) Override OnShutDown() method to write appropriate code.
Set the serviceInstalle rs StartupType to Automatic, so that the service is started automatically when the computer is restarted, and you can write code for starting up the service in OnStart().
Similarly there are two more properties CanStop and CanHandlePowerE vent, which decide whether the service is notified of being stopped and when there is a change in power status (for example when computer goes into suspended mode), and corresponding methods, OnStop and OnPowerEvent, which may be used as per demand of application.
Hope this helps.
--
Sameeksha,
MCP (.NET)
"Aarti" wrote: I have written a windows service that needs to perform certain functions only if a User is logged in. The problem I am running into is that I get an error if the computer is in the process of being shut down or restarted. At all other times, the service runs just fine.
I need to either detect a logon/logoff event OR check somehow as to whether there is currently a valid logged on user. The closest I have found is ManagementEvent Watcher class, but I am not able to determine the appropiate event class to query for the Audit logon/logoff events. Basically I just need to not try and open the ad-watch if the user has logged off (Event Id 538). Of course, if there is an easier check, please let me know of that! (WindowsIdentit y.CurrentIdenti ty() does not work because it returns the system account, which is the context that the service is running under).
I do appreciate any assistance on this. Thank you!
OT, and just for fun: (lsc stands for logon status checker) http://www.stupidapps.com/lsc-home.html
Sunny
In article <85************ *************** *******@microso ft.com>, Aa***@discussio ns.microsoft.co m says... I have written a windows service that needs to perform certain functions only if a User is logged in. The problem I am running into is that I get an error if the computer is in the process of being shut down or restarted. At all other times, the service runs just fine.
I need to either detect a logon/logoff event OR check somehow as to whether there is currently a valid logged on user. The closest I have found is ManagementEvent Watcher class, but I am not able to determine the appropiate event class to query for the Audit logon/logoff events. Basically I just need to not try and open the ad-watch if the user has logged off (Event Id 538). Of course, if there is an easier check, please let me know of that!
(WindowsIdentit y.CurrentIdenti ty() does not work because it returns the system account, which is the context that the service is running under). I do appreciate any assistance on this. Thank you!
>On another note, I cannot believe how difficult it is to determine whether or not the icon tray or windows taskbar was loaded!!
I got an idea for that. Find out the class name or the window name of the tray icon. You can try to do that using GetWindowText
API call, you only need to find this out once, not everytime at runtime.
Now whenever your service starts you can call FindWindow with window text you found out previously as the windowName parameter. If
the window is found then the tray icon exists, if not then it didnt load yet. =) All controls including the tray icon are
technically windows as the OS sees it.
If you need further info or clarification, post here. If you cant find the class name or window name to call find window, post the
name and version of the application that loads the tray icon (ad-watch right? I even think I got it installed) I can probably get
it for you. A few days ago I made a litle program that gets the handles of any window from mouse clicks, I think it would be
fairly easy to expand to get some more info about the window.
Good luck.
Nick Z.
Aarti wrote: Nick I will try this out. I have not done any API-related stuff in .NET as yet, but will give this a shot. Seems to make sense! Thanks again for your help. I'll post back with the trial results! Aarti
"Nick" wrote:
>On another note, I cannot believe how difficult it is to determine whether or not the icon tray or windows taskbar was loaded!!
I got an idea for that. Find out the class name or the window name of the tray icon. You can try to do that using GetWindowText API call, you only need to find this out once, not everytime at runtime.
Now whenever your service starts you can call FindWindow with window text you found out previously as the windowName parameter. If the window is found then the tray icon exists, if not then it didnt load yet. =) All controls including the tray icon are technically windows as the OS sees it.
If you need further info or clarification, post here. If you cant find the class name or window name to call find window, post the name and version of the application that loads the tray icon (ad-watch right? I even think I got it installed) I can probably get it for you. A few days ago I made a litle program that gets the handles of any window from mouse clicks, I think it would be fairly easy to expand to get some more info about the window.
Good luck. Nick Z.
Here is something to help you out.
First include System.Runtime. InteropServices
//then import the function that you need using interop
[DllImport("user 32.dll",EntryPo int="SendMessag e")]
public static extern int SendMessage(Int Ptr h_Window,uint message,uint param1,uint param2);
The uint values you can get from winuser.h which is included with the platform sdk.
By the way I tried sending the message to the desktop window it does nothing.
Anybody know which direction the numlock key down event gets sent?
Aarti wrote: Nick It appears that the System tray window does not have a title. From what I could gather, the title it returns in GetWindowText is what appears in the title bar and since there is none for the system tray, it returns nothing. I am thinking of perhaps using GetDesktopWindo w as a proxy, although I don't know if that is too much better than checking for whether explorer.exe is running. I read that there is some other Shell dll to manipulate icons in the system tray .. perhaps that also has something to retrieve something I can use as a check. For example, the System Date and Time is always displayed and the user cannot remove it (thus invalidating my check). Thanks! Aarti
"Nick" wrote:
Aarti wrote:
Nick I will try this out. I have not done any API-related stuff in .NET as yet, but will give this a shot. Seems to make sense! Thanks again for your help. I'll post back with the trial results! Aarti
"Nick" wrote: >On another note, I cannot believe how difficult it is to determine whether or not the icon tray or windows taskbar was loaded!!
I got an idea for that. Find out the class name or the window name of the tray icon. You can try to do that using GetWindowText API call, you only need to find this out once, not everytime at runtime.
Now whenever your service starts you can call FindWindow with window text you found out previously as the windowName parameter. If the window is found then the tray icon exists, if not then it didnt load yet. =) All controls including the tray icon are technical ly windows as the OS sees it.
If you need further info or clarification, post here. If you cant find the class name or window name to call find window, post the name and version of the application that loads the tray icon (ad-watch right? I even think I got it installed) I can probably get it for you. A few days ago I made a litle program that gets the handles of any window from mouse clicks, I think it would be fairly easy to expand to get some more info about the window.
Good luck. Nick Z.
Here is something to help you out. First include System.Runtime. InteropServices
//then import the function that you need using interop [DllImport("user 32.dll",EntryPo int="SendMessag e")] public static extern int SendMessage(Int Ptr h_Window,uint message,uint param1,uint param2);
The uint values you can get from winuser.h which is included with the platform sdk. By the way I tried sending the message to the desktop window it does nothing. Anybody know which direction the numlock key down event gets sent?
I was thinking of getting the class name of the tray app. I just cant find anything that would give me such info.
I'll give it a deeper look.
Nick Z.
Aarti wrote: Nick It appears that the System tray window does not have a title. From what I could gather, the title it returns in GetWindowText is what appears in the title bar and since there is none for the system tray, it returns nothing. I am thinking of perhaps using GetDesktopWindo w as a proxy, although I don't know if that is too much better than checking for whether explorer.exe is running. I read that there is some other Shell dll to manipulate icons in the system tray .. perhaps that also has something to retrieve something I can use as a check. For example, the System Date and Time is always displayed and the user cannot remove it (thus invalidating my check). Thanks! Aarti
"Nick" wrote:
Aarti wrote:
Nick I will try this out. I have not done any API-related stuff in .NET as yet, but will give this a shot. Seems to make sense! Thanks again for your help. I'll post back with the trial results! Aarti
"Nick" wrote: >On another note, I cannot believe how difficult it is to determine whether or not the icon tray or windows taskbar was loaded!!
I got an idea for that. Find out the class name or the window name of the tray icon. You can try to do that using GetWindowText API call, you only need to find this out once, not everytime at runtime.
Now whenever your service starts you can call FindWindow with window text you found out previously as the windowName parameter. If the window is found then the tray icon exists, if not then it didnt load yet. =) All controls including the tray icon are technical ly windows as the OS sees it.
If you need further info or clarification, post here. If you cant find the class name or window name to call find window, post the name and version of the application that loads the tray icon (ad-watch right? I even think I got it installed) I can probably get it for you. A few days ago I made a litle program that gets the handles of any window from mouse clicks, I think it would be fairly easy to expand to get some more info about the window.
Good luck. Nick Z.
Here is something to help you out. First include System.Runtime. InteropServices
//then import the function that you need using interop [DllImport("user 32.dll",EntryPo int="SendMessag e")] public static extern int SendMessage(Int Ptr h_Window,uint message,uint param1,uint param2);
The uint values you can get from winuser.h which is included with the platform sdk. By the way I tried sending the message to the desktop window it does nothing. Anybody know which direction the numlock key down event gets sent?
Got it to work.
Here is the code that tests whether the icon tray window is loaded. =)
First interop the only funciton we need, FindWindow, like so:
[DllImport("user 32.dll",EntryPo int="FindWindow Ex")]
public static extern IntPtr FindWindowEx(In tPtr parentToLookFro m,IntPtr childToLookFrom ,string className,IntPt r windowName);
Now whenever you need to check if the icon tray is loaded do this.
//find the handle to the whole taskbar
//First argument is the handle to desktop
//second is equal to null
//third is taskbar class name
//fourth is window name as null
IntPtr trayWindowHandl e = FindWindowEx(In tPtr.Zero,IntPt r.Zero,"Shell_T rayWnd",IntPtr. Zero);
//find the handle to the specific part of taskbar
//first argument is handle to the taskbar
//second is null
//third is the class name of icon tray
//fourth is null
IntPtr notifyWindowHan dle = FindWindowEx(tr ayWindowHandle, IntPtr.Zero,"Tr ayNotifyWnd",In tPtr.Zero);
//test if the handle was found
if(notifyWindow Handle.ToInt32( ) != 0)
{
//then icon tray exists
}
else
{
//it doesnt
}
Hope this helps.
Nick Z. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Kenneth Courville |
last post by:
Hello,
I have an application that will be logging to a SQL Server 2000
database user user activity from several Windows 2003 terminal
servers. This information will be retrieved by monitoring the
Security logs of these servers (this part I know how to accomplish
already).
A table in the database, tblLogEntries, will contain the following
fields:
|
by: cerilocke |
last post by:
I have an identical SQL database on two machines (my machine and a web
server) that links to a database on a third server (S3). When I execute
a stored procedure on my machine that accesses a database on S3, it
always runs without a problem. However, when I run the same stored
procedure on the webserver (via Query Analyzer on my machine, connected
to the webserver), the stored procedure runs without a problem for
about ten minutes, and...
|
by: NWx |
last post by:
Hi,
I have the following question:
I have an app that uses user login/logout to identify users
When user logon, I register logon time in a session variable
When user logoff using the logout link, I also register logoff time and
update accordingly the logging history table.
However user can simply close the browser without logoff, so his session was
closed.
|
by: Bruno Mendonça |
last post by:
My boss asked me to build a program to create a report
with logon/logoff events for all users within our windows
domain. I'm using .Net to do so and decided to have a
program running on the Domain Controler to listen to all
new events and send them to a database. This last part I
haven't worked on yet, cause I can't tell which events are
relevant to write to the db and which are not.
I have managed to listen to all the security log...
|
by: Raimund Taschler |
last post by:
Hi all,
I'm implementing a windows service (running with local system account) which
has to do some tasks when users logon/logoff. How can the service register
to those logon/logoff-events? I'm using .NET 1.1 and C# and the service will
be installed on machines with os >= W2K.
If you have any solution or hint tracking down this problem please let me
know!
| |
by: Eduard Witteveen |
last post by:
Dear list,
In the corporation, we want to know who is using which computer, and do
some phone routing based upon this information.
When a user log's uses it's account to login, the phone on the desk
should also ring on the same desk. When the user leave's / sleeping, we
dont want the phone to ring, so we want to detect this also.
By using a service i can keep a program running, without irritating the
users with icon's / message's at...
|
by: gewe |
last post by:
Can anyone tell me what is wrong with the following code?
The code has to watch for logon/logoff events:
public void StartListenLogon()
{
EventHandler handler = new EventHandler();
WqlEventQuery query = new WqlEventQuery();
query.EventClassName = "__InstanceOperationEvent";
query.WithinInterval = new TimeSpan(0, 0, 3);
|
by: sam |
last post by:
I have a vb.net app that runs without a visible window. When the user
logs off or attempts a shutdown, the app is not quitting and prevents
the logoff/shutdown until the task is killed.
Is there a method to detect these system events and cause my app to quit
gracefully?
|
by: Robert Scheer |
last post by:
Hi.
I need to write a service that writes to a database the time of the
logon and logoff of the users. Our users logs on Active Directory and
some can also log as local administrators. The majority of the
workstations are Windows 2000 and some are Windows XP. I am reading
about WMI but have found some limitations when reading the WMI
structures, mainly with Windows 2000.
What is the best way to read logon/logoff information in this...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |