user541686
user541686

Reputation: 210613

Why does this program sometimes crash, and sometimes not?

The following program turns off the monitor. It sometimes crashes when I run it, and it sometimes doesn't. The disassembly just points to a random location like 0x00011000, and has no real information.

If I recompile the program and run it, the recompiled version runs fine while I'm testing it. But the next time I really need to use it, it crashes again, and I need to recompile it... making me regret doing this in the first place.

I have no idea how to consistently reproduce the error. (That is, other than running it when I need it most desperately and watching it crash.)

What could be causing a random crash in this program?

#include <Windows.h>
#include <tchar.h>

int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
    return SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}

I'm using Windows 7 x64, and compiling this as a 32-bit program. I believe I've tried the same thing with 64-bit and received the same result, though I'm not 100% certain.


Edit 1:


Edit 2:

For some reason, whenever you want to show people a problem, it magically gets solved. Ditto the case here. I'll keep on trying to reproduce the error, but at the moment, it seems to be working fine. :\ (I have a suspicion that it might be because of installing Windows 7 SP1, but I really doubt it... if I find out I'll post here.)

Sorry about this everyone...


Edit 3:

Okay... as it happens, whenever you need to reproduce a bug, you can't. :|

However, at least I found something else: it seems that the correct window to send the message to is the window returned by GetShellWindow(). Hopefully that'll be useful for someone else.

Upvotes: 6

Views: 1037

Answers (3)

Blip
Blip

Reputation: 46

The only thing that makes any sense re. your statement that it works if you recompile it is that something is hashing the exe and interfering with it in some way. That something would 99 times out of a 100 be a virus scanner - but you say you don't have one?

  1. You build the exe. It has a hash of 0x1234.
  2. You run it. The virus scanner hashes it, allows it to run, then decides that it is doing something it doesn't like (such as broadcasting shutdown messages to all windows).
  3. You run it again. The virus scanner says "look that 0x1234 is starting up again; I'm going to patch it to stop it broadcasting shutdown messages, or maybe I'll just stop it running at all".
  4. You rebuild the exe. It has a new hash of 0x4321 (the hash changes solely because it has a new creation/modified date).
  5. Go back to 2.

Upvotes: 0

Steve Townsend
Steve Townsend

Reputation: 54168

Perhaps you've already seen this but the gist of this article by Windows god Raymond Chen is that using HWND_BROADCAST in this way is not recommended. I found this via comments on a site that shall not be named, to the effect that the sample code you are using here, while popular, is not the right way to handle monitor powerdown.

This does not explain why you are seeing exactly what you are seeing, but it does offer some evidence that the code in question is suspect.

Upvotes: 2

Stefan
Stefan

Reputation: 43575

Configure your virus scanner to leave the app alone and not interfere with it.

Your app is too close to the evil winmain to ignore virus scanners here.

Upvotes: 0

Related Questions