What if two programs did this? Practical exam


A customer who doesn't read this blog had the following question:

The requirement for our application is that it must be on top of all other windows, even in the presence of other windows with the topmost style. In our WM_KILLFOCUS and WM_PAINT messages, we set ourselves to topmost, and then call SetWindowPos to bring ourselves to the front, and then we SetForegroundWindow ourselves just to make sure.

The result is that our application and another application end up fighting back and forth because both applications are applying similar logic to remain on top. There are other applications that defeat our logic, and they manage to cover our application. (These other applications are third party applications we have no control over.)

We are in the process of writing a DLL to hook all messages for all windows and [even crazier things that aren't worth elaborating on], but at the moment this causes the other hooked applciations to crash. We are also considering setting a timer and calling SetWindowPos when the timer fires, but that seems inefficient.

The customer didn't even have to run through the thought experiment What if two programs did this?; they got to run it in real life! And the result was as predicted: The two programs fight with each other to be the one that is on top of the other. Nobody wins; everybody loses, especially the user.

Comments (41)
  1. asdbsd says:

    So, what was their "question"? All that's quoted is the description of their misery :)

    I have a feeling that many of these "you cannot be special" cases arise from not distinguishing between program wishing to be special and user wishing that for some program. People might not want their program to be special for everyone, everywhere, but just in their domain where having the app is corporate policy.

    In other words, the correct design would be to allow every form of being special, but only if the user (or the group policy) asked for it. Just like with file format handlers, only allowing just one application to succeed.

    Well, in the most cases it's probably not worth the trouble though.

  2. Dan Bugglin says:

    @BrianG sounds like you're talking about the ISP that comes before NetOne and after NetNegativeOne.  Not to name names.

    Speaking of VMs, that would have been a novel way to defeat it.  Figure out how to get it running inside the VM, share the VM's internet connection with the host, then simply minmize the VM window.

  3. Dan Bugglin says:

    PS: Despite there technically being an infinite range of numbers between -1 and 1, if that comment violates the blog rules feel free to snip that part out Raymond.  I'm not sure if it does now.

  4. @Dan Bugglin says:

    Nope, that wasn't the ISP at all.  If they were providing the service, it was subcontracted to them via a different front, at least.

  5. Joshua Ganes says:

    @Brian G. – I used a similar piece of software for my first internet connection. My parents were too frugal to pay for internet services, so we installed the ad-laden software and gained very slow access to the internet. That software made my already-small, 14" monitor display even less working space. I recall that I found a sequence of processes to kill so that the ads went away, but the internet connection stayed alive.

    Why do so many people seem to think that their software is so special? It must be the same reason so many of the drivers on the road seem to think the whole road belongs to them.

  6. Brian G. says:

    @Dan Bugglin: nope that wasn't the ISP at all.  Well, it may have been the company that provided the service, but as a subcontractor for a different front.  But I have no evidence that was the case.

  7. Paul says:

    @Dan:

    Too late. I've already seen you name and shame NetNegativeZeroPointSixFourThreeTwoEightFourNine

    TRWTF is that the customer thought their app was that important; they should give the user the choice: Stay on top? True, False, FileNotFound; I have rearranged your z-order, pray I don't rearrangeit further; and the customer in this story should be banned from their mummy and daddy's modem.

  8. Michael Kohne says:

    Did you tell them that the right approach was to get out of the business? Obviously this kind of 'take over the user's machine' thinking is only going to lead them to more and more trouble, crashes, and irritated users. And the worst of it is, they'll probably not see but half of the trouble they cause.

  9. Skip says:

    As a related task, I wonder if there's anything I could do as a user to prevent apps from ever bringing themselves to focus over the app that I, the user, have chosen for focus.   Probably not, it most likely runs into the same arms race conditions.   But it's something that I wish the OS would allow/enforce.   System messages are one thing, but it used to annoy me to no end back when I was on dialup – I'd start a web page or four loading, or fire up office, then go back to editing code, to make use of the 3-4 minutes that I'd have before the pages would be loaded.   And invariably the browser would decide to bring itself to the front right as I was typing a backspace, causing me to waste another few minutes loading it again.

  10. Klimax says:

    Any hint at what type of app they were providing? Afterall special app should be from very special field.

    And then they should have fired whoever whrote that requirment…

  11. Klimax says:

    Any hint at what type of app they were providing? Afterall special app should be from very special field.

    And then they should have fired whoever wrote that requirment…

  12. Antonio Rodríguez says:

    When I find an application that keeps staying in my face or makes something as annoying as that, I uninstall it. Immediately. I wouldn't date to make a program that acts that way, because I respect my users.

    @Paul:

    Remember that, maybe, not everybody here reads The Daily Worse Than Failure. Just maybe ;-) .

  13. [useless comment]@Brian G.: I've seen that kind of thing done by restricting the working area (SystemParametersInfo w/ SPI_SETWORKAREA) to a smaller rectangle and then positioning the toolbar programmatically in the newly created "forbidden" zone.

    This is the same mechanism that allows the Start bar (which is actually a quite normal window) to stay where it stays without having other windows getting over it.[/useless comment]

  14. David Heffernan says:

    You've just described a very large portion of the requests for help on Stack Overflow!

  15. Brian G. says:

    @Matteo Italia: Ah, that makes sense.  I wasn't sufficiently familiar with the vocabulary to know what it's called, but had an intuitive feel for what it is.  What you describe fits exactly with what I recall experiencing.

  16. rs says:

    Even if the customer's question is paraphrased, it's hard to believe that there are people who seriously ask for such things.

  17. Drizzt says:

    There are a lot of situations where all this can be necessary, but you can smell the wrong situation by the fact that there are other apps broking the system.

    I mean, for my customers it's perfectly normal to have our apps always on top, or always in background, in one case even replacing shell, sometimes even moving by themeselves accross screens. But, in such situations, it's MY team that decide what's on those PC (plannig with the customers, obviously). There is no way another app can do whatever it like, for if it's doing so, it's not an app that should be on the machines.

    Well, or one of my collegues has gone drunk, but that's an entire different story.

  18. Klimax says:

    @Alex: How did you get there? Only fullscreen apps do it for me. (Both XP and Win 7) Or I just don't have such bloody programm installed… (what that then would be?)

  19. frymaster says:

    "In other words, the correct design would be to allow every form of being special, but only if the user (or the group policy) asked for it."

    the issue is that just about anything a user can specify (like "make this window special"), the app can specify on the user's behalf

  20. Crescens2k says:

    Alex: Windows does it by default these days, no setting needed. It is enforced by Windows that any focus related calls will only go through if it is coming from the application with focus.

    For the start menu, it is explorer being kind. Because lets face it, if it did that when you were using an application staying in front, it would be annoying. The obvious solution is to just not use the start menu for a few seconds after you have logged in.

  21. Brian G. says:

    I once had a program (back in Win95 days) which allowed me to connect to a free ISP.  (This was back in the days before I knew anything about GUI software development, so I'm going to try to reverse engineer it from memory).  It stayed at the bottom of the screen, moved anything that covered it under it (or perhaps just shoved them back up the screen so there was no layering issues to deal with), and displayed ads to pay for the service I was getting for free.  If I closed the app, it killed the network connection.  I guess that's the closest I've ever come to seeing an application which has a "legitimate" use for doing evil things to the customer's desktop.  Aside from being hugely annoying (and eating a huge chunk of real estate) while connected to the internet, it seemed to be well-behaved (didn't leave processes running all the time in the background while not in use, etc.).

    Actually, I don't know that I ever thought to put an always-on-top window over it.  *sigh*  If only I still had that sort of thing to experiment with (in a Win 98 VM, of course).

  22. Oh boy says:

    That reminds me of silverlight and how it every second (500ms i guess) calls SetPriorityClass with ABOVE_NORMAL_PRIORITY_CLASS

    I noticed this because it disrupted my other programs and when i tried to manually change the priority back (yes i DID search for a setting to change first, none exists) to normal it instantly changed the priority back to ABOVE_NORMAL_PRIORITY_CLASS.

    The funny and sad part is according to microsoft support this is "by design"

    Microsoft isn't even following its own damn rules.

    Example:

    1. The good citizens rule.

    2. Don't abuse API's that can/will affect the user and/or other programs on the same computer.

    the list goes on but you get my point..

    Microsoft as hypocritical as ever..

    Tells outsiders (non-microsoft devs) to follow their rules and microsoft doesnt even follow them themselves, <sarcasm> very professional </sarcasm>

    whats next spying on their paying users..oh right they do that already…hmm..what is lower that that..

    [Why are you telling me? Glass houses are great places to throw stones. -Raymond]
  23. Crescens2k says:

    @Oh boy

    What is the point in turning this into a bash against Microsoft? The teams are autonimous, so if one team puts out these rules, it doesn't mean another team has to follow them. Please, coming up with bashes and rants will do nothing but strengthen the case for turning off comments in these blog entries.

  24. Cheong says:

    I'd be grateful if there's a future version of SetForegroundWindow() can just be modified that, if centain user setting is set, it'll just blink the taskbar and do not steal focus. Or just like the "hide notification icon" function, the user can surpress even the blinking if the application is annoying enough.

    If that's the case, in this scenario you'll just have two (or more) blinking button on taskbar. The user is not losing.

    And with this feature, if some web browser is tricked to bypass scripting filter and popup infinate window, the user can claimly right click the button on taskbar can close all windows. (Not that they can't do now, but continuously popping up windows case panic to users, and they may just be stunned and cannot think what they could do)

  25. Ivo says:

    [Crescens2k – The teams are autonimous, so if one team puts out these rules, it doesn't mean another team has to follow them.]

    The teams are autonomous, yes. So Raymond's blog is not the place to bash those other teams. That's true.

    However if the Windows team puts out rules for developing Windows applications, of course the other teams at Microsoft should follow them. Just like any external developer is expected to.

  26. Crescens2k says:

    @Ivo

    Yes it is true, the other teams should be setting an example. But again, this isn't the place to discuss it. The correct places would be either with the bug reports or via direct communication with those teams. I have no issue with what is being said it is just the whole place of it being said.

  27. Alex says:

    @Skip: There is such a thing yes. Dunno about Windows, although I do remember a programs task bar entry flashing to signal that it wants something but not appearing on top. But on Linux I have exactly that setting, "focus stealing prevention". It has different levels, probably for the different types of calls windows can use to bring themselves on top.

    As an aside: One thing that has always driven me crazy in Windows, was the fact that the start menu would not stay on top if something else was doing something. Lets say during windows start up, something is still loading, but I can already use the start menu. Someone does some kind of focus call and my start menu vanishes. *ARGH* Same thing after start up. Some program wants to show me a message while I'm in the start menu and it vanishes. *ARGH*. Doesn't happen in KDE at least. Love that!

  28. Drak says:

    The funny thing is that it's "never right". Using a certain Microsoft Development Tool, I hate that it steals focus when I load a solution, but I'm glad is steals focus when it pops up for debugging. It wont ever be "right" until they make PCs that can read a user's mind :P

  29. Sam says:

    Hey, why not have a canned message to reply to such developers with say..

    "Dear Customer,

    If you are such a pompous ass and consider yourself above all mortals, why don't you go get a Mac already? Seriously, those were BUILT specifically for people like you."

  30. Pi says:

    @Oh boy

    Congratulations on making the same tired points that have already been adequately answered in the comments of the linked article from 5 years ago!

  31. asdbsd says:

    @frymaster: > the issue is that just about anything a user can specify (like "make this window special"), the app can specify on the user's behalf

    Not if you run shell with elevated privileges.

  32. Paul says:

    @Antonio Rodríguez

    I know, but a fair few will.

    Also, a comment like I made here would probably have been removed from tdwtf. :(

  33. Lawrence says:

    The scary part, and I don't think anyone has commented on it yet, is that the customer is describing *three* applications: 1. the one he's developing, 2. the one he's fighting with, and 3. 3rd party apps that 'defeat' him.

    The most likely interpretation is that app #2 was *also* developed by this customer, and is interfering with their development of app #1! So he's saying "we wrote an application that always stays on top, but now we want to write another one that always stays on top even of that".

    How this person didn't realize their peril *after developing both applications* is mind boggling!

  34. Oh boy says:

    @Crescens2k it's not bashing of any kind, i'm merely stating a fact, an observation (that i have seen over and over again and it have never been corrected)

    Don't make it into what its not.

    a bug report or via direct communication, will be ignored, i've already tried that.

    So i'm taking a page from the security researchers book, official discloser for all to see. To officially ask the questions to microsoft.

    Bad PR is the only thing microsoft seem to respond to, another example of bad professionalism (ignoring developers)

    "this isn't the place to discuss it"

    Since both outside developers and microsoft employees alike read this blog, it is actually a good place. Its open and read by both sides.

    Yes it is a bit off topic, i'm apologize for that.

    @Ivo

    You're on the right track.

    @Pi

    "adequately answered" no, it has not, but an official detailed answer about these practices (their doing and how and when they will correct them) would be nice.

    An answered question means people will stop asking the same question because they now have the answer.

    You think i'm asking a question that have been "adequately answered" before perhaps you can now see the mistake you did, think before you post, please.

    Come on people, these questions deserve to be answered and _corrected_ once and for all.

    Sigh why are whistleblower always getting the blame, not logical at all.

    [I think you've confused the words "effective" and "good." -Raymond]
  35. Gechurch says:

    @oh boy

    You seem to be very confused. On a lot of things.

    I'm not sure what makes you thing typing a comment to Raymond's blog post is "officially ask[ing] the questions". Particularly when the question is about Silverlight process priority, and the blog post is about apps wanting to be on top – totally unrelated. I'm also not sure what effect you think posting a comment here "for all to see" will have. The readers of Raymond's blog would have to be the set of people that are already most intimitely aware of standards and compatibility and best-practice for writing apps. Do you really think anyone here is going to be shocked and outraged that an app puts itself slightly higher in the process priority stakes?

    Googling for this issue, I found one blog post and one forum thread. So you are asking Microsoft to create "an official, detailed answer about these practices" for an issue about 10 people in cyberspace have commented on. Imagine if Microsoft did this for every issue a handful of people complained about! What a ridiculous request.

    For the record, I dislike the fact Silverlight does this. Presumably it does it because Silverlight gets used to stream high-quality video. People tend to be very fussy about wanting video to play smoothly, so the developers must have decided this was the best way to solve the issue. Other technologies (like Flash) get used for the same purpoe and don't need to increase priority, so this seems like a solvable problem without resorting to this blanket approach. If you are watching a video in Silverlight, I don't see this as being a big deal.

    Please stop with the cries of matrydom. Your initial post was a Microsoft bash. You were not simply stating fact in an impartial way. You were deliberately being emotive and aggressive. It is not the fact you pointed out an issue with Silverlight people are responding to, it's the childish way you have gone about it. As examples, see your use of sarcasm, use of language such as "hypocritical" and your unbased comment that Microsoft spies on it's customers (heck, it was even unexplained – presumably you are referring to the usage statistics that end-users have to opt-in to, and are completely anonymous). So no, you are not as you claim "taking a leaf out of the "security researchers book". More like the "Slashdot trolls book".

    Oh, and apologising for being off-topic in your follow-up post that continues to be off-topic is hardly sincere.

  36. Oh boy says:

    @Gechurch you're the one that's confused, i think you should go to your thinking place and redo it because you're not getting it.

    Did you miss my second post ?

    "I'm also not sure what effect you think posting a comment here "for all to see" will have."

    For one, making more people aware of the problems. To increase the pressure on microsoft.

    "The readers of Raymond's blog would have to be the set of people that are already most intimately aware of standards and compatibility and best-practice for writing apps."

    Well i could dream…

    "Presumably it does it because Silverlight gets used to stream high-quality video"

    Well in short: They have problems with their decoding efficiency.

    And instead of fixing it correctly they were lazy and worked around the issue.

    (quite common practice, sadly enough)

    Of course if they had done it correct the first time they wouldn't have to waste time fixing it twice. (Time is money and all that)

    I see i got you fired up and flustered for the comment about Microsoft spying on it's customers.

    Yeah sorry i couldn't resist, i was just having a bit of fun. People was sort of assuming / expecting something like that would come so i'd thought i would give it to them.

    I guess you just didn't see the obvious joke.

    No i don't troll (never), i just want microsoft to follow their own rules and be a good example to others. Is that so bad ?

    So can you see now, i actually want to help microsoft become better.

    My apology was sincere.

    Of course i have to answer people when i get attacked.

  37. 640k says:

    This looks like a contest in how to abuse the window system. And the loser asks how to do it more because he got defeated.

  38. Alex Cohn says:

    Is it only me that is perpetually defeated by Windows Update asking if I want to restart the system every time my half prepared e-mail requires the "R" key?

  39. david.tyler.hunt says:

    The foreground window battles are bad enough, but this insane behavior is also sometimes observed in people who are writing privileged services to do heavy lifting on behalf of user programs. The bad part comes in when they don't decide to design interfaces that are hard to use for evil, and then try fruitlessly to defend their client application from all the other evil processes that are happily trying to inject code into it. It frustrates me that when you tell them this is bad design, they get defensive.

    RE: Silverlight, I don't think 'above-normal' is a huge deal if Silverlight isn't doing any work.  But yeah, continually futzing with scheduling after the system has expressed its intent at you is less than cool.

  40. Timothy Byrd says:

    Klimax: "And then they should have fired whoever whrote that requirment…"

    No, "I bet somebody got a really nice bonus for that feature"

    ( blogs.msdn.com/…/922449.aspx )

  41. Klimax says:

    Unfortunately I know how Real World operates. That's why I used "should". (Heh, and I read linked post at that time.)

    Afterall TDWTF is good site to visit everyday…

Comments are closed.