0

I have a code part like this in my project :

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.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Sercan Ozdemir
  • 4,641
  • 3
  • 34
  • 64

3 Answers3

3

In a Windows Service there is no UI, no logged in user and no active Window.

The Windows Service starts when the system starts (if configured as automatic) and no user is logged in so it makes no sense to try to get the Active or ForeGroundWindow.

you should fix your design and avoid usage of any window because you cannot imagine or assume any available and active.

Edit: this could have been working on machines up to Windows XP as the change of having services running in their own Windows Session was done in Vista/Win7.

still, even if it worked in Win2000 or WinXP, was a bad design.

Davide Piras
  • 43,984
  • 10
  • 98
  • 147
1

The service runs in a separate session, far away from the interactive session with the desktop. In the service session there is no such thing as a foreground window and thus it returns 0.

Anders Abel
  • 67,989
  • 17
  • 150
  • 217
  • So With Windows Service can not i reach active window ' s text ? – Sercan Ozdemir Oct 27 '11 at 13:17
  • Windows services run in the background, outside the context of a desktop session. It might just be possible to do it but it has all sorts of security issues and pitfalls. You'd be better off finding an alternate approach - see my answer for one suggestion. – Stephen Kennedy Oct 27 '11 at 13:22
  • As David pointed out, starting with Vista and 2003 Server you are no longer able to interact with the desktop (or create a window on your own). That is specifically forbidden since these versions of windows. – Mario The Spoon Oct 27 '11 at 13:25
0

You can attempt to allow the service to interact with the Desktop. I am not 100% sure if this will solve your issue, but it is worth a try.

In your service properties window (services -> right-click -> properties), go to the log On tab and check "Allow service to interact with desktop".

DAC
  • 1,769
  • 1
  • 20
  • 32
  • Sorry But i cant find anything like this :S – Sercan Ozdemir Oct 27 '11 at 13:27
  • From what I've read here and elsewhere it's not available post-Vista. – Stephen Kennedy Oct 27 '11 at 13:29
  • Although the option seems to be enabled (at least on my Win7) it appears to be almost useless with the new [Session 0 isolation feature](http://blogs.technet.com/voy/archive/2007/02/23/services-isolation-in-session-0-of-windows-vista-and-longhorn-server.aspx) – DAC Oct 27 '11 at 13:36