Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C# Windows service API
I have a code part like this in my project :
private void GetActiveWindow()
        {
           
            const int cChars = 256;
            int ihandler = 0;
            StringBuilder sBuff = new StringBuilder(cChars);
            ihandler = GetForegroundWindow();
              StreamWriter sw;
            if (File.Exists("C:\\Log.txt"))
            {
                sw = File.AppendText("C:\\Log.txt");
                sw.WriteLine(sBuff.ToString() + "->" + ihandler.ToString());
                sw.WriteLine("-----------");
                sw.Close();
            }
            else
            {
                sw = new StreamWriter("C:\\Log.txt");
                sw.WriteLine(sBuff.ToString() + "->" + ihandler.ToString());
                sw.WriteLine("-----------");
                sw.Close();
            }
}
It's value always getting 0 and i cant reach active window's name.Iam using Windows 7.And this is a windows service project what should i do ? ihandler always getting 0 so i cant reach name.
Posted 27-Oct-11 3:46am
Edited 27-Oct-11 3:59am
v2
Comments
phil.o at 27-Oct-11 8:49am
   
It's value always getting 0
Which value ?
 
And can you show us what is the GetForegroundWindow() method ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

I don't think that should ever work when running as a windows service. To start with, services in 7 can't interact with the desktop at all. In addition to that they can run regardless of how many users are logged in, so what should it return if there are multiple users logged in (or none)? Which session should it return the foreground window for?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Debug your GetForegroundWindow Method.
 
Here is a information on "How to Debug Windows Service Application".
http://msdn.microsoft.com/en-us/library/7a50syb3%28v=VS.90%29.aspx
  Permalink  
Comments
SercanOzdemir at 27-Oct-11 9:02am
   
Sorry but Debug can not help me.Need Active window's name so i should learn about windows services and Windows 7. I tried and it works on win forms well.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

You can not do what you want. Under Windows 7 all services run under a different Session (session 0) than the user Session (session 1 and above). That means that your service code cannot interact with the users desktop at all. It's no surprise you keep getting 0 returned by GetForegroundWindow. There is no foreground window in Session 0 because there's no UI being displayed.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Impersonate the logged in user.
  Permalink  
Comments
Marcus Kramer at 19-Oct-12 15:36pm
   
Please don't reopen really old questions like this. The common guideline, although not fixed in stone, is around 5 or 6 monthts. Cheers and welcome to CP.

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

  Print Answers RSS
0 OriginalGriff 215
1 George Jonsson 175
2 Kornfeld Eliyahu Peter 159
3 PIEBALDconsult 110
4 Richard MacCutchan 85
0 OriginalGriff 6,080
1 DamithSL 4,648
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,624
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 19 Oct 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100