作者e1090128 (肥龟)
看板C_and_CPP
标题[问题] SetConsoleCtrlHandler无法使用
时间Tue Nov 17 15:17:59 2015
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
VC++
额外使用到的函数库(Library Used): (Ex: OpenGL, ...)
OpenCV
问题(Question):
SetConsoleCtrlHandler在整段的程式中没有效果,我想要使其产生CTRL+BREAK的效果
但其没有接收到
喂入的资料(Input):
NO
预期的正确结果(Expected Output):
Console会被关掉
错误结果(Wrong Output):
Console并未接收到讯号,而无法被关掉
程式码(Code):(请善用置底文网页, 记得排版)
#include "stdafx.h"
#include <string>
#include <stdio.h>
#include <windows.h>
#include <iostream>
#include <WinBase.h>
wchar_t fileName[] = {L"C:\\Windows\\System32\\cmd.exe"};
wchar_t CmdString[] = {L" /c node test.js"};
wchar_t CmdString1[] = {L"C:\\Windows\\System32\\cmd.exe /c node 0.js"};
wchar_t CmdString2[] = {L"C:\\Windows\\System32\\cmd.exe /c node 2.js"};
private: System::Void button1_Click(System::Object^ sender,
System::EventArgs^ e) {
BOOL bSuccess;
STARTUPINFO StartInfo;
PROCESS_INFORMATION ProcInfo;
HANDLE hNewProc, hNewThread;
DWORD dwProcID, dwThreadID;
//Initial STARTUPINFO
StartInfo.cb = sizeof(STARTUPINFO);
StartInfo.lpReserved = NULL;
StartInfo.lpDesktop = NULL;
StartInfo.lpTitle = NULL;
StartInfo.cbReserved2 = 0;
StartInfo.lpReserved2 = NULL;
//Initial PROCESS_INFORMATION
ProcInfo.hProcess = hNewProc;
ProcInfo.hThread = hNewThread;
ProcInfo.dwProcessId = dwProcID;
ProcInfo.dwThreadId = dwThreadID;
bSuccess = CreateProcess(
NULL, // name of executable module
CmdString1, // command line string
NULL, // SD
NULL, // SD
true, // handle inheritance option
CREATE_NEW_PROCESS_GROUP, // creation flags
NULL, // new environment block
NULL, // current directory name
&StartInfo, // startup information
&ProcInfo // process information
);
SetConsoleCtrlHandler(CTRL_C_EVENT,FALSE);
}
补充说明(Supplement):
我主要是要把新跳出来的执行视窗关闭,如果有其他的方法可以关掉的话
请大家救救我
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.117.192.66
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/C_and_CPP/M.1447744681.A.E54.html
1F:推 stupid0319: ShowWindow(hwnd,SW_HIDE); 隐藏起来,眼不见为净 11/17 15:27
2F:→ e1090128: %不行喔,因为要连续开很多个,不关掉的话会记忆体暴增 11/17 15:47
3F:→ hichcock: CreateProcess => TerminateProcess 11/17 16:08
4F:→ hichcock: Cmd => taskkill 11/17 16:09
5F:→ hichcock: SendMessage => exit 11/17 16:09
6F:→ hichcock: 方法很多, 选一个用吧 11/17 16:09
7F:→ e1090128: TerminateProcess(PROCESS_ALL_ACCESS,1)这样用法对吗 11/17 16:37