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.
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.