作者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://www.ptt.cc/bbs/C_and_CPP/M.1447744681.A.E54.html
推 stupid0319: ShowWindow(hwnd,SW_HIDE); 隱藏起來,眼不見為淨 11/17 15:27
→ e1090128: %不行喔,因為要連續開很多個,不關掉的話會記憶體暴增 11/17 15:47
→ hichcock: CreateProcess => TerminateProcess 11/17 16:08
→ hichcock: Cmd => taskkill 11/17 16:09
→ hichcock: SendMessage => exit 11/17 16:09
→ hichcock: 方法很多, 選一個用吧 11/17 16:09
→ e1090128: TerminateProcess(PROCESS_ALL_ACCESS,1)這樣用法對嗎 11/17 16:37