BOOL GenerateConsoleCtrlEvent(
DWORD dwCtrlEvent, // signal to generate
DWORD dwProcessGroupId // process group to get signal
)
这个不行吗?
DWORD dwCtrlEvent, // signal to generate
DWORD dwProcessGroupId // process group to get signal
)
这个不行吗?
This section shows an example of the SetConsoleCtrlHandler function that is used to install a control handler. When a CTRL+C signal is received, the control handler returns TRUE, indicating that it has handled the signal. Doing this prevents other control handlers from being called. When a CTRL_CLOSE_EVENT signal is received, the control handler returns TRUE, causing the system to display a dialog box that gives the user the choice of terminating the process and closing the console or allowing the process to continue execution. If the user chooses not to terminate the process, the system closes the console when the process finally terminates. When a CTRL+BREAK, CTRL_LOGOFF_EVENT, or CTRL_SHUTDOWN_EVENT signal is received, the control handler returns FALSE. Doing this causes the signal to be passed to the next control handler function. If no other control handlers have been registered or none of the registered handlers returns TRUE, the default handler will be used, resulting in the process being terminated. BOOL CtrlHandler(DWORD fdwCtrlType)
{
switch (fdwCtrlType)
{
// Handle the CTRL+C signal.
case CTRL_C_EVENT:
Beep(1000, 1000);
return TRUE;
// CTRL+CLOSE: confirm that the user wants to exit.
case CTRL_CLOSE_EVENT:
return TRUE;
// Pass other signals to the next handler.
case CTRL_BREAK_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
default:
return FALSE;
}
}
void main(void)
{
BOOL fSuccess;
fSuccess = SetConsoleCtrlHandler(
(PHANDLER_ROUTINE) CtrlHandler, // handler function
TRUE); // add to list
if (! fSuccess)
MyErrorExit("Could not set control handler");
}
keybd_event(VK_CTRL,0,0,0)
keybd_event(VK_C,0,0,0)
why??
原控制台肯定接受CTRL_C_EVENT消息.
TerminateProcess(pi.hProcess, 0)当然不用,因为此控制台有自己的数据需要保存啊
呵呵,兄弟:你不是也接管了这个console的hstdInput 吗? 使用WriteFile向这个标准输入写入Ctrl+C的ASCII码即可达到你的想往Console中输入Ctrl+c的目的;
示例代码如下:
char Buffer[] ="this is test data";
DWORD nBytesWrite = 0;
WriteFile( hstdInput , Buffer, lstrlen(Buffer), &nBytesWrite,NULL);
_flushall();
你将Buffer改为自己需要的值就可以了!
1、我的目的是要控制它“开”和“关”,同时我自己的程序一直运行着。我没有要“強制它結束”,我的意思就是想表现得“温和”些:)2、CTRL+C和TerminateProcess(pi.hProcess, 0)结果会一样吗?你又不知道那个控制台是否响应CTRL+C消息(事实上它收到CTRL+C时会有一系列动作,比如写文件等)。TerminateProcess(pi.hProcess, 0)会使它“主动”做这些动作吗?
谢谢各位的参与,尤其是thomas269(Thomas),我获易匪浅