如何使用 PostThreadMessage 从 C++ 关闭 Internet Explorer
我正在尝试启动 iexplore.exe
让它运行 5 秒钟,然后再次关闭它。
iexplore
打开得很好,但是当我调用 PostThreadMessage 时它没有关闭。 谁能看到我做错了什么吗? 这是我的代码:
CString IEPath = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";//GetIEPath();
//IEPath += ' ' + url;
std::string strCommand((LPCTSTR)IEPath);
PROCESS_INFORMATION procinfo;
STARTUPINFO startupinfo;
GetStartupInfo(&startupinfo);
CreateProcess(
NULL,
(char *)strCommand.c_str(),// name of executable module
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
false, // handle inheritance option
CREATE_SHARED_WOW_VDM, // creation flags
NULL, // new environment block
NULL, // current directory name
&startupinfo, // startup information
&procinfo // process information
);
Sleep(5000);
::PostThreadMessage(procinfo.dwThreadId, WM_QUIT, 0, 0); //<---Dosent Close internet explorer!
有人知道我做错了什么吗? 或者有更好的方法来做到这一点吗?
I'm trying to start iexplore.exe
let it run for 5 seconds and then close it again.
iexplore
opens just fine however it doesn't close when I call the PostThreadMessage.
Can anyone see what I'm doing wrong? Here is my code:
CString IEPath = "C:\\Program Files\\Internet Explorer\\IEXPLORE.EXE";//GetIEPath();
//IEPath += ' ' + url;
std::string strCommand((LPCTSTR)IEPath);
PROCESS_INFORMATION procinfo;
STARTUPINFO startupinfo;
GetStartupInfo(&startupinfo);
CreateProcess(
NULL,
(char *)strCommand.c_str(),// name of executable module
NULL, // lpProcessAttributes
NULL, // lpThreadAttributes
false, // handle inheritance option
CREATE_SHARED_WOW_VDM, // creation flags
NULL, // new environment block
NULL, // current directory name
&startupinfo, // startup information
&procinfo // process information
);
Sleep(5000);
::PostThreadMessage(procinfo.dwThreadId, WM_QUIT, 0, 0); //<---Dosent Close internet explorer!
Anyone have an idea of what I'm doing wrong? Or is there better way what to do the trick?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果你可以枚举桌面上的窗口并向 IE 窗口发送 WM_CLOSE ,它可能会起作用..你可以使用间谍程序来获取 IE 窗口的窗口类
if you can enumerate the windows on the desktop and send a WM_CLOSE to the IE window , it might work .. you can use the spy programme to get the window class of the IE window
PostThreadMessage 调用的返回值是什么? 这可能会提供线索。
What is the return value from the PostThreadMessage call? That might give a clue.
对我来说,这很完美:
For me, this works perfect:
尝试将 WM_CLOSE 发送到主(顶层)窗口。 这相当于正常的 Alt-F4 退出。
Try sending WM_CLOSE to the main (top-evel) window. That's equivalent to the normal Alt-F4 exit.
我没有具体回答为什么 PostThreadMessage 不起作用。 但是,如果你详细说明为什么要这样做,也许有更好的解决方案?
例如,如果您只想显示网页 5 秒钟,则可以使用嵌入式 Internet Explorer ActiveX 控件创建并显示您自己的窗口。 您还可以添加一个接收器来检测 ActiveX 控件中何时加载网页,以便仅在加载并显示网页后才启动 5 秒计数器。
I don't have an answer specifically why PostThreadMessage didn't work. But, perhaps if you elaborate on why you want to do this, there is a better solution?
For example, if you just want to show a web page for 5 seconds, you can create and show your own window with an embedded Internet Explorer ActiveX control. You'll be also able to add a sink to detect when the web page is loaded in the ActiveX control, so that you start your 5-second counter only after the web page is loaded and displayed.
我最终对窗口进行了枚举(正如你们提到的我应该做的那样)
我的灵感来自 http://simplesamples.info/Windows/EnumWindows.php。
I ended up doing an enumeration of the windows (As serval of you mentioned i should do)
I was inspired of http://simplesamples.info/Windows/EnumWindows.php.
不要使用 PostThreadMessage(),它将消息发送到目标进程中的特定线程,而不是进程的 Windows 消息泵。 请改用 PostMessage() 或 SendMessage(),将消息放入目标进程的 Windows 消息泵中——这正是您想要的。
Don't use PostThreadMessage(), which sends a message to a specific thread in the target process instead of the process' Windows Message Pump. Use PostMessage() or SendMessage() instead, which place the message in the target process's Windows Message Pump -- which is exactly what you want.
不,永远不要使用 SendMessage() (基本 win32 规则)
不要使用 EnmWindows() (可怕)
No, never use SendMessage() (basic win32 rule)
Don't use EnmWindows() (horrible)