First, I want to apologize, that I´m asking about quite freqently
discused topic, but I was searching two days on the internet and I
didn´t find anything, what would solve my problem.
I´m
programming windows service, which would collect info about all started
windows (concretly window titles). I was trying to pass it via this code
(I have it on thread):
C#
while (true)
{
Thread.Sleep(1000); //Because of high CPU.foreach (Process currentProcess in Process.GetProcesses())
{
if (currentProcess.MainWindowTitle.Contains("Something"))
{
//Window has been found.
}
}
}
Normally
(in console app.) it works perfectly. But when I put it into my win
service, nothing happen. So I got a list (saved to the text file, by
the same way like in the code up) of all "MainWindowHandle"s and started
it like a service. All values were on zero. Then I tried API as well:
C#
//Dll imports:
[DllImport("user32.dll", EntryPoint = "FindWindow", CharSet = CharSet.Ansi)]
privatestaticextern IntPtr FindWindow(string className, string windowName);
[DllImport("user32.dll", EntryPoint = "GetWindowText", CharSet = CharSet.Ansi)]
privatestaticexternboolGetWindowText(IntPtr hWnd, [OutAttribute()] StringBuilder strNewWindowName, Int32 maxCharCount);
//And then:while (true)
{
Thread.Sleep(1000); //Because of high CPU.
FileStream stream = new FileStream(@"some path\info.txt", FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter writer = new StreamWriter(stream);
StringBuilder sbWinText = new StringBuilder(256);
GetWindowText(FindWindow(null, "Some text in title"), sbWinText, 256);
writer.WriteLine(sbWinText.ToString());
writer.Close();
}
The txt was empty and when I tried to get the handle (by "FindWindow"), I got
zero again. It looks, that my servis doesn´t see the main window of the
processes at all.
Does anybody know how to solve it?
Thanks
for replies.
6 Answers
Know the answer? Post it. Somebody with the same question will find it here.
PJ MartinsRank #2,548 · 548 reputationPosted Jun 5, 2010, 5:47 AM EDT
0
The problem is that a windows service does not run in the same login session as the computer is currently in, ie. if you are logged in it does not run in the same session you are, it creates its own, this allows services to run even when nobody is logged in. Due to this, the only processes it will see are any processes it has started itself. What are you trying to accomplish?
Ondrej Hruby7 reputationPosted Jun 5, 2010, 10:36 AM EDT
0
I´m working on service for our administrator on my high school. He wants to have overview on the LAN about programs.
But I can see processes (the names) via my service. I made a log with all running processes. My service normally see the processes, but it can´t see no window which should run under some process. No handle of that window and no title etc.
So it is possible to share the control between sessions?
Do you need to use a service? You could write a small application that launches after a user logs in and runs in the background without the user knowing, have it run on a timer which periodically checks all windows and sends the data you need.
Ondrej Hruby7 reputationPosted Jun 6, 2010, 12:54 AM EDT
0
I wanted to do it like you´re saying first, but he said me that he wants it like a service. What do you think is more elegant with the view of administrator? Do it like small program or like a service?
PJ MartinsRank #2,548 · 548 reputationPosted Jun 6, 2010, 7:54 AM EDT
0
You're only option will be an application running in the background. Create a console application and set it's output type (Project properties) to Windows Forms to avoid any window from being displayed. Then use a System.Threading.Timer to periodically check for running processes. That's the closest you'd get to a service.
PJ MartinsRank #2,548 · 548 reputationPosted Jun 5, 2010, 5:47 AM EDT
Ondrej Hruby7 reputationPosted Jun 5, 2010, 10:36 AM EDT
But I can see processes (the names) via my service. I made a log with all running processes. My service normally see the processes, but it can´t see no window which should run under some process. No handle of that window and no title etc.
So it is possible to share the control between sessions?
Thanks
PJ MartinsRank #2,548 · 548 reputationPosted Jun 5, 2010, 3:12 PM EDT
Ondrej Hruby7 reputationPosted Jun 6, 2010, 12:54 AM EDT
PJ MartinsRank #2,548 · 548 reputationPosted Jun 6, 2010, 7:54 AM EDT
Ondrej Hruby7 reputationPosted Jun 6, 2010, 10:59 AM EDT