为什么这个程序有时会崩溃,有时不会?
以下程序关闭显示器。当我运行它时,它有时会崩溃,有时则不会。反汇编仅指向一个随机位置,如0x00011000
,并且没有真实信息。
如果我重新编译该程序并运行它,则重新编译的版本在我测试它时运行良好。但下次我真正需要使用它时,它再次崩溃,我需要重新编译它......让我后悔一开始就这么做了。
我不知道如何一致地重现该错误。(也就是说,除了在我最需要它时运行它并看着它崩溃之外。)
什么可能导致这个程序随机崩溃?
#include <Windows.h>
#include <tchar.h>
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
return SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2);
}
我使用的是 Windows 7 x64,并将其编译为 32 位程序。我相信我已经在 64 位上尝试过同样的事情并收到了相同的结果,尽管我不能 100% 确定。
编辑1:
如果有人真正复制了此内容,请发表评论并让我知道,我很好奇其他人是否可以复制此内容。
我目前正在自己测试一个稍微精简的版本(不依赖于 C 运行时):
#include
; #pragma comment(链接器,“/NoDefaultLib”) #pragma comment(链接器,“/Entry:mainCRTStartup”) #pragma comment(链接器,“/子系统:Windows”) int mainCRTStartup() { 返回SendMessageW(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, 2); } /* 该程序的 Base64 版本,如果您想使用它: TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAwAAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5vdCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAAABkN3fRfG zjEXxs4xF8bOMhv7ujEbxs4xF8bKMRPGzjIb+0IxE8bOMhv7pjETxs4xSAWNoRfGzjAAAAAAAAAAAUEUAAEwBAQBYIgROAAAAAAAAAADgAA8BCwEHCgACAAAAAAAAAAAAAAgQAAAAEAAAACAAAAAAQAAAEAAAAAIAAAQAAAAAAAAAAAAAAA啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊AudGV4d啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊…… */ 目前,这个版本可以工作......但是话又说回来,当我重新编译它时,前一个版本也可以工作。如果结果证明这个没有崩溃,我会将其发布在这里。
如果你想重现这个,我的建议是:编译程序,让它腌制一两天。 :) 当您使用计算机一段时间后,请尝试运行该程序几次...当我尝试这样做时,我通常会收到错误,直到我重新编译该程序。< /p>
编辑 2:
由于某种原因,每当你想向人们展示一个问题时,它就会神奇地得到解决。这里的情况也是如此。我将继续尝试重现该错误,但目前,它似乎工作正常。 :\(我怀疑这可能是因为安装了Windows 7 SP1,但我真的很怀疑......如果我发现我会在这里发帖。)
对此大家感到抱歉......
编辑3:
好的..事实上,每当您需要重现错误时,您都无法做到。 :|
然而,至少我发现了一些其他的东西:似乎将消息发送到的正确窗口是GetShellWindow()
返回的窗口。希望这对其他人有用。
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+0IxE8bOMhv7pjETxs4xSaWNoRfGzjAAAAAAAAAAAUEUAAEwBAQBYIgROAAAAAAAAAADgAA8BCwEHCgACAAAAAAAAAAAAAAgQAAAAEAAAACAAAAAAQAAAEAAAAAIAAAQAAAAAAAAABAAAAAAAAAAAIAAAAAIAAAAAAAACAAAEAAAQAAAQAAAAABAAABAAAAAAAAAQAAAAAAAAAAAAAAAoEAAAKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAudGV4dAAAAHQAAAAAEAAAAAIAAAACAAAAAAAAAAAAAAAAAAAgAABgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABYEAAAAAAAAGoCaHDxAABoEgEAAGj//wAA/xUAEEAA99gbwPfYw8zMUBAAAAAAAAAAAAAAaBAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAFgQAAAAAAAAQgJTZW5kTWVzc2FnZVcAAFVTRVIzMi5kbGwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA== */
Currently, this version works... but then again, so does the previous one, when I 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
也许您已经看到了这篇文章的要点Windows大神Raymond Chen的观点是,不建议以这种方式使用HWND_BROADCAST。我通过评论找到了这个该网站不应被命名,大意是您在这里使用的示例代码虽然很受欢迎,但并不是处理显示器断电的正确方法。
这并不能解释为什么您所看到的正是您所看到的,但它确实提供了一些证据表明相关代码是可疑的。
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.
配置您的病毒扫描程序,让应用程序保持独立而不干扰它。
您的应用程序与 邪恶的 winmain 太接近,无法忽略此处的病毒扫描程序。
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.
唯一有意义的事情。你所说的如果你重新编译它就可以工作的说法是,有些东西正在对exe进行哈希处理并以某种方式干扰它。百分之九十九的东西都是病毒扫描程序 - 但你说你没有?
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?