Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • If anyone actually reproduces this, please post a comment and let me know, I'm curious if others can reproduce this.

  • I'm currently testing a slightly more trimmed-down version myself (which doesn't depend on the C runtime):

    #include <Windows.h>
    #pragma comment(linker, "/NoDefaultLib")
    #pragma comment(linker, "/Entry:mainCRTStartup")
    #pragma comment(linker, "/Subsystem:Windows")
    
    int mainCRTStartup()
    {
        return SendMessageW(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
    }
    
    /*
    Base64 version of this program, in case you want to use it:
    TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAABkN3fRfGzjEXxs4xF8bOMhv7ujEbxs4xF8bKMRPGzjIb+0IxE8bOMhv7pjETxs4xSaWNoRfGzjAAAAAAAAAAAUEUAAEwBAQBYIgROAAAAAAAAAADgAA8BCwEHCgACAAAAAAAAAAAAAAgQAAAAEAAAACAAAAAAQAAAEAAAAAIAAAQAAAAAAAAABAAAAAAA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recompile it. If it turns out that this one doesn't crash, I'll post it here.

  • If you'd like to reproduce this, here's my suggestion: Compile the program, let it marinate for a day or two. :) When you've used the computer for a while, try running the program a couple of times... when I try that, I usually get an error, until I recompile the program afresh.


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.

like image 479
user541686 Avatar asked Jun 24 '11 04:06

user541686


1 Answers

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.

like image 141
Steve Townsend Avatar answered Nov 03 '22 14:11

Steve Townsend