BOOL GenerateConsoleCtrlEvent(
  DWORD dwCtrlEvent,       // signal to generate
  DWORD dwProcessGroupId   // process group to get signal
)
这个不行吗?

解决方案 »

  1.   

    Registering a Control Handler Function
    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"); 
    }
      

  2.   

    试试
    keybd_event(VK_CTRL,0,0,0)
    keybd_event(VK_C,0,0,0)
      

  3.   

    GenerateConsoleCtrlEvent(CTRL_C_EVENT,pi.ProcessId)返回0,GetLastError()=0
    why??
    原控制台肯定接受CTRL_C_EVENT消息.
      

  4.   

    to richen(苦行僧):那个控制台是个别人完成的EXE,我不是要写控制台,而是要控制他的行为,比如目的之一就是想隐藏该死的DOS-BOX(当然,这个简单)
      

  5.   

    這可能是因為這個console是基於CreateProcess產生的, 所以GenerateConsoleCtrlEvent不能到達它那裡, 這是因為每個process可以有他自己的一個console, 所以你CreateProcess的那個根本不是你的Process的
      

  6.   

    為何不用TerminateProcess(pi.hProcess, 0);來終止它呢
      

  7.   

    你为啥不用TerminateProcess()呢?,昨天我就想问。
      

  8.   

    1、刚才看了一下msdn,你看是不是应该在createProcess()时 指定CREATE_NEW_PROCESS_GROUP。2、如果这个还不行的话,你就先用CREATE_NEW_PROCESS_GROUP创建一个console ,然后在这个console 进程里面在启动那个程序,这样他们就在同一Process组中,而组id就是前一个进程id3、要你就是要关闭这个窗口的话,那就在创建那个进程时指定CREATE_NO_WINDOW,让他根本没有窗口不行吗????????
      

  9.   

    to thomas269(Thomas):有可能,但真是如此又该咋办好?
    TerminateProcess(pi.hProcess, 0)当然不用,因为此控制台有自己的数据需要保存啊
      

  10.   

    我始終有點不明, CreateProcess建立的過程它完結時不就結束了嗎, 你還想強制它結束, 但上面又說有数据要保存, 你CTRL+C, 這些数据也就不能保存了, 不是嗎?你既然是想取它的output, 自然就要它正常結束, 你要知的應該是它何時結束, 而不是怎樣令它結束. 要知道過程結束了沒有, 用GetExitCodeProcess(pi.hProcess, &code) == STILL_ALIVE, 如果它返回FALSE, 即数据已接收完了, 它也結束了, 那用煩它的結束!
      

  11.   


    呵呵,兄弟:你不是也接管了这个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改为自己需要的值就可以了!
      

  12.   

    to thomas269(Thomas):
    1、我的目的是要控制它“开”和“关”,同时我自己的程序一直运行着。我没有要“強制它結束”,我的意思就是想表现得“温和”些:)2、CTRL+C和TerminateProcess(pi.hProcess, 0)结果会一样吗?你又不知道那个控制台是否响应CTRL+C消息(事实上它收到CTRL+C时会有一系列动作,比如写文件等)。TerminateProcess(pi.hProcess, 0)会使它“主动”做这些动作吗?
      

  13.   

    to tpProgramer(tp编程者):“Ctrl+C的ASCII码”:是什么?
      

  14.   

    1. 如果你想温和些, 要視乎你要控制的Console Application的行為, 如果它是如Compiler的, 即只用參數作輸入, GetExitCodeProcess能夠很溫和的"等"它結束. 如果它在運行中途曾經和使用者互動, 你可以用WriteFile寫data to hStdInput, 來順著它本身的行為來結束, 如果它並不是以上兩種, 請說明, 或給個例子.2. CTRL+C和TerminateProcess結果普遍是一樣的. 但depence on它有沒有接管CTRL+C, CTRL+P,...之類的組合鍵(但通常都不會). 而TerminateProcess更不會給它任何通知.還有, console如果當前不是處於等待或輸出, CTRL+C是沒有反應的.
      

  15.   

    好了,问题我在N天前已经解决。
    谢谢各位的参与,尤其是thomas269(Thomas),我获易匪浅